LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 01-11-2007, 03:01 AM   #1
jasdeep_js
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


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
 
Old 09-07-2008, 02:44 AM   #2
teamusa
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);
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;
}
 
  


Reply

Tags
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 11:54 AM
LXer: Real Time Coming to Linux Real Soon LXer Syndicated Linux News 1 08-23-2006 07:30 PM
Real-time Round-Robin Scheduling cttran Programming 2 05-06-2005 05:41 PM
Real Time Clock(RTC) problem gongcon Linux - Software 6 01-06-2005 10:36 PM
Pthread scheduling... msriram_linux Programming 3 11-21-2004 04:57 PM


All times are GMT -5. The time now is 02:38 PM.

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