2016-07-15 2 views
1

나는 리눅스 세마포어의 보류 정책을 테스트 중이다.리눅스 semaphore.h 보류 중/대기 중 대기 정책

리눅스의 맨 보류중인 정책을 말하지 않습니다 http://linux.die.net/man/3/sem_wait,

아마 스케줄러에 의해 결정됩니다. 즉,이 세마포어에서 우선 순위가 가장 높은 스레드가 먼저 실행될 수 있습니다.

그래서 우선 순위 10, 20, 30, 40으로 4 개의 pthread를 만들고 SCHED_FIFO에 스케줄러 정책을 설정합니다.

그러나 테스트 결과 보류 정책은 FIFO임을 나타냅니다. 기다리는 정책 정보를 위해 semaphore.h의 문서 나 소스 코드가 있는지 아는 사람이 있습니까?

감사

#include <string.h> 
#include <pthread.h> 
#include <semaphore.h> 
#include "stdlib.h" 
#include "stdio.h" 

sem_t b_sem; 

int test_routine(int my_num) 
{ 
    usleep(1+ my_num * 1000); 
    printf("I'm %d, going to wait sem\n", my_num); 
    sem_wait(&b_sem); 
    printf("I'm %d, I got the sem\n", my_num); 
    while(1) 
    { 
      sleep(1); 
    } 
} 

int main() 
{ 
    int i; 
    int pthrid[4] = {0,0,0,0}; 
    pthread_attr_t attr[4]; 
    struct sched_param prv_priority[4]; 

    struct sched_param param; 
    param.sched_priority = sched_get_priority_max(SCHED_FIFO); 
    sched_setscheduler(0, SCHED_FIFO, &param); 

    sem_init(&b_sem, 1, 0); 

    for(i = 0 ; i < 4 ; i++) 
    { 
      pthread_attr_init(&attr[i]); 
      pthread_attr_getschedparam(&attr[i], &prv_priority[i]); 
      pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO); 
      prv_priority[i].sched_priority = (10 + i*10); 
      printf("test start, thread %d has priority %d\n", i ,prv_priority[i].sched_priority); 
      pthread_attr_setschedparam(&attr[i], &prv_priority[i]); 
      pthread_create(&pthrid[i], &attr[i], test_routine, i); 
    } 

    sleep(1); 

    for(i = 0 ; i < 4 ; i++) 
    { 
      printf("give sem\n"); 
      sem_post(&b_sem); 
      sleep(1); 
    } 

    sleep(100000); 

} 

결과 :

test start, thread 0 has priority 10 
test start, thread 1 has priority 20 
test start, thread 2 has priority 30 
test start, thread 3 has priority 40 
I'm 0, going to wait sem 
I'm 1, going to wait sem 
I'm 2, going to wait sem 
I'm 3, going to wait sem 
give sem 
I'm 0, I got the sem 
give sem 
I'm 1, I got the sem 
give sem 
I'm 2, I got the sem 
give sem 
I'm 3, I got the sem 

답변

0

귀하의 테스트가 끊어집니다.

기본적으로 새 스레드는 스레드를 만든 스레드의 일정 정책을 상속받습니다. 이 기본값을 무시하지 마십시오. 당신이 pthread_create를 호출하기 전에이 작업을 수행 :

 pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED); 

또한, root로 프로그램을 실행하거나 그렇지 않으면 SCHED_RR을 사용하는 권한을 부여해야 하나.

그런데 하나의 중요한 단서는 ps axlm을 사용하여 스레드 우선 순위를 확인하고 모두 동일하다는 것을 확인하는 것이 었습니다.

+0

고맙습니다. "pthread_attr_setinheritsched"뒤에 예외 결과가 있습니다. – Wanga