pthread의 create와 schdueling의 실행 시간에 관환 질문입니다.
제가 우선 순위가 각각 50,70인 쓰레드를 순서대로 라운드 로빈 스케줄링 정책하에 생성합니다.그리고 각 쓰레드는 쓰레드 루틴을 종료한후 각각 usleep(1000),usleep(500)을 주기로 실행됩니다.
제가 생각한 결과는 우선 순위가 70인 쓰레드가 먼저 실행되면서 결과가 나와야 합니다.그런데 아래 결과를 보시면 처음에는 우선순위가 50인 쓰레드가 실행되고 얼마 후 우선순위가 70인 쓰레드가 실행되며 그 이후에서야 RR (라운드로빈)정책에 따란 프로세서를 스케즐링합니다.
[질문] 제의 짧은 생각으로는 pthread_create()실행후 쓰레드가 생성 시킬때가지 어느 정도 시간이 필요하고, 따라서 두번째 쓰레드가 메모리에 적제 되기전까지는 운영체제에서 첫번째 쓰레드만을 계속 실행되는 것같은데요. 고수님들의 의견 듣고 싶습니다.실행 결과가 이렇게 나와는 이유가 무엇일까요 ?
실행 결과
maxium pri = 99
minium pri = 1
쓰레드 파라미터 , 상속, 우선순위 설정 완료
thread_routine 0 running at PR / 50
thread id = 0 / i = 0
thread id = 0 / i = 10000
thread id = 0 / i = 20000
thread id = 0 / i = 30000
thread id = 0 / i = 40000
thread id = 0 / i = 50000
thread id = 0 / i = 60000
thread id = 0 / i = 70000
thread id = 0 / i = 80000
thread id = 0 / i = 90000 <---------- pri 50 /한번 실행
thread id = 0 / i = 0
thread id = 0 / i = 10000
thread id = 0 / i = 20000
thread id = 0 / i = 30000
thread id = 0 / i = 40000
thread id = 0 / i = 50000
thread id = 0 / i = 60000
thread id = 0 / i = 70000
thread id = 0 / i = 80000
thread id = 0 / i = 90000 <---------- pri 50 /두번 실행
thread_routine 1 running at PR / 70
thread id = 1 / i = 0
thread id = 1 / i = 10000
thread id = 1 / i = 20000
thread id = 1 / i = 30000
thread id = 1 / i = 40000
thread id = 1 / i = 50000
thread id = 1 / i = 60000
thread id = 1 / i = 70000
thread id = 1 / i = 80000
thread id = 1 / i = 90000 <---------- pri 70 /한번 실행 종료
thread id = 0 / i = 0 <---------- pri 50 /세번째 실행 시작
thread id = 0 / i = 10000
thread id = 0 / i = 20000
thread id = 1 / i = 0 <---------- pri 70 /두번 실행 시작 / RR적책하에 pri 50으로 부터 자원 선점 시작
thread id = 1 / i = 10000
thread id = 1 / i = 20000
thread id = 1 / i = 30000
thread id = 1 / i = 40000
thread id = 1 / i = 50000
thread id = 1 / i = 60000
thread id = 1 / i = 70000
thread id = 1 / i = 80000
thread id = 1 / i = 90000 <---------- pri 70 /두번째 실행 종료
thread id = 0 / i = 30000
thread id = 0 / i = 40000
thread id = 0 / i = 50000
thread id = 1 / i = 0 <---------- pri 70 /세 번째 실행 시작
thread id = 1 / i = 10000
thread id = 1 / i = 20000
thread id = 1 / i = 30000
thread id = 1 / i = 40000
thread id = 1 / i = 50000
thread id = 1 / i = 60000
thread id = 1 / i = 70000
thread id = 1 / i = 80000
thread id = 1 / i = 90000 <---------- pri 70 /세 번째 실행 종료
thread id = 0 / i = 60000
thread id = 0 / i = 70000
thread id = 1 / i = 0
thread id = 1 / i = 10000
thread id = 1 / i = 20000
thread id = 1 / i = 30000
thread id = 1 / i = 40000
thread id = 1 / i = 50000
thread id = 1 / i = 60000
thread id = 1 / i = 70000
thread id = 1 / i = 80000
thread id = 1 / i = 90000
thread id = 0 / i = 80000
thread id = 0 / i = 90000 <---------- pri 50 /세 번째 실행 종료
thread id = 1 / i = 0
thread id = 1 / i = 10000
thread id = 1 / i = 20000
thread id = 1 / i = 30000
thread id = 1 / i = 40000
thread id = 1 / i = 50000
thread id = 1 / i = 60000
thread id = 1 / i = 70000
thread id = 1 / i = 80000
thread id = 1 / i = 90000
thread id = 0 / i = 0
thread id = 0 / i = 10000
thread id = 0 / i = 20000
thread id = 1 / i = 0
thread id = 1 / i = 10000
thread id = 1 / i = 20000
...
----------------------------소스 코드 ----------------------------------------------
/* date : 08 07 06
* 컴파일 : gcc -o testpri testprie.c -lpthread
* 실행 명령어 : sodo ./testpri2 (ubuntu )
* 참고 페이지 : http://man.kldp.org/wiki/ManPage/pthread_setschedparam.3
* 참고 : pthread_setschedparam은 수퍼 유저 권한을 가지 사용자가 실행하여야한다.
* 문제 : 쓰레드간 선점이 일어 나지 않는거같다.
*/
#include
#include
#include
#define THREADS 2
/*각 스레드를 나타내는 구조체 */
typedef struct thread_tag{
int index;
pthread_t id;
pthread_attr_t thread_attr;
} thread_t;
thread_t threads[THREADS];
int rr_min_priority,rr_max_priority;
/*쓰레드 실행 루틴 */
void *thread_routine (void * arg)
{
thread_t *self = (thread_t *) arg;
int my_policy; //현재 쓰레드의 스케줄링 정책 확인용
struct sched_param my_param; //현재 쓰레드의 파라미터값 확인용
int status;
int i=0;
//스케줄링 정책 파마리터 확인
status = pthread_getschedparam(self->id, &my_policy, &my_param);
if (status!= 0)
printf("error at geting sched param\n");
// 읽어온 쓰레드 정보 출력
printf("thread_routine %d running at %s / %d\n",
self->index,
(my_policy == SCHED_FIFO ? "FIFO"
: (my_policy == SCHED_RR ? "PR"
: (my_policy == SCHED_OTHER ? "OTHRER"
: "unknown"))),
my_param.sched_priority);
//작업 수행
while(1){
for(i=0; i<10000*10;i++)
{ if ( i % 10000 ==0 )printf ("thread id = %d / i = %d\n",self->index, i);
}
if( self->index == 0 )usleep(1000);
else usleep(500);
//printf ("thread id = %d / i = %d\n",self->index, i);
//sleep(2);
}
}
int main( int argc, char *arg[])
{
struct sched_param thread_param;
int count, status;
int total_roof;
total_roof=0;
// 쓰레드 스케줄링 지원 여부 체크
#if defined (_POSIX_THREAD_PRIORITY_SCHEDULING) && !defined (sun)
//우선순위 최고값 최소값 확인
rr_max_priority = sched_get_priority_max(SCHED_RR);
printf("maxium pri = %d\n" , rr_max_priority);
rr_min_priority = sched_get_priority_min(SCHED_RR);
printf("minium pri = %d\n" , rr_min_priority);
for (count = 0; count < THREADS; count++)
{
//스케줄링 정책 셋팅
status = pthread_attr_setschedpolicy( &threads[count].thread_attr, SCHED_RR);
//스케줄링 우선 순위 설정 ( 우선 순위= (최고 +최소)/2 + count*5 )
thread_param.__sched_priority= ( rr_min_priority + rr_max_priority)/2 + count*20;
status = pthread_attr_setschedparam( &threads[count].thread_attr , &thread_param);
//스케줄링 상속 설정
status = pthread_attr_setinheritsched(&threads[count].thread_attr, PTHREAD_EXPLICIT_SCHED);
}
printf ("쓰레드 파라미터 , 상속, 우선순위 설정 완료\n");
#else
printf(" 쓰레드 스케줄링이 지원되지 않습니다\n");
#endif
//쓰레드 생성 및 실행
for(count = 0; count < THREADS ; count++)
{
threads[count].index = count;
status = pthread_create (
&threads[count].id, &threads[count].thread_attr,
thread_routine, (void *)&threads[count]);
if (status!= 0)
printf("error at creating thread/n");
}
//쓰레드 종료대기
for(count = 0; count < THREADS; count++)
{
status = pthread_join ( threads[count].id, NULL);
if (status!= 0)
printf("error at joying thread/n");
}
printf ("메인 종료\n");
return 0;
}
그럼 block 된 상태로 생성하세요.
정확한 표준과 coding 방식은 모르겠지만 block 된 상태로 두 개의 thread를 생성하고 나서
block을 동시에 풀어주면 되겠군요.
즉 lock을 걸어놓고 생성한 후 lock을 풀면 될 것 같습니다.
아마 barrier라고 하는 것 같던데...
댓글 달기