set priority of thread

Hello,

I tried to set the priority of different threads in different processes to a
specified value by interpreting the argv and argc values from the command
line. I used setprio() from sched.h and it produces two problems:

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my servers
    change according to the client priorities - inheritance ). But the second
    time I start the programs: QNX hangs. Is it possible that the setprio()
    function is not supported for newer QNX versions anymore? How do I correctly
    handle the task of setting my own thread priority?

  2. I’d like to change the server priority back to its initial value after
    the client’s request is replied (I want to process some data at server
    priority before going RECEIVE BLOCKED again). How do I do it in C?

Thank you.

Nnamdi

I can answer #2. The following bit of code is what I do after handling a
client message

// the receiver floats to the priority of the sender.
// must reset the thread priority
setprio(0,sched_get_priority_max(SCHED_FIFO) - MSGHANDLER_PRI);

It seems to work reliably in QNX 6.1.

Regards,
David Kuechenmeister

“Nnamdi Kohn” <nnamdi.kohn@tu-bs.de> wrote in message
news:b6jka7$dgg$1@inn.qnx.com

Hello,

I tried to set the priority of different threads in different processes to
a
specified value by interpreting the argv and argc values from the command
line. I used setprio() from sched.h and it produces two problems:

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my
    servers
    change according to the client priorities - inheritance ). But the second
    time I start the programs: QNX hangs. Is it possible that the setprio()
    function is not supported for newer QNX versions anymore? How do I
    correctly
    handle the task of setting my own thread priority?

  2. I’d like to change the server priority back to its initial value after
    the client’s request is replied (I want to process some data at server
    priority before going RECEIVE BLOCKED again). How do I do it in C?

Thank you.

Nnamdi

Nnamdi Kohn <nnamdi.kohn@tu-bs.de> wrote:

Hello,

I tried to set the priority of different threads in different processes to a
specified value by interpreting the argv and argc values from the command
line. I used setprio() from sched.h and it produces two problems:

First problem – setprio() works on processes, if a process has more than
one thread, then setprio()'s behaviour is undefined. (POSIX). (QNX will,
in fact, apply that priority to the first thread in the process.)

Even though QNX does apply a meaning to a setprio() on a multi-threaded
process, making such a use of it is not portable. If you need to change
the priority of a particular thread, this can be done by
pthread_setschedparam(), and can only be done by a thread (same or
another) in the same process.

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my servers
    change according to the client priorities - inheritance ). But the second
    time I start the programs: QNX hangs. Is it possible that the setprio()
    function is not supported for newer QNX versions anymore? How do I correctly
    handle the task of setting my own thread priority?

setprio() is supported.

When you say “QNX hangs” what do you mean? Are you running in text mode
or Photon? Or, are you running on a target with some sort of embedded
image? If in text mode, do you see a kernel dump? If you have a keyboard
attached, and you hit the caps lock or num lock key, does the light on the
keyboard toggle?

There should be nothing in setprio() that will actually crash QNX – but
if you are playing with priorities, and you push the priority of something
that will use 100% of the CPU above the priority of your user interface,
then that program will not give you US any CPU time, and it will appear
that QNX is hung. In fact, QNX is doing exactly what it is supposed to
do, it is giving the high priority task all the CPU it asks for, (fully
pre-emptive scheduling), and QNX is not hung.

  1. I’d like to change the server priority back to its initial value after
    the client’s request is replied (I want to process some data at server
    priority before going RECEIVE BLOCKED again). How do I do it in C?

If it is a multi-threaded server, or even if singled threaded, you can
call pthread_setscheduler( pthread_self(), …).

Only if single-threaded, you can use sched_setparam( getpid(), …) to
change the priority of your “process” – in fact, the first thread in
that process. (There are also (non-portable) convenience functions
getprio() and setprio() that set the priority of a “process” without
the annoyance of having to deal with struct sched_params and
scheduling policies.)

[My statement that you can’t change the priority of a thread in
another process is a small lie. In fact, the underlying kernel
call SchedSet() takes a pid and tid, allowing you to change the
priority of any thread on the system – but this is a highly
non-portable route to go, and also breaks the process model
by (effectively) accessing the private member (thread) of an
object (process). I recommend not using SchedSet() directly.]

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Hello David, thanks for the help so far.

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my
    servers
    change according to the client priorities - inheritance ). But the
    second
    time I start the programs: QNX hangs. Is it possible that the setprio()
    function is not supported for newer QNX versions anymore? How do I
    correctly
    handle the task of setting my own thread priority?

setprio() is supported.

When you say “QNX hangs” what do you mean? Are you running in text mode
or Photon? Or, are you running on a target with some sort of embedded
image? If in text mode, do you see a kernel dump? If you have a keyboard
attached, and you hit the caps lock or num lock key, does the light on the
keyboard toggle?

QNX hangs means that I can not even move the mouse anymore. I’m running
Photon and the processes I start in a terminal window. Pressing the NUM LOCK
key in the “hang” toggles the keyboard led, but CAPS LOCK changes nothing.
So I reboot the system.

But I also recognized an other oddity. Let me describe it:

At first I start a server “process1” (I set the priority to 12r and then
create a second thread at 8r - this works fine). Process1 does a
MsgReceive(). Then I start a second “process2” (just one threaded) and set
its priority to 10r (which it would be anyway if I did nothing). Now
process2 does a MsgSend() to process1. This causes thread1 of Process1 to
jump to priority 10r and handle the request. After leaving process2 (with
CTRL-C) process1 suddenly jumps to priority 20r.

When I start process2 again with the same priority as before (10r), the
whole computer becomes incredibly slow (might be connected to the mutexes
and condvars I use within process2) but communication with process1 works
fine as before.

Now I leave process2 (CTRL-C) and start it again at priority level of e.g.
14r. Now it does not even start but QNX hangs as described above. I don’t
even know if it has to do with priorities or rather with the mutexes or
condvars that mysteriously slow down the whole system after a second start.

How could I go on checking for the source of the error?

Nnamdi

Nnamdi Kohn <nnamdi.kohn@tu-bs.de> wrote:

Hello David, thanks for the help so far.

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my
    servers
    change according to the client priorities - inheritance ). But the
    second
    time I start the programs: QNX hangs. Is it possible that the setprio()
    function is not supported for newer QNX versions anymore? How do I
    correctly
    handle the task of setting my own thread priority?

setprio() is supported.

When you say “QNX hangs” what do you mean? Are you running in text mode
or Photon? Or, are you running on a target with some sort of embedded
image? If in text mode, do you see a kernel dump? If you have a keyboard
attached, and you hit the caps lock or num lock key, does the light on the
keyboard toggle?

QNX hangs means that I can not even move the mouse anymore. I’m running
Photon and the processes I start in a terminal window. Pressing the NUM LOCK
key in the “hang” toggles the keyboard led, but CAPS LOCK changes nothing.
So I reboot the system.

That NUM_LOCK toggles the keyboard led suggests that the system is still
operating. Also your later description of priorities bouncing around
also suggests that you are not hanging the system – you are just
pre-empting the user interface, including the updates for moving the
mouse pointer around. If you take a look at the output from ‘pidin’,
you will notice that most of the Photon components run in the priority
range of 10-15, if anything runs higher than that, nothing will get updated
on the screen.


But I also recognized an other oddity. Let me describe it:

At first I start a server “process1” (I set the priority to 12r and then
create a second thread at 8r - this works fine). Process1 does a
MsgReceive().

Which thread in Process1? Once you have more than one thread in a
process you can no longer usefully say that the process does something,
you must talk about what each thread does.

Then I start a second “process2” (just one threaded) and set
its priority to 10r (which it would be anyway if I did nothing). Now
process2 does a MsgSend() to process1. This causes thread1 of Process1 to
jump to priority 10r and handle the request. After leaving process2 (with
CTRL-C) process1 suddenly jumps to priority 20r.

Ctrl-C causes a signal to be delivered to process2. This carries no
priority information. The death, though, of process2 may pass some
information to process1 – how did you create the channel for process1
and if done with ChannelCreate() what channel flags did you pass?

When I start process2 again with the same priority as before (10r), the
whole computer becomes incredibly slow (might be connected to the mutexes
and condvars I use within process2) but communication with process1 works
fine as before.

If process2 is single-threaded, why does it have mutexes and condvars?

Again, the “becomes incredibly slow” sure sounds like you have something
chewing up a lot of the CPU time – something is probably spinning at
priority 10 (since the UI is slow, but still acting).

Is the CPU monitor visible on your shelf? Is it pegged at this point?

Now I leave process2 (CTRL-C) and start it again at priority level of e.g.
14r. Now it does not even start but QNX hangs as described above. I don’t
even know if it has to do with priorities or rather with the mutexes or
condvars that mysteriously slow down the whole system after a second start.

How could I go on checking for the source of the error?

Well, do the testing in console, rather than GUI mode, and have high-priority
shell open to do pidin(s) to check what is happening, and what state various
threads are in – anything READY or RUNNING all the time is highly
likely to be the cause of this, or should I say, the highest priority
usually READY thread is the likely cause of this.

Use qconn and the system information perspective in the IDE accross a
network, with qconn running at a high priority (say 60) to watch the
system and do similar data collection to the above.

Install the instrumented kernel, use tracelogger to collect system events,
and use the System Profiling perspective of the IDE to analyse what is
going on.

Run your code under the profiler in the IDE and see where you are chewing
up huge amounts of CPU.

Examine your code, and look for places where you loop, or might chew up
large amounts of CPU.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Hello David,

thanks for your efforts and your help. The problem was with the mutex and
condvars I initialized in an improper way. This caused the system to show
unexpected behavior. The priority can be adjusted fine now, without handing
the system.

Nnamdi

“David Gibbs” <dagibbs@qnx.com> schrieb im Newsbeitrag
news:b6kv1r$dni$1@nntp.qnx.com

Nnamdi Kohn <> nnamdi.kohn@tu-bs.de> > wrote:
Hello David, thanks for the help so far.

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my
    servers
    change according to the client priorities - inheritance ). But the
    second
    time I start the programs: QNX hangs. Is it possible that the
    setprio()
    function is not supported for newer QNX versions anymore? How do I
    correctly
    handle the task of setting my own thread priority?

setprio() is supported.

When you say “QNX hangs” what do you mean? Are you running in text
mode
or Photon? Or, are you running on a target with some sort of embedded
image? If in text mode, do you see a kernel dump? If you have a
keyboard
attached, and you hit the caps lock or num lock key, does the light on
the
keyboard toggle?

QNX hangs means that I can not even move the mouse anymore. I’m running
Photon and the processes I start in a terminal window. Pressing the NUM
LOCK
key in the “hang” toggles the keyboard led, but CAPS LOCK changes
nothing.
So I reboot the system.

That NUM_LOCK toggles the keyboard led suggests that the system is still
operating. Also your later description of priorities bouncing around
also suggests that you are not hanging the system – you are just
pre-empting the user interface, including the updates for moving the
mouse pointer around. If you take a look at the output from ‘pidin’,
you will notice that most of the Photon components run in the priority
range of 10-15, if anything runs higher than that, nothing will get
updated
on the screen.


But I also recognized an other oddity. Let me describe it:

At first I start a server “process1” (I set the priority to 12r and then
create a second thread at 8r - this works fine). Process1 does a
MsgReceive().

Which thread in Process1? Once you have more than one thread in a
process you can no longer usefully say that the process does something,
you must talk about what each thread does.

Then I start a second “process2” (just one threaded) and set
its priority to 10r (which it would be anyway if I did nothing). Now
process2 does a MsgSend() to process1. This causes thread1 of Process1
to
jump to priority 10r and handle the request. After leaving process2
(with
CTRL-C) process1 suddenly jumps to priority 20r.

Ctrl-C causes a signal to be delivered to process2. This carries no
priority information. The death, though, of process2 may pass some
information to process1 – how did you create the channel for process1
and if done with ChannelCreate() what channel flags did you pass?

When I start process2 again with the same priority as before (10r), the
whole computer becomes incredibly slow (might be connected to the
mutexes
and condvars I use within process2) but communication with process1
works
fine as before.

If process2 is single-threaded, why does it have mutexes and condvars?

Again, the “becomes incredibly slow” sure sounds like you have something
chewing up a lot of the CPU time – something is probably spinning at
priority 10 (since the UI is slow, but still acting).

Is the CPU monitor visible on your shelf? Is it pegged at this point?

Now I leave process2 (CTRL-C) and start it again at priority level of
e.g.
14r. Now it does not even start but QNX hangs as described above. I
don’t
even know if it has to do with priorities or rather with the mutexes or
condvars that mysteriously slow down the whole system after a second
start.

How could I go on checking for the source of the error?

Well, do the testing in console, rather than GUI mode, and have
high-priority
shell open to do pidin(s) to check what is happening, and what state
various
threads are in – anything READY or RUNNING all the time is highly
likely to be the cause of this, or should I say, the highest priority
usually READY thread is the likely cause of this.

Use qconn and the system information perspective in the IDE accross a
network, with qconn running at a high priority (say 60) to watch the
system and do similar data collection to the above.

Install the instrumented kernel, use tracelogger to collect system events,
and use the System Profiling perspective of the IDE to analyse what is
going on.

