problem in assigning real time scheduling to pthread in linux
Hi,
I am using RH9-2.4.21 , NPTL version 0.29 I am trying to assign realtime scheduling to the pthreads but facing a strange problem. I am unable to assign the scheduling policy and priority to a thread while creating it. after the creation i am able to change its policy and priority (using pthread_setschedparam). I am working as root . I even tried with PTHREAD_EXPLICIT_SCHED ,( although this is the default behaviour), but of no use following is the code : #include<stdio.h> #include<pthread.h> #include<errno.h> #include<sched.h> void* CaptureThread() { int my_policy; struct sched_param my_param; int status; status = pthread_getschedparam (pthread_self (), &my_policy, &my_param); printf ("# :thread_routine running at %s/%d\n", (my_policy == SCHED_FIFO ? "FIFO" : (my_policy == SCHED_RR ? "RR" : (my_policy == SCHED_OTHER ? "OTHER" : "unknown"))), my_param.sched_priority); } pthread_t m_thread; int main(void) { pthread_attr_t thread_attr; struct sched_param thread_param; pthread_attr_init(&thread_attr); pthread_attr_setschedpolicy(&thread_attr, SCHED_RR); thread_param.sched_priority = 50; pthread_attr_setschedparam(&thread_attr, &thread_param); pthread_create(&m_thread, &thread_attr, CaptureThread, NULL); pthread_join(m_thread,NULL); } OUTPUT which i got on running this code : # :thread_routine running at OTHER/0 here i am just trying to assign scheduling policy as RR and priority 50 and creating a pthread. Inside the thread function i am printing the sched Policy and priority. Ideally it should print policy = RR and priority = 50 ; but instead its printing policy = OTHER and priority = 0 where am i making mistake? Thanks Jasdeep |
You can try the following code...
#include <stdio.h>
#include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <pthread.h> #include <sched.h> // Check rc(return code) and exit program if func call failed static void checkRC(char *msg, int rc) { if (rc) { printf("err: %s, rc=%d\n", msg, rc); exit(EXIT_FAILURE); } return; } void *thdfunc(void *tid) { struct sched_param sp; int rc, policy, cnt=0; while (1) { rc=pthread_getschedparam(pthread_self(), &policy, &sp); checkRC("pthread_getschedparam", rc); printf ("Thrd: Running at %s/%d\n", (policy == SCHED_FIFO ? "FIFO" : (policy == SCHED_RR ? "RR" : (policy == SCHED_OTHER ? "OTHER" : "unknown"))), sp.sched_priority); sleep(1); if (++cnt==5) break; } return NULL; } int main(void) { pthread_t thd; pthread_attr_t attr; struct sched_param sp; int rc; rc=pthread_attr_init(&attr); checkRC("pthread_attr_init", rc); rc=pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); checkRC("pthread_attr_setinheritsched", rc); rc=pthread_attr_setschedpolicy(&attr, SCHED_RR); checkRC("pthread_attr_setschedpolicy", rc); memset(&sp, 0, sizeof(sp)); sp.sched_priority=50; rc=pthread_attr_setschedparam(&attr, &sp); checkRC("pthread_attr_setschedparam", rc); pthread_create(&thd, &attr, thdfunc, NULL); sleep(2); sp.sched_priority=30; printf("Main: setschedparam\n"); rc=pthread_setschedparam(thd, SCHED_FIFO, &sp); checkRC("pthread_setschedparam", rc); rc=pthread_join(thd, NULL); checkRC("pthread_join", rc); rc=pthread_attr_destroy(&attr); checkRC("pthread_attr_destroy", rc); printf("Main: completed\n"); return 0; } |
All times are GMT -5. The time now is 08:41 PM. |