LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2014, 09:54 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

================================
Code:
#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;
}

Last edited by Jia Hong; 04-29-2014 at 10:21 PM. Reason: code is not readable
 
Old 04-29-2014, 02:14 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Try this and see how it might influence your understanding of what's going on
Code:
grep -i ^processor /proc/cpuinfo
(and in future use [code] tags so your posts are at least readable)
 
Old 04-29-2014, 10:06 PM   #3
Jia Hong
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for reply

I run the command and it reports two processors (0 and 1 ). Does scheduler place the two pthreads each on one of the two processors? What change should I make to the code so that high priority thread will block lower priority thread until high priority thread completes in multicore environment?

Code is reformatted using code tag. Please see original post above.

Last edited by Jia Hong; 04-29-2014 at 11:52 PM. Reason: Code is not readable
 
Old 04-30-2014, 01:59 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you will have to prioritise yourself I should imagine.
if both CPUs are available then obviously both threads can be running.

even better, keep away from threads and use simpler design unless absolutely necessary
 
Old 04-30-2014, 07:16 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Indeed, this is a flawed premise. The mere fact that one thread is running does not mean that another thread is not or should not. If you want to impose any sort of synchronization upon any two threads, then you must impose it, using mutexes or other appropriate synchronization objects. A process or thread may make no assumptions whatsoever as to what else is going on.
 
  


Reply



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
Kernel scheduler - Maximum Real time priority reg13 Linux - Kernel 7 07-27-2011 02:49 PM
problem in assigning real time scheduling to pthread in linux jasdeep_js Linux - Kernel 1 09-07-2008 01:44 AM
LXer: Linux 2.6 scheduler improves JVM, SMP, real-time performance LXer Syndicated Linux News 0 07-06-2006 09:03 AM
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 > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 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