LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Linux pthread priority real time scheduler (SCHED_RR) doesn't seem work properly (https://www.linuxquestions.org/questions/linux-software-2/linux-pthread-priority-real-time-scheduler-sched_rr-doesnt-seem-work-properly-4175503302/)

Jia Hong 04-28-2014 10:01 PM

Linux pthread priority real time scheduler (SCHED_RR) doesn't seem work properly
 
Platform: HP dc7900 (intel), Debian distribution (2.6 kernel)
Problem: Create two pthreads with priority 40 and 60. Respective counter (count0 or count 1) gets incrtemented when it is scheduled to run. Main routine prints the two counters every second. Unexpected behaviour is that counter for the first pthread becomes even bigger. Expected behaviour is that the first pthread (pri 40) should be completely blocked by the second pthread (pri 60) since it is lower priority and second thread doesn't block by itself. Need help to understand what is going on.

Code and output are as follows:

===============================

Thread created successfully

First thread processing

Second thread processing

Thread created successfully
223891966 11374955
434026187 22372766
643700229 34908176
783430529 43956430
993360049 55111456
1203710619 65921699

================================

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int pid[ 2 ] = { 1, 2 };
int count0, count1;

void* doSomeThing(void *arg)
{
unsigned long i = 0;

if( *((int *)arg) == 1 )
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}

while(1)
{
switch( *((int *)arg) )
{
case 1:
++count0;
break;
case 2:
++count1;
break;
default:
break;
}
}
return NULL;
}

int main(void)
{
int i = 0;
int err;
struct sched_param param;
pthread_attr_t attr;

while(i < 2)
{
if( i == 0 )
{
param.sched_priority = 40;
err = pthread_attr_init(&attr);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setschedpolicy(&attr,SCHED_RR);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setschedparam(&attr, &param);
if( err )
{
printf("error\n");
return 0;
}
}
else
{
param.sched_priority = 60;
err = pthread_attr_init(&attr);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setschedpolicy(&attr,SCHED_RR);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
if( err )
{
printf("error\n");
return 0;
}
err = pthread_attr_setschedparam(&attr, &param);
if( err )
{
printf("error\n");
return 0;
}

}
err = pthread_create(&(tid[i]), &attr, doSomeThing, &pid[ i ]);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");

i++;
}
for(;;)
{
printf("%d %d \n", count0, count1);
sleep(1);
}
pthread_join(tid[0], 0);
pthread_join(tid[1], 0);
return 0;
}

unSpawn 04-29-2014 01:26 AM

Please post your thread once and in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread should be closed because it is a duplicate of https://www.linuxquestions.org/quest...ly-4175503300/.


All times are GMT -5. The time now is 12:44 PM.