LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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


Closed Thread
  Search this Thread
Old 04-28-2014, 10:04 PM   #1
Jia Hong
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Rep: Reputation: Disabled
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;
}
 
Old 04-29-2014, 01:26 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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/.
 
  


Closed Thread



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
Linux pthread priority real time scheduler (SCHED_RR) doesn't seem work properly Jia Hong Programming 4 04-30-2014 07:16 AM
Linux pthread priority real time scheduler (SCHED_RR) doesn't seem work properly Jia Hong Linux - Software 1 04-29-2014 01:26 AM
Kernel scheduler - Maximum Real time priority reg13 Linux - Kernel 7 07-27-2011 02:49 PM
Hard/Soft Real Time Scheduler for Linux 2.4 amit_bst Linux - General 1 05-25-2006 06:44 AM
Hard/Soft Real Time Scheduler for Linux 2.4 amit_bst Linux - General 3 05-25-2006 04:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:41 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
Open Source Consulting | Domain Registration