Skip to content
Snippets Groups Projects
Commit b8745f7f authored by 이장원's avatar 이장원
Browse files

thread

parent 302ec23f
No related branches found
No related tags found
No related merge requests found
CC = gcc
OBJS = thread.o
test: $(OBJS)
$(CC) -o $@ -lpthread $^
thread.o: thread.c
$(CC) -c $^
clean:
rm $(OBJS) test
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
#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;
}
Images/thread.png

140 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment