2016-12-28 7 views
1
내가 SCHED_RR와 pthread를 만드는 몇 가지 코어를 작성하려고

실패스레드 생성

pthread_attr_t attr; 
pthread_attr_init(&attr); 
pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); 
pthread_attr_setschedpolicy(&attr, SCHED_RR); 
struct sched_param params; 
params.sched_priority = 10; 
pthread_attr_setschedparam(&attr, &params); 
pthread_create(&m_thread, &attr, &startThread, NULL); 
pthread_attr_destroy(&attr); 

을하지만 스레드가 실행 does't, 나는 더 많은 매개 변수를 설정해야합니까?

답변

0

스레드는 CAP_SYS_NICE 기능이없는 경우에만 SCHED_OTHER으로 예약을 설정할 수 있습니다. sched(7)에서 : 당신이 pthread_attr_setschedpolicy() 당신이로 실행하거나 실행중인 사용자에 대해이 기능을 활성화하지 않는 한이 (모든 가능성에 실패한 것을 사용하여 라운드 로빈 스케줄링 (SCHED_RR)에 대한 스케줄링 정책을 설정할 때 의미

In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) 
    threads can set a nonzero static priority (i.e., set a real-time 
    scheduling policy). The only change that an unprivileged thread can 
    make is to set the SCHED_OTHER policy, and this can be done only if 
    the effective user ID of the caller matches the real or effective 
    user ID of the target thread (i.e., the thread specified by pid) 
    whose policy is being changed. 

프로그램을 CAP_SYS_NICE을 무시할 수있는 sysadmin/root 사용자로 지정하십시오.

당신은 setcap 프로그램을 사용하여 기능을 설정할 수 있습니다

$ sudo setcap cap_sys_nice=+ep ./a.out 

(a.out을 가정하는 프로그램 이름입니다).

오류 검사를했다면이 사실을 알았을 것입니다. 모든 pthread 함수 (일반적으로 모든 라이브러리 함수)의 리턴 값을 검사하여 실패했는지 확인해야합니다. 당신은 전체 코드를 게시하지 않은 때문에 당신이하지 당신이 만든 스레드와에 가입 한 경우

, 그것은 문제가 될 수있다 (주요m_thread가 생성되기 전에 종료 할 수 실이 종료 전체로 방법).

pthread_join(m_thread, NULL); 

을하거나 메인 쓰레드가 더 이상 주에서 pthread_exit(NULL);()를 사용하여 필요한 경우에 가입하지 않고 주 스레드를 종료 할 수 : 그래서, 당신은 가입 할 수 있습니다.