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, ¶m);
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, ¶m);
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;
}