Scheduling parameters -- policy and priority -- are not set when thread is created even though the parameters are set in the thread attributes. Example code below. Output (as root) is: thread id = 0xf701fbb0, policy = SCHED_OTHER, priority = 0 re-setting priority and policy now schedule data for thread is: thread id = 0xf701fbb0, policy = SCHED_RR, priority = 25 #include <stdio.h> #include <string.h> #include <pthread.h> #include <sched.h> #define HPRIO 25 void show_sched(void) { struct sched_param sp; int policy; const char *pstr; if (pthread_getschedparam(pthread_self(), &policy, &sp) != 0) { perror("pthread_getschedparam failed"); return; } switch(policy) { case SCHED_OTHER: pstr = "SCHED_OTHER"; break; case SCHED_FIFO: pstr = "SCHED_FIFO"; break; case SCHED_RR: pstr = "SCHED_RR"; break; default: pstr = "unknown"; } printf("thread id = 0x%lx, policy = %s, priority = %d\n", pthread_self(), pstr, sp.sched_priority); return; } void set_sched(int policy, int priority) { struct sched_param schedp; memset(&schedp, 0, sizeof(schedp)); schedp.sched_priority = priority; if (pthread_setschedparam(pthread_self(), policy, &schedp) != 0) perror("pthread_setschedparam failed"); } void thread1(void) { show_sched(); printf("\nre-setting priority and policy\n"); set_sched(SCHED_RR, HPRIO); printf("\nnow schedule data for thread is:\n"); show_sched(); return; } int main(void) { pthread_attr_t attr; struct sched_param schedp; pthread_t id1; int rc; /* * Create thread at high priority */ if (pthread_attr_init(&attr) != 0) { perror("pthread_attr_init failed"); return 1; } /* set schedule policy */ if (pthread_attr_setschedpolicy(&attr, SCHED_RR) != 0) { perror("pthread_attr_setschedpolicy failed"); return 1; } /* set schedule priority */ schedp.sched_priority = HPRIO; if (pthread_attr_setschedparam(&attr, &schedp) != 0) { perror("pthread_attr_setschedparam failed"); return 1; } /* create thread */ rc = pthread_create(&id1, &attr, (void *(*)(void *))thread1, 0); if (rc) { perror("pthread_create failed"); return(1); } pthread_attr_destroy(&attr); pthread_join(id1, NULL); return(0); }
NPTL defaults to PTHREAD_INHERIT_SCHED, so if you want to use the explicit scheduling parameters, you should also pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED).
------- Additional Comments From jakub at redhat dot com 2004-06-15 08:23 ------- NPTL defaults to PTHREAD_INHERIT_SCHED, so if you want to use the explicit scheduling parameters, you should also pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED).