LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-28-2006, 09:08 AM   #1
gs_martin
LQ Newbie
 
Registered: Jun 2006
Posts: 1

Rep: Reputation: 0
Priority Scheduling with Pthreads


In order to demonstrate priority scheduling with Pthreads, I have written the following short c- program. I think its Output isn't very demonstrative because there is no descending order in the threads priorities.

>>> Program-Output
6'er Gruppe an der Barriere synchronisiert
Thread (1077267376) running at FIFO with priority 2
Thread (1087773616) running at FIFO with priority 4
Thread (1081469872) running at FIFO with priority 4
Thread (1085672368) running at FIFO with priority 3
Thread (1079368624) running at FIFO with priority 3
Thread (1083571120) running at FIFO with priority 2
6'er Gruppe an der Barriere synchronisiert
Thread (1083571120) running at FIFO with priority 2
Thread (1087773616) running at FIFO with priority 4
Thread (1081469872) running at FIFO with priority 4
Thread (1085672368) running at FIFO with priority 3
Thread (1079368624) running at FIFO with priority 3
Thread (1077267376) running at FIFO with priority 2
...
>>>

Instead the order 2 4 4 3 3 2, I would expect an priority order of 4 4 3 3 2 2. Does anyone know what is the problem here?

#define PRIO_GROUP 2

pthread_t hpt[PRIO_GROUP];
pthread_t mpt[PRIO_GROUP];
pthread_t npt[PRIO_GROUP];

barrier_t mybarrier;

void *highprior_thread(void *arg){
pthread_t thread_id = pthread_self();

while(1){
barrier_wait(&mybarrier);
print_scheduling_info(thread_id);
}

return NULL;
}

void *middleprior_thread(void *arg){
pthread_t thread_id = pthread_self();

while(1){
barrier_wait(&mybarrier);
print_scheduling_info(thread_id);
}

return NULL;
}

void *lowprior_thread(void *arg){
pthread_t thread_id = pthread_self();
int count = 0;

while(1){
barrier_wait(&mybarrier);
print_scheduling_info(thread_id);
}

return NULL;
}

int main(int argc, char *argv[]){
struct sched_param my_param;
pthread_attr_t hp_attr;
pthread_attr_t mp_attr;
pthread_attr_t lp_attr;
int i;

/* MAIN-THREAD WITH LOW PRIORITY */
my_param.sched_priority = sched_get_priority_min(SCHED_FIFO);
pthread_setschedparam(pthread_self(), SCHED_RR, &my_param);
print_scheduling_info(pthread_self());


/* SCHEDULING POLICY AND PRIORITY FOR OTHER THREADS */
pthread_attr_init(&lp_attr);
pthread_attr_init(&mp_attr);
pthread_attr_init(&hp_attr);

pthread_attr_setinheritsched(&lp_attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&mp_attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&hp_attr, PTHREAD_EXPLICIT_SCHED);

pthread_attr_setschedpolicy(&lp_attr, SCHED_FIFO);
pthread_attr_setschedpolicy(&mp_attr, SCHED_FIFO);
pthread_attr_setschedpolicy(&hp_attr, SCHED_FIFO);

my_param.sched_priority = min_priority + 1;
pthread_attr_setschedparam(&np_attr, &my_param);
my_param.sched_priority = min_priority + 2;
pthread_attr_setschedparam(&mp_attr, &my_param);
my_param.sched_priority = min_priority + 3;
pthread_attr_setschedparam(&hp_attr, &my_param);

barrier_init(&mybarrier, PRIO_GROUP*3);

for(i=0; i<PRIO_GROUP; i++){
pthread_create(&lpt[i], &np_attr, lowprior_thread, NULL);
pthread_create(&mpt[i], &mp_attr, middleprior_thread, NULL);
pthread_create(&hpt[i], &hp_attr, highprior_thread, NULL);
}

for(i=0; i<PRIO_GROUP; i++){
pthread_join(hpt[i], NULL);
pthread_join(mpt[i], NULL);
pthread_join(lpt[i], NULL);
}

printf("main exiting\n");

return 0;
}
 
  


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
pthreads ftgow Linux - Software 0 07-08-2004 03:55 AM
Scheduling priority for the 'nice' command savoiu Linux - Newbie 2 05-18-2004 11:35 AM
pthreads socket9001 Programming 2 12-29-2003 12:23 AM
pthreads Keith Hampton Linux - Software 1 10-23-2003 01:46 PM
window Nt assigns priority internally or by user?Which type of scheduling algorithm u seema General 0 09-26-2003 01:10 AM

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

All times are GMT -5. The time now is 04:07 AM.

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