linux thread priority 에 대한 질문 드립니다.
리눅스를 공부하고 있는 직장인입니다.
최근thread priority 에 대해서 공부를 하는 도중에,
priority를 확인하는 방법이 궁금해서, 아래의 코드를 짜서 수행을 해보았는데요.
제 생각으로론 thread1이 가장 먼저 수행되리라 생각했지만
main -> thread2 -> thread1 이렇게 수행이 된 결과가 printf와 전역변수를 사용한 배열에 값이 들어가더라구요.
priority를 thread 1 이 99, thread2가 1인데 ,, 왜 thread1이 제일 나중에 나올까요...
그리고 세마포어를 thread1과 thread2에 동시에 같은 걸 주고 main에서 sem_post를 하면,
thread2가 실행이 되더라구요..
분명히priority는 thread1이 높은데 결과는 그렇게 나오지 않네요.
제가 잘못해서 그런건지, 이거를 확인할수가 없어서 답답하네요.ㅜㅜ
1 #include
2 #include
3 #include
4 #include
5
6 sem_t th1_sem; //th1 세마포어
7 sem_t th2_sem; //th2 세마포어
8
9 int num[10] = {0,};
10 int count = 0;
11 void * thread1()
12 {
13 sem_wait(&th1_sem);
14 sleep(1);
15 printf("thread1 start...\n");
16
17 num[count]=1;
18 count ++;
19 int i;
20 int j=0;
21 for(i = 0; i<1000000; i++){
22 j=j++;
23 }
24
25 printf("thread1 end...\n");
26 }
27
28 void * thread2()
29 {
30 sem_wait(&th2_sem);
31 sleep(1);
32 printf("thread2 start...\n");
33
34 num[count]=2;
35 count ++;
36 int i;
37 int j=0;
38 for(i = 0; i<1000000; i++){
39 j=j++;
40 }
41 printf("thread2 end...\n");
42 }
43
44 int main()
45 {
46 pthread_t tid1,tid2; // 쓰레드 변수 선언
47
48 pthread_attr_t attr1, attr2; // attr변수 선언
49 struct sched_param schedparam1,schedparam2;
50
51 int th1_priority;
52 int th2_priority;
53 int ii,n;
54
55 if(pthread_attr_init(&attr1) !=0){
56 perror("pthread_attr1_init");
57 exit(1);
58 }
59 if(pthread_attr_init(&attr2) !=0){
60 perror("pthread_attr2_init");
61 exit(1);
62 }
63
64 if(pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_JOINABLE) !=0){
65 perror("pthread_attr1_setdetachstate");
66 exit(1);
67 }
68 if(pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_JOINABLE) !=0){
69 perror("pthread_attr2_setdetachstate");
70 exit(1);
71 }
72
73 if(pthread_attr_setschedpolicy(&attr1,SCHED_FIFO) !=0 ){
74 perror("pthread_attr1_schedpolicy");
75 exit(1);
76 }
77 if(pthread_attr_setschedpolicy(&attr2,SCHED_FIFO) !=0 ){
78 perror("pthread_attr2_schedpolicy");
79 exit(1);
80 }
81 th1_priority=sched_get_priority_max(SCHED_FIFO);
82 th2_priority=sched_get_priority_min(SCHED_FIFO);
83
84 printf("th1_priority = %d\n", th1_priority);
85 printf("th2_priority = %d\n", th2_priority);
86
87
88 schedparam1.sched_priority = th1_priority;
89 schedparam2.sched_priority = th2_priority;
90
91 if(pthread_attr_setschedparam(&attr2,&schedparam2) !=0 ){
92 perror("pthread_attr_schedparam2");
93 exit(1);
94 }
95 if(pthread_attr_setschedparam(&attr1,&schedparam1) !=0 ){
96 perror("pthread_attr_schedparam1");
97 exit(1);
98 }
99
100 printf("th1_priority = %d\n", schedparam1.sched_priority);
101 printf("th2_priority = %d\n", schedparam2.sched_priority);
102
103 pthread_setschedprio(tid1, th1_priority);
104 pthread_setschedprio(tid2, th2_priority);
105
106 //세마포어 초기화
107 sem_init(&th1_sem,0,0);
108 sem_init(&th2_sem,0,0);
109
110 // 쓰레드 1,2 생성
111 if(pthread_create(&tid1, &attr1,&thread1, NULL) || pthread_create(&tid2, &attr2, &thread2, NULL) )
112 {
113 printf("n ERROR creating thread");
114 exit(1);
115 }
116
117 sleep(1);
118 sem_post(&th1_sem); // 세마포어 동작
119 sem_post(&th2_sem); // 세마포어 동작
120
121 sleep(1);
122 printf("main thread start...\n");
123
124 num[count] = 3;
125 count ++;
126 for(n=0; n<10; n++){
127 printf("num(%d) = thread %d\n",n,num[n]);
128
129 if(pthread_join(tid1, NULL)) // th1 끝나기를 기다림
130 {
131 printf("n ERROR joining thread 1");
132 exit(1);
133 }
134 if(pthread_join(tid2, NULL)) //th2 끝나기를 기다림
135 {
136 printf("n ERROR joining thread 2");
137 exit(1);
138 }
139
140 sem_destroy(&th1_sem);
141 sem_destroy(&th2_sem);
142
143 printf("main finished...\n");
144 //main 쓰레드 종료
145 pthread_exit(NULL);
146
147 return 0;
148 }
103
103 pthread_setschedprio(tid1, th1_priority);
104 pthread_setschedprio(tid2, th2_priority);
105
106 //세마포어 초기화
107 sem_init(&th1_sem,0,0);
108 sem_init(&th2_sem,0,0);
109
110 // 쓰레드 1,2 생성
111 if(pthread_create(&tid1, &attr1,&thread1, NULL) || pthread_create(&tid2, &attr2, &thread2, NULL) )
112 {
113 printf("n ERROR creating thread");
114 exit(1);
115 }
111번 줄이 실행되어야지만 tid가 설정이 되는데 103, 104에서 어떻게 없는 tid에 우선순위를 설정한다는 것이지요?
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.
댓글 달기