From 60b897cd539159d5f087f72b01384b031d2b93a8 Mon Sep 17 00:00:00 2001 From: Jieun <yjeka@ajou.ac.kr> Date: Wed, 19 Aug 2020 21:02:59 +0900 Subject: [PATCH] Fix Project8 --- Project8 - mutex/2.mymutexthread_st.c | 174 +++++++++++++------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/Project8 - mutex/2.mymutexthread_st.c b/Project8 - mutex/2.mymutexthread_st.c index c1c2143..58e95bf 100644 --- a/Project8 - mutex/2.mymutexthread_st.c +++ b/Project8 - mutex/2.mymutexthread_st.c @@ -1,87 +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"); -} +#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"); +} -- GitLab