Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 01-11-2007, 02:01 AM   #1
LQ Newbie
Registered: Sep 2005
Location: Bangalore (INDIA)
Distribution: Redhat9
Posts: 10

Rep: Reputation: 0
problem in assigning real time scheduling to pthread in linux

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 :


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"))),


pthread_t m_thread;

int main(void)

pthread_attr_t thread_attr;
struct sched_param thread_param;

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);


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?

Old 09-07-2008, 01:44 AM   #2
LQ Newbie
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
Lightbulb 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);

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);
if (++cnt==5) break;
return NULL;

int main(void)
pthread_t thd;
pthread_attr_t attr;
struct sched_param sp;
int rc;

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));
rc=pthread_attr_setschedparam(&attr, &sp);
checkRC("pthread_attr_setschedparam", rc);
pthread_create(&thd, &attr, thdfunc, NULL);

printf("Main: setschedparam\n");
rc=pthread_setschedparam(thd, SCHED_FIFO, &sp);
checkRC("pthread_setschedparam", rc);

rc=pthread_join(thd, NULL);
checkRC("pthread_join", rc);
checkRC("pthread_attr_destroy", rc);
printf("Main: completed\n");
return 0;


pthread, scheduling

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Real-time Linux gains real-time JVM LXer Syndicated Linux News 0 10-12-2006 10:54 AM
LXer: Real Time Coming to Linux Real Soon LXer Syndicated Linux News 1 08-23-2006 06:30 PM
Real-time Round-Robin Scheduling cttran Programming 2 05-06-2005 04:41 PM
Real Time Clock(RTC) problem gongcon Linux - Software 6 01-06-2005 09:36 PM
Pthread scheduling... msriram_linux Programming 3 11-21-2004 03:57 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration