From: "Steve Graegert" <graegerts@gmail.com>
To: Jonathan Walsh <jwalsh@atl.lmco.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Question about pthreads on Linux
Date: Tue, 25 Sep 2007 16:12:25 +0200 [thread overview]
Message-ID: <6a00c8d50709250712l676cf64ev94230be4b541a5d1@mail.gmail.com> (raw)
In-Reply-To: <46F9082C.1060804@atl.lmco.com>
Jonathan,
On 9/25/07, Jonathan Walsh <jwalsh@atl.lmco.com> wrote:
> I am attempting to set up a program that has 3 threads. The first
> thread is a receiver process that listens on a UDP port and puts a
> struct on a one of two queues depending on a flag in the packet. The
> other two threads block on a pthread_cond_t (that is signaled when a
> message is added to their queue), then take messages off their
> corresponding queues and perform a fixed amount of computation. I am
> setting thread priorities as follows:
>
> pthread_t tid_processor;
> struct sched_param sparam;
> pthread_attr_t tattr;
> pthread_attr_init(&tattr);
> pthread_attr_getschedparam(&tattr, &sparam);
> sparam.sched_priority = SOME_THREAD_PRIORITY;
> pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
> pthread_attr_setschedparam(&tattr, &sparam));
> pthread_create(&tid, &tattr, &thread_function, NULL );
>
> I set the receiver thread to the highest priority, then one of the
> processor threads is higher than the other.
>
> I have two questions about this situation:
>
> 1) Are pthread priorities reversed from Linux priorities. In other
> words, does a lower number mean a higher priority?
No. SCHED_FIFO or SCHED_RR have an allowed range of 1 to 99 with lower
numbers representing lower thread priorities.
> 2) Will a higher priority thread preempt the execution of a lower
> priority thread?
Normally, this is the true for SCHED_FIFO, where threads run until
preempted by a thread of higher priority, or until blocked.
However, Linux limits the scheduling policies SCHED_RR and SCHED_FIFO
to processes with superuser privileges. So, unless your program runs
with elevated privileges SCHED_OTHER will be selected by default which
is "based on the nice level ... and increased for each time quantum
the process [i.e. thread] is unable to run." according to
sched_setscheduler(2).
(Note, the pthread_set_schedparam() call maps to the
sched_setscheduler() call. An operation that requires root
privileges.)
For SCHED_OTHER the allowed mininmum and maximum priorities are 0.
Therefore it is not possible to change the priority.
Hope that sheds some light on your questions
\Steve
prev parent reply other threads:[~2007-09-25 14:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-25 13:07 Question about pthreads on Linux Jonathan Walsh
2007-09-25 14:12 ` Steve Graegert [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6a00c8d50709250712l676cf64ev94230be4b541a5d1@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=jwalsh@atl.lmco.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).