|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <pthread.h> |
| 5 | + |
| 6 | +void *ssu_thread1(void *arg); |
| 7 | +void *ssu_thread2(void *arg); |
| 8 | + |
| 9 | +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
| 10 | +pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; |
| 11 | +pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; |
| 12 | +pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; |
| 13 | + |
| 14 | +int count = 0; |
| 15 | +int input = 0; |
| 16 | +int t1 = 0, t2 = 0; |
| 17 | + |
| 18 | +int main(void) |
| 19 | +{ |
| 20 | + pthread_t tid1, tid2; |
| 21 | + int status; |
| 22 | + |
| 23 | + if (pthread_create(&tid1, NULL, ssu_thread1, NULL) != 0) { |
| 24 | + fprintf(stderr, "pthread_create error\n"); |
| 25 | + exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + if (pthread_create(&tid2, NULL, ssu_thread2, NULL) != 0) { |
| 29 | + fprintf(stderr, "pthread_create error\n"); |
| 30 | + exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + while (1) { |
| 34 | + printf("2개 이상의 개수 입력: "); |
| 35 | + scanf("%d", &input); |
| 36 | + |
| 37 | + if (input >= 2) { |
| 38 | + pthread_cond_signal(&cond1); // ssu_thread1을 깨움 |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pthread_join(tid1, (void*)status); |
| 44 | + pthread_join(tid2, (void*)status); |
| 45 | + |
| 46 | + printf("complete\n"); |
| 47 | + exit(0); |
| 48 | +} |
| 49 | + |
| 50 | +void *ssu_thread1(void *arg) { |
| 51 | + while (1) { |
| 52 | + |
| 53 | + pthread_mutex_lock(&mutex1); // 뮤텍스 락 설정 |
| 54 | + |
| 55 | + if (input < 2) |
| 56 | + pthread_cond_wait(&cond1, &mutex1); // 시그널 대기 |
| 57 | + |
| 58 | + if (input == count) { |
| 59 | + pthread_cond_signal(&cond2); // ssu_thread2에 시그널 전송 |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + if (count == 0) { |
| 64 | + t2++; |
| 65 | + count++; |
| 66 | + printf("Thread 1 : %d\n", t1); |
| 67 | + } else if (count % 2 == 0) { |
| 68 | + t1 += t2; |
| 69 | + count++; |
| 70 | + printf("Thread 1 : %d\n", t1); |
| 71 | + } |
| 72 | + |
| 73 | + pthread_cond_signal(&cond2); // ssu_thread2에 시그널 전송 |
| 74 | + pthread_cond_wait(&cond1, &mutex1); // 시그널 대기 |
| 75 | + pthread_mutex_unlock(&mutex1); // 뮤텍스 락 해제 |
| 76 | + } |
| 77 | + return NULL; |
| 78 | +} |
| 79 | + |
| 80 | +void *ssu_thread2(void *arg) { |
| 81 | + while(1) { |
| 82 | + |
| 83 | + pthread_mutex_lock(&mutex2); // 뮤텍스 락 설정 |
| 84 | + |
| 85 | + if (input < 2) |
| 86 | + pthread_cond_wait(&cond2, &mutex2); // 시그널 대기 |
| 87 | + |
| 88 | + if (input == count) { |
| 89 | + pthread_cond_signal(&cond1); // ssu_thread1에 시그널 전송 |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + if (count == 1) { |
| 94 | + count++; |
| 95 | + printf("Thread 2 : %d\n", t2); |
| 96 | + } else if (count % 2 == 1) { |
| 97 | + t2 += t1; |
| 98 | + count++; |
| 99 | + printf("Thread 2 : %d\n", t2); |
| 100 | + } |
| 101 | + |
| 102 | + pthread_cond_signal(&cond1); // ssu_thread1에 시그널 전송 |
| 103 | + pthread_cond_wait(&cond2, &mutex2); // 시그널 대기 |
| 104 | + pthread_mutex_unlock(&mutex2); // 뮤텍스 락 해제 |
| 105 | + } |
| 106 | + return NULL; |
| 107 | +} |
| 108 | + |
| 109 | + |
0 commit comments