Run your code under the profiler in the IDE and see where you are chewing
up huge amounts of CPU.

Examine your code, and look for places where you loop, or might chew up
large amounts of CPU.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Nnamdi Kohn <nnamdi.kohn@tu-bs.de> wrote:

Hello David,

thanks for your efforts and your help. The problem was with the mutex and
condvars I initialized in an improper way. This caused the system to show
unexpected behavior. The priority can be adjusted fine now, without handing
the system.

Ah, you weren’t blocking on the mutexes or condvars, so you were running
READY, which is what I thought was happening – I just didn’t know the
reason why.

-David

Nnamdi

“David Gibbs” <> dagibbs@qnx.com> > schrieb im Newsbeitrag
news:b6kv1r$dni$> 1@nntp.qnx.com> …
Nnamdi Kohn <> nnamdi.kohn@tu-bs.de> > wrote:
Hello David, thanks for the help so far.

  1. the first time I start my programs everything runs fine and I can
    exchange messages as usual between the threads (the priorities of my
    servers
    change according to the client priorities - inheritance ). But the
    second
    time I start the programs: QNX hangs. Is it possible that the
    setprio()
    function is not supported for newer QNX versions anymore? How do I
    correctly
    handle the task of setting my own thread priority?

setprio() is supported.

When you say “QNX hangs” what do you mean? Are you running in text
mode
or Photon? Or, are you running on a target with some sort of embedded
image? If in text mode, do you see a kernel dump? If you have a
keyboard
attached, and you hit the caps lock or num lock key, does the light on
the
keyboard toggle?

QNX hangs means that I can not even move the mouse anymore. I’m running
Photon and the processes I start in a terminal window. Pressing the NUM
LOCK
key in the “hang” toggles the keyboard led, but CAPS LOCK changes
nothing.
So I reboot the system.

That NUM_LOCK toggles the keyboard led suggests that the system is still
operating. Also your later description of priorities bouncing around
also suggests that you are not hanging the system – you are just
pre-empting the user interface, including the updates for moving the
mouse pointer around. If you take a look at the output from ‘pidin’,
you will notice that most of the Photon components run in the priority
range of 10-15, if anything runs higher than that, nothing will get
updated
on the screen.


But I also recognized an other oddity. Let me describe it:

At first I start a server “process1” (I set the priority to 12r and then
create a second thread at 8r - this works fine). Process1 does a
MsgReceive().

Which thread in Process1? Once you have more than one thread in a
process you can no longer usefully say that the process does something,
you must talk about what each thread does.

Then I start a second “process2” (just one threaded) and set
its priority to 10r (which it would be anyway if I did nothing). Now
process2 does a MsgSend() to process1. This causes thread1 of Process1
to
jump to priority 10r and handle the request. After leaving process2
(with
CTRL-C) process1 suddenly jumps to priority 20r.

Ctrl-C causes a signal to be delivered to process2. This carries no
priority information. The death, though, of process2 may pass some
information to process1 – how did you create the channel for process1
and if done with ChannelCreate() what channel flags did you pass?

When I start process2 again with the same priority as before (10r), the
whole computer becomes incredibly slow (might be connected to the
mutexes
and condvars I use within process2) but communication with process1
works
fine as before.

If process2 is single-threaded, why does it have mutexes and condvars?

Again, the “becomes incredibly slow” sure sounds like you have something
chewing up a lot of the CPU time – something is probably spinning at
priority 10 (since the UI is slow, but still acting).

Is the CPU monitor visible on your shelf? Is it pegged at this point?

Now I leave process2 (CTRL-C) and start it again at priority level of
e.g.
14r. Now it does not even start but QNX hangs as described above. I
don’t
even know if it has to do with priorities or rather with the mutexes or
condvars that mysteriously slow down the whole system after a second
start.

How could I go on checking for the source of the error?

Well, do the testing in console, rather than GUI mode, and have
high-priority
shell open to do pidin(s) to check what is happening, and what state
various
threads are in – anything READY or RUNNING all the time is highly
likely to be the cause of this, or should I say, the highest priority
usually READY thread is the likely cause of this.

Use qconn and the system information perspective in the IDE accross a
network, with qconn running at a high priority (say 60) to watch the
system and do similar data collection to the above.

Install the instrumented kernel, use tracelogger to collect system events,
and use the System Profiling perspective of the IDE to analyse what is
going on.

Run your code under the profiler in the IDE and see where you are chewing
up huge amounts of CPU.

Examine your code, and look for places where you loop, or might chew up
large amounts of CPU.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.