diff --git a/HW6_thread/README.md b/HW6_thread/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d96ebdeabb7f015db84264c02256a581900a5988 --- /dev/null +++ b/HW6_thread/README.md @@ -0,0 +1,9 @@ +# 실전코딩2 + +## HW6_thread + +## 점수의 합과 평균 출력 하는 thread +## 점수를 성적순으로 sorting 하는 thread + +## 결과 사진 + \ No newline at end of file diff --git a/HW6_thread/image/1.png b/HW6_thread/image/1.png new file mode 100644 index 0000000000000000000000000000000000000000..67f7a448706db3f8b058f17deb2399271e96a8b1 Binary files /dev/null and b/HW6_thread/image/1.png differ diff --git a/HW6_thread/input.txt b/HW6_thread/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..40f32378c5bcab61c98c8c2db26fa66189760222 --- /dev/null +++ b/HW6_thread/input.txt @@ -0,0 +1,30 @@ +55 +14 +17 +49 +28 +19 +39 +45 +68 +10 +1 +5 +7 +9 +11 +23 +24 +29 +31 +38 +81 +86 +84 +94 +92 +67 +73 +79 +99 +97 \ No newline at end of file diff --git a/HW6_thread/thread.c b/HW6_thread/thread.c new file mode 100644 index 0000000000000000000000000000000000000000..b5a6e0cbb1d550d10c05284297562423067e83cb --- /dev/null +++ b/HW6_thread/thread.c @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> +#define size 30 + +int max = 0; +int sum = 0; +short int score[size]; + +int compare(const void* a, const void* b); + +int compare(const void* a, const void* b) +{ + if (*(short*)a > * (short*)b) + return 1; + else if (*(short*)a < *(short*)b) + return -1; + else + return 0; +} + +void* score_thread(void* arg) +{ + + printf("score_Thread Start!!\n"); + for (int i = 0; i < size; i++) { + scanf("%hd", &score[i]); + } + for(int i = 0; i < size; i++) { + sum += score[i]; + sleep(1); + } + printf("평균 = %d\nSum = %d\n", sum/size, sum); + printf("score_Thread End!!\n"); + + pthread_exit(arg); +} + +void* sort_thread(void* arg) +{ + printf("sort_Thread Start!!\n"); + qsort(score, size, sizeof(score[0]), compare); + printf("성적순 정렬:"); + for (int i = size-1; i >= 0; i--) { + printf(" %hd", score[i]); + } + printf("\n"); + printf("sort_Thread End!!\n"); + + pthread_exit(arg); +} + +int main(void) +{ + pthread_t tid; + pthread_t tid2; + int status; + int status2; + + printf("Main_Thread Start!!\n"); + status = pthread_create(&tid, NULL, score_thread, (void*)3); + status2 = pthread_create(&tid2, NULL, sort_thread, (void*)3); + + if (status != 0) { + perror("thread create"); + exit(1); + } + if (status != 0) { + perror("thread create"); + exit(1); + } + for (int i = 1; i <= 4; i++) { + sleep(1); + } + + printf("Main_Thread End!!\n"); + pthread_exit(NULL); + return 0; + +}