LinuxQuestions.org
Help answer threads with 0 replies.
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 09-08-2011, 10:02 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
pthread multithreading with priority


Hi,

I'm trying to run a multi-threading process (under my ubuntu) with different priorities but can't make it to work. The program is:

Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pthread_t task_2_id;

void* task_2(void* arg)
{
    int type;
    struct sched_param sch_params;
    pthread_getschedparam(task_2_id, &type, &sch_params);
    while(1)
    {
        printf("TASK 2 priority: %d\n", sch_params.sched_priority);
        sleep(1);
    }
}

int main(void)
{
    int type;
    int status;
    pthread_attr_t attr;
    struct sched_param sch_params;

    pthread_attr_init(&attr);
    pthread_attr_getschedparam(&attr, &sch_params);
    sch_params.sched_priority = 100;
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    pthread_attr_setschedparam(&attr,&sch_params);

    /* Main task with priority */
    status = pthread_create(&task_2_id,
                            &attr,
                            task_2,
                            NULL);

    printf("Status: %d\n", status);

    pthread_getschedparam(pthread_self(), &type, &sch_params);

    while(1)
    {
        printf("Maint task priority: %d\n", sch_params.sched_priority);
        sleep(1);
    }

    exit(0);
}
But at my out in the console is:
Code:
$ ./my_app 
Status: 0
Maint task priority: 0
TASK 2 priority: 0
Maint task priority: 0
TASK 2 priority: 0
Maint task priority: 0
TASK 2 priority: 0
I thought I could set a priority (100-140) to a task under linux. Or Am I wrong? Or simply, is there is something wrong with my code?

Thanks in advance,
Indy
 
Old 09-08-2011, 11:09 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Check the value that is returned by these two functions, and report back whether they indicate the call was successful.
Code:
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
pthread_attr_setschedparam(&attr,&sch_params);
 
Old 09-09-2011, 05:03 AM   #3
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Sorry. Silly of me. I should have checked.
There were problems setting schedule parameters. So I changed the code to the following in main:

Code:
printf("Priority (min-max): %d-%d\n",
           sched_get_priority_min(sch_type),
           sched_get_priority_max(sch_type));


    pthread_attr_init(&attr);
    status = pthread_attr_getschedparam(&attr, &sch_params);
    printf("getschedparam status: %d\n", status);
    sch_params.sched_priority = 50;
    status = pthread_attr_setschedpolicy(&attr, SCHED_RR);
    printf("setschedpolicy status: %d\n", status);
    status = pthread_attr_setschedparam(&attr,&sch_params);
    printf("setschedparam status: %d\n", status);

    /* Main task with priority */
    status = pthread_create(&task_2_id,
                            &attr,
                            task_2,
                            NULL);
    printf("pthread_create status: %d\n", status);
And the output is:
Code:
$ ./my_app 
Priority (min-max): 1-99
getschedparam status: 0
setschedpolicy status: 0
setschedparam status: 0
pthread_create status: 0
Maint task priority: 0
TASK 2 priority: 0
Maint task priority: 0
TASK 2 priority: 0
Maint task priority: 0
TASK 2 priority: 0
So even though my priority is 50 (between 1-99), I still don't get the priority I want. One thing is concerning me is that I'm running latest Ubuntu without real-time kernel patch. Does that have any affect? I have also tried to run the binary as normal user and sudo but without any success, i.e same result.

Thanks again,
Indy
 
Old 09-09-2011, 05:11 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Check this old post:
http://www.linuxquestions.org/questi...hreads-459117/

Regards,
archieval
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
X: warning; priority set to -1 instead of requested priority 0 HitmanX Linux - Newbie 5 12-13-2010 11:09 AM
Implementing priority based preemtive pthread bhatta_sobs Linux - Kernel 0 05-28-2010 05:46 AM
libtest.a uses pthread: user of libtest.a should not link pthread again debulu Programming 2 01-31-2007 09:23 PM
process priority,nice -- small question regarding high/low priority values beeblequix Linux - Newbie 1 10-11-2006 10:22 AM
Multithreading jayashrik Programming 1 07-20-2005 04:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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