LinuxQuestions.org
Review your favorite Linux distribution.
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 08-30-2006, 11:43 PM   #1
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Rep: Reputation: 0
Thread Scheduling using POSIX THREAD


I want to know how to schedule threads using POSIX THREAD library.I created 4 threads and i want to schedule them by setting priorities.How can i do this.I don't have root privileges.so tell me how can i schedule using SCHED_OTHER shall i have to use any synchronizing mechanism.can anyone help me with sample code.

Thanks in advance

Last edited by rameshtekumudi; 08-30-2006 at 11:51 PM.
 
Old 08-31-2006, 09:32 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
The sched_param structure contains the scheduling parameters. The
scheduling parameters may only allow you to specify the priority of the thread. Scheduling Policy may also be restricted. Read your info pthread page.

pthread_attr_setschedparam(), pthread_attr_getschedparam(),
pthread_setschedparam(), or pthread_getschedparam() functions all play with scheduling parameters.

Scheduling Policy Information shows/sets what algorithm will be used to schedule threads Some scheduling policies might be Round Robin, FIFO. The SCHED_OTHER constant indicates the delay cost scheduling that the system uses. The scheduling parameter functions may only support one if they exist. See if you have the pthread_attr_getschedpolicy() and pthread_attr_setschedpolicy() functions supported. Some pthreads implementations may not have these. If you have only one policy then you can't play with it.

Plus, on threads questions - complete system info always helps you to get better answers - why my answer is full of qualifications and maybes.

Here is a tutorial with code examples:
http://yolinux.com/TUTORIALS/LinuxTu...ixThreads.html
 
Old 09-01-2006, 06:26 AM   #3
rameshtekumudi
LQ Newbie
 
Registered: Aug 2006
Location: Mumbai
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0
Thread Scheduling using POSIX THREAD

I have done whatever u mentioned. The code looks as follows.

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

void *tf1(void *arg);
void *tf2(void *arg);
void *tf3(void *arg);

char m1[] = " Thread1";
char m2[] = " Thread2";
char m3[] = " Thread3";

struct sched_param schedparam1,schedparam2,schedparam3;

int main() {

int res,max_priority,min_priority;
pthread_t t1,t2,t3;
pthread_attr_t at1,at2,at3;

schedparam1.sched_priority = 40;
schedparam2.sched_priority = 60;
schedparam3.sched_priority = 80;

/* Set attributes*/

pthread_attr_init(&at1);
pthread_attr_setinheritsched(&at1, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&at1, SCHED_OTHER);
pthread_attr_setschedparam(&at1, &schedparam1);

// I have done the same for other threads

max_priority=sched_get_priority_max(SCHED_OTHER);
min_priority=sched_get_priority_min(SCHED_OTHER);
printf(" MAX is %d and Min is %d\n",max_priority,min_priority);
printf("$$$$$$$$$$$$$$$$$$$$$$$$\n");

//Thread creatiom
res = pthread_create(&t1, &at1, tf1, (void *)m1);
res = pthread_create(&t2, &at2, tf2, (void *)m2);
res = pthread_create(&t3, &at3, tf3, (void *)m3);
}
void *tf1(void *arg) {
printf("tf1 is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(m1, "thread1 left");
pthread_exit("...........T1");
}
//tf2 tf3

Iam getting Max and Min priority as zero.How can i modify this to get the threads scheduled . How can I get the header file info about Pthread library.
 
Old 09-01-2006, 10:47 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Based on what you show, SCHED_OTHER is not implemented on your system.

I don't think it is a case of headers. Some distros do not have a full pthread implementation, especially older ones. You have to read your documentation for your distro. Go to the distribution website.
 
  


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
thread scheduling vkmgeek Programming 2 08-21-2006 10:29 AM
scheduling thread gives error bndpatel Programming 1 06-24-2005 01:54 PM
POSIX thread Ivan Lee Programming 2 03-28-2003 03:54 AM
RT Posix thread permissions spaceape Programming 0 01-23-2003 08:50 AM
posix thread sulo Programming 1 02-28-2002 07:41 PM

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

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