09-07-2007
3,
0
Join Date: Mar 2007
Last Activity: 22 July 2008, 7:23 AM EDT
Posts: 3
Thanks Given: 0
Thanked 0 Times in 0 Posts
Schedule Real time threads
I want to set the priority and the scheduler as SCHED_FIFO for two threads . I want to see that the thread with high priority will run forever .
A simple code is given below . But both the threads are being scheduled . Why this is happening ?
I am executing the code as root.
I expect to see only the string P1 since the function doit1 is being executed by the thread with priority 35. But the string P2 is also being printed whose thread priority is 32.
#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
void *doit1(void *);
void *doit2(void *);
pthread_attr_t tattr1;
pthread_attr_t tattr2;
struct sched_param param1;
int main()
{
int i,a;
pthread_t th1,th2;
pthread_attr_init(&tattr1);
pthread_attr_init(&tattr2);
pthread_attr_setschedpolicy(&tattr1 , SCHED_FIFO);
pthread_attr_setschedpolicy(&tattr2 , SCHED_FIFO);
param1.sched_priority=35;
pthread_attr_setschedparam(&tattr1 , ¶m1);
param1.sched_priority=32;
pthread_attr_setschedparam(&tattr2 , ¶m1);
pthread_create(&th1,&tattr1,doit1,NULL);
pthread_create(&th2,&tattr2,doit2,NULL);
pthread_join(th1,NULL);
pthread_join(th2,NULL);
}
void * doit1(void *ptr)
{
while (1)
write(1,"P1",2);
return NULL;
}
void * doit2(void *ptr)
{
while (1)
write(1,"P2", 2);
return NULL;
} 54,1 68%