diff --git a/mutex/1.mytwothread.c b/mutex/1.mytwothread.c new file mode 100644 index 0000000000000000000000000000000000000000..b7583aca59336435137b2c1c073ee3c830b13fbe --- /dev/null +++ b/mutex/1.mytwothread.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <pthread.h> + +void * thread_function(void *); + +int Counter=0; + +int main(void) +{ + pthread_t tid1, tid2; + void *thread_result; + + if(pthread_create(&tid1, NULL, thread_function, "thread1")!=0) { + perror("pthread_create"); + exit(1); + } + if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) { + perror("pthread_create"); + exit(1); + } + + if(pthread_join(tid1, &thread_result)!=0) { + perror("pthread_join"); + exit(1); + } + if(pthread_join(tid2, &thread_result)!=0) { + perror("pthread_join"); + exit(1); + } + + printf(" thread result : %s\n", (char *) thread_result); + return 0; +} + +void * thread_function(void * arg) +{ + int temp; + int i, j; + char buffer[80]; + + printf("thread_function called\n"); + for(i=0; i<20; i++) { + sprintf(buffer, "%s : Counter : from %d to ", (char*)arg, Counter); + write(1, buffer, strlen(buffer)); + + temp=Counter; + temp = temp+1; + +// delay + for(j=0; j<500000; j++); + + Counter=temp; + + sprintf(buffer, " %d\n", Counter); + write(1, buffer, strlen(buffer)); + + } + pthread_exit("thread end"); +} diff --git a/mutex/2.mymutexthread_st.c b/mutex/2.mymutexthread_st.c new file mode 100644 index 0000000000000000000000000000000000000000..c1c2143419b6e611fd99087077ea713365c52d8c --- /dev/null +++ b/mutex/2.mymutexthread_st.c @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h> +#include <sys/types.h> +#include <errno.h> + +#include <unistd.h> +#include <sys/syscall.h> + +void * thread_function(void *); + +int Counter=0; +#ifndef NOMUTEX +pthread_mutex_t mutex_id; +#endif + +int main(void) +{ + pthread_t tid1, tid2; + void *thread_result; + +#ifndef NOMUTEX + if(pthread_mutex_init(&mutex_id, NULL) != 0){ + perror("pthread_mutex_init"); + exit(errno); + } +#endif + + if(pthread_create(&tid1, NULL, thread_function, "thread1")!=0) { + perror("pthread_create"); + exit(1); + } + + if(pthread_create(&tid2, NULL, thread_function, "thread2")!=0) { + perror("pthread_create"); + exit(1); + } + + if(pthread_join(tid1, &thread_result)!=0) { + perror("pthread_join"); + exit(1); + } + if(pthread_join(tid2, &thread_result)!=0) { + perror("pthread_join"); + exit(1); + } + +#ifndef NOMUTEX + pthread_mutex_lock(&mutex_id); +#endif + + printf(" thread result : %s\n", (char *) thread_result); + return 0; +} + +void * thread_function(void * arg) +{ + int temp; + int i, j; + char buffer[80]; + + printf("thread_function called\n"); + for(i=0; i<8; i++) { +#ifndef NOMUTEX + pthread_mutex_lock(&mutex_id); +#endif + sprintf(buffer, "%s: CountRelay: from %d to ", (char*)arg, Counter); + write(1, buffer, strlen(buffer)); + + temp=Counter; + temp = temp+1; +// delay + for(j=0; j<5000000; j++); + + Counter=temp; + + sprintf(buffer, "%d\n", Counter); + write(1, buffer, strlen(buffer)); + +#ifndef NOMUTEX + pthread_mutex_unlock(&mutex_id); +#endif + } + // getchar(); + pthread_exit("thread end"); +}