Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-19-2004, 07:57 AM   #1
LQ Newbie
Registered: Nov 2004
Posts: 19

Rep: Reputation: 0
POSIX thread Programming

I would like to know how to set priorities for threads...
In the program I have been experimenting on, I have three threads and I want to set thier pririties as I wish. Is this possible?..
Could someone please mail an example application where different scheduling policies and scheduling attributes become clear?..
Thank you
Old 11-19-2004, 08:18 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
Linux doesn't allow you to set a thread's scheduling priority directly; instead, you can set the niceness value, using the nice(2) function:

#include <unistd.h>
int nice(int inc); //returns 0 on success or -1 on error (sets errno)
Edit: In response to your email, it seems that some implementations return the actual nice value. From the manpage:
Note that the routine is documented in SUSv2 and POSIX 1003.1-2003 to return the new nice value,
while the Linux syscall and (g)libc (earlier than glibc 2.2.4) routines return 0 on success. The
new nice value can be found using getpriority(2). Note that an implementation in which nice returns
the new nice value can legitimately return -1. To reliably detect an error, set errno to 0 before
the call, and check its value when nice returns -1.
The nice values range from -19 (most foreground; highest priority) to +20 (most background; lowest priority). The initial default is usually 0.

Reducing the nice value is a privilaged instruction that requires superuser powers.

The getpriority(2) call returns the current nice value, and the nice and renice programs can be used to change niceness from the shell.

The actual scheduling priority is set depending on the nice value, recent schedular performance, and other system loads.

Last edited by rjlee; 11-21-2004 at 10:49 AM.
Old 11-19-2004, 08:24 AM   #3
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
you probably don't what to hack your kernel.
If you need to scedule your threads, what you need to do is to build a simple server,
who will be the sceduler. When a thread starts, make it connect to the server (through TCP if you want),
The severs may send simple messages to the threads, to block and unblock them.
So the sever is responsible for the sceduling, and the threads all they need to do is to listen on a socken. When they get a block message they stop and when they get a unblock message they continue doing what they do. When a thread finishes the work, it closes the connection, so that the server knows it. I had to do something like that for a project in the univercity, a few years ago. I don't remember all the details, but i think this is the basic consept. If i find the project i will e-mail it to you.
Old 11-19-2004, 09:23 AM   #4
Registered: Feb 2004
Location: Carpina, PE, Brazil
Distribution: Ubuntu 5.10
Posts: 44

Rep: Reputation: 15
Check these functions:


Poxis Thread Programming
Old 11-21-2004, 03:14 PM   #5
LQ Newbie
Registered: Nov 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Hi Lauro!
Thank you for the suggestion.
However my problem was solved with nice fuction suggested by rjlee.
I tried with pthread_setschedparam (in a different program)but I have some
problems reagrding which I have stated a new thread subjected "Pthread scheduling..". Could you please take pains to go through it?


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
POSIX Thread programming problem... rajsun Programming 0 10-07-2005 11:18 PM
POSIX Thread Termination confusion sleepymish Programming 2 12-05-2004 08:16 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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration