I try to write some cores for create a pthread with SCHED_RR:
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, ¶ms);
pthread_create(&m_thread, &attr, &startThread, NULL);
pthread_attr_destroy(&attr);
But the thread does't run, do I need set more parameters?
A thread can only set the scheduling to
SCHED_OTHER
without theCAP_SYS_NICE
capability. Fromsched(7)
:That means when you set the scheduling policy to round-robin scheduling (
SCHED_RR
) usingpthread_attr_setschedpolicy()
it's failed in all likelihood (unless you have enabled this capability for the user you are running as or running the program as sysadmin/root user who can overrideCAP_SYS_NICE
).You can set the capability using the
setcap
program:(assuming
a.out
is your program name).You'd have figured this out if you did error checking. You should check the return value of all the pthread functions (and generally all the library functions) for failure.
Since you haven't posted the full code, it might be an issue if you haven't joined with the thread you create (as main thread could exit before the
m_thread
was created and this exit the whole process). So, you might want to join:or you could exit main thread without joining if main thread is no longer needed using
pthread_exit(NULL);
in main().