diff --git a/0812/Makefile b/0812/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..ab1d8771b14dc7d8cc6e569d2fde852a8da58c11 --- /dev/null +++ b/0812/Makefile @@ -0,0 +1,11 @@ +CC = gcc +OBJS = thread.o + +test: $(OBJS) + $(CC) -o $@ -lpthread $^ + +thread.o: thread.c + $(CC) -c $^ + +clean: + rm $(OBJS) test diff --git a/0812/input_scores b/0812/input_scores new file mode 100644 index 0000000000000000000000000000000000000000..f963fa6aeba42e3f7348d786eb668fb5fa97c815 --- /dev/null +++ b/0812/input_scores @@ -0,0 +1,30 @@ +75 +70 +73 +97 +47 +12 +33 +62 +81 +21 +4 +7 +27 +79 +34 +13 +38 +16 +29 +43 +14 +87 +28 +45 +71 +65 +89 +57 +93 +67 \ No newline at end of file diff --git a/0812/thread.c b/0812/thread.c new file mode 100644 index 0000000000000000000000000000000000000000..6e749224b031f0e7e91100fd6ca9103e89f28bd1 --- /dev/null +++ b/0812/thread.c @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> + +short scores[30]; + +void *sthread1(void *arg) +{ + printf("sthread1 start!\n"); + short sum = 0, i; + float avg; + + for (i = 0; i < 30; i++) + { + sum += scores[i]; + } + avg = (float)sum / 30; + + printf("Sum : %hd, Average : %f\n", sum, avg); + printf("sthread1 end!\n"); + pthread_exit(arg); +} + +void *sthread2(void *arg) +{ + printf("sthread2 start!\n"); + short i, j, temp; + + for (i = 0; i < 29; i++) + { + for (j = 0; j < 29 - i; j++) + { + if (scores[j] < scores[j + 1]) + { + temp = scores[j]; + scores[j] = scores[j + 1]; + scores[j + 1] = temp; + } + } + } + + printf("Sorted scores\n"); + for (i = 0; i < 30; i++) + { + printf("%2hd ", scores[i]); + if (i % 10 == 9) + { + printf("\n"); + } + } + printf("sthread2 end!\n"); + pthread_exit(arg); +} + +int main() +{ + printf("Main thread start!\n"); + pthread_t tid1, tid2; + int status1, status2, i; + + for (i = 0; i < 30; i++) + { + scanf("%hd", &scores[i]); + } + + status1 = pthread_create(&tid1, NULL, sthread1, (void*)scores); + if (status1 != 0) + { + perror("thread1 create"); + exit(1); + } + + status2 = pthread_create(&tid2, NULL, sthread2, (void*)scores); + if (status2 != 0) + { + perror("thread2 create"); + exit(1); + } + + printf("Main thread end!\n"); + pthread_exit(NULL); + return 0; +} diff --git a/Images/thread.png b/Images/thread.png new file mode 100644 index 0000000000000000000000000000000000000000..25a7bca024ab9de6b9bb545897290d40f8e6d31e Binary files /dev/null and b/Images/thread.png differ