diff --git a/08_14_mutex/1.mytwothread.c b/08_14_mutex/1.mytwothread.c new file mode 100644 index 0000000000000000000000000000000000000000..5f5f3521779cdf4fe394c9854a4261b1b952921a --- /dev/null +++ b/08_14_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); //sprintf : 배열에 찍는것 + 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/08_14_mutex/2.mymutexthread_st.c b/08_14_mutex/2.mymutexthread_st.c new file mode 100644 index 0000000000000000000000000000000000000000..c1c2143419b6e611fd99087077ea713365c52d8c --- /dev/null +++ b/08_14_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"); +} diff --git a/08_14_mutex/ledThreads/Makefile b/08_14_mutex/ledThreads/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..6e9dcdc6ec2d07a1072f54db87cc3eb905ac761a --- /dev/null +++ b/08_14_mutex/ledThreads/Makefile @@ -0,0 +1,17 @@ +SRCS := led_main.c led_green.c led_yellow.c +OBJS := $(SRCS:.c=.o) +CC := gcc +#컴파일 옵션 +CFLAGS = -Wall -g +#linking 옵션 +LDFLAGS = -lpthread -lwiringPi + +led_main : $(OBJS) + $(CC) -o $@ $^ ${LDFLAGS} + +# dummy target +clean : + -rm $(OBJS) + -rm led_main +dep : + gccmakedep ${SRCS} \ No newline at end of file diff --git a/08_14_mutex/ledThreads/led.h b/08_14_mutex/ledThreads/led.h new file mode 100644 index 0000000000000000000000000000000000000000..9782bedb1cb157b902813db4a098e14cd3a8961d --- /dev/null +++ b/08_14_mutex/ledThreads/led.h @@ -0,0 +1,12 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <wiringPi.h> +#include <pthread.h> + +#define LED_RED 1 +#define LED_GREEN 4 +#define LED_YELLOW 5 + +void* led_yellow_task(void* args); +void* led_green_task(void* args); \ No newline at end of file diff --git a/08_14_mutex/ledThreads/led_green.c b/08_14_mutex/ledThreads/led_green.c new file mode 100644 index 0000000000000000000000000000000000000000..6a7cbd54a12973abbe3f7caa954da4e3e2d4be5e --- /dev/null +++ b/08_14_mutex/ledThreads/led_green.c @@ -0,0 +1,17 @@ +#include "led.h" + +void * led_green_task(void *arg) +{ + int i; + pinMode(LED_GREEN, OUTPUT); + + for(i=0; i<(int)arg; i++) { + digitalWrite(LED_GREEN, HIGH); + printf("GREEN ON!\n"); + sleep(2); + digitalWrite(LED_GREEN, LOW); + printf("GREEN OFF!\n"); + sleep(2); + } + pthread_exit(arg); +} \ No newline at end of file diff --git a/08_14_mutex/ledThreads/led_main b/08_14_mutex/ledThreads/led_main new file mode 100644 index 0000000000000000000000000000000000000000..a33595168cd6a255657d4aefce298c7589060022 Binary files /dev/null and b/08_14_mutex/ledThreads/led_main differ diff --git a/08_14_mutex/ledThreads/led_main.c b/08_14_mutex/ledThreads/led_main.c new file mode 100644 index 0000000000000000000000000000000000000000..26fbdc92dbf5b451d9d34afd0167402d9f499af7 --- /dev/null +++ b/08_14_mutex/ledThreads/led_main.c @@ -0,0 +1,67 @@ +#include "led.h" + +void led_red_task() +{ + pinMode(LED_RED, OUTPUT); + + for(int i=0; i<10; i++) { + digitalWrite(LED_RED, HIGH); + printf("RED ON!\n"); + sleep(1); + digitalWrite(LED_RED, LOW); + printf("RED OFF!\n"); + sleep(1); + } +} + +int main(int argc, char *argv[]) +{ + int gno; + int count; + pthread_t tid_Y, tid_G; + + if(argc < 2) { + wiringPiSetup(); + led_red_task(); + } + else{ + gno = atoi(argv[1]); + count = atoi(argv[2]); + + wiringPiSetup(); + + if (gno == 1) { + led_red_task(); + if (pthread_create(&tid_G, NULL, led_green_task, (void*)count) != 0) + { + perror("pthread_create"); + exit(1); + } + } else if (gno == 2) { + led_red_task(); + if (pthread_create(&tid_Y, NULL, led_yellow_task, (void*)count) != 0) + { + perror("pthread_create"); + exit(1); + } + } else if(gno == 3){ + led_red_task(); + if (pthread_create(&tid_Y, NULL, led_yellow_task, (void*)count) != 0) + { + perror("pthread_create"); + exit(1); + } + if (pthread_create(&tid_G, NULL, led_green_task, (void*)count) != 0) + { + perror("pthread_create"); + exit(1); + } + } + } + pthread_exit(NULL); + return 0; +} + + + + diff --git a/08_14_mutex/ledThreads/led_yellow.c b/08_14_mutex/ledThreads/led_yellow.c new file mode 100644 index 0000000000000000000000000000000000000000..c87ba7104bd02ff9c7d8ee0bd801a048013f96bb --- /dev/null +++ b/08_14_mutex/ledThreads/led_yellow.c @@ -0,0 +1,18 @@ +#include "led.h" + +void * led_yellow_task(void *arg) +{ + int i; + pinMode(LED_YELLOW, OUTPUT); + + for(i=0; i<(int)arg; i++) { + digitalWrite(LED_YELLOW, HIGH); + printf("YELLOW ON!\n"); + sleep(3); + digitalWrite(LED_YELLOW, LOW); + printf("YELLOW OFF!\n"); + sleep(3); + } + pthread_exit(arg); + +} \ No newline at end of file