UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-07-15 01:11 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

hi,,

Assume i am running from user space a function or a system call (e.g. that will generate a signal, like SIGALRM) , how can i tell the kernel scheduler to give high priority to this function or to be executed first in order to not miss the deadlines ?? is there a system call or c-lib functions? any examples?

thanks

Offline

#2 2008-07-15 02:48 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: proccess prioritization !!

If you're working with strict deadlines you should run your applications with
real-time priority. So determine with real-time priorities when and which programs
run, and then it doesn't matter if you sent signals or do somethign else, and they'll
run when you want them. (Ignoring hardware limitations/interrupts and things like
that.)

Offline

#3 2008-07-15 02:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: proccess prioritization !!

Well, you can change the priority (and possibly scheduling algorithm) for the whole
process/thread, but not for just a single function...  You could get the same effect by
having the function save the current priority, change to the higher one, then restore
the old one when it returns...

See your local man pages for setpriority(), sched_setscheduler(), or if you're using
threads, pthread_setschedprio(), pthread_setschedparam()...

Offline

#4 2008-07-15 03:09 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

Offline

#5 2008-07-15 08:33 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: proccess prioritization !!

No...  Like I say, you can change the prio for the whole process or thread, but not
for individual functions or kernel-level code executing on your behalf...

If you want most of your code to run at lower priority, but this timer stuff to run higher
priority, then I suspect you need to go multi-threaded or multi-process...  (And,
mixing signals with threads is often a risky and error-prone idea, so your particular
case might not fit well for multi-threading...)  Ie: your high-prio thread/process would
just setup the timer, then sit in pause() or similar doing nothing until hit with the signal
and your signal handler called in its context (with its higher priority), while the rest of
your code goes on executing separately at its lower priority...

But, are you sure you need that level of complexity?  Is there some reason you
don't want the whole process executing at the higher priority?

Offline

#6 2008-07-15 11:32 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

Offline

#7 2008-07-16 01:02 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: proccess prioritization !!

Offline

#8 2008-07-16 01:18 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

#include <stdio.h>

int main(void)
{
	printf("Hello World");
	
	return 0;
}

Offline

#9 2008-07-16 01:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: proccess prioritization !!

Offline

#10 2008-07-16 01:59 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

Offline

#11 2008-07-16 02:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: proccess prioritization !!

No.

For that you want a hard realtime system, but that's totally ridiculous for a
printf() and similar slow non-critical things like that. If you use a RT kernel then
using setpriority() or sched_setscheduler() is probably enough. (e.g. on rt-linux the
interrupt handles are run as processes, so then everything uses the same priority
system, in which case the answer is yes.)

But if you go that insane path then you need to worry about a thousand other
things too, like your memory always being resident, getting enough disk IO,
and who knows what. But realtime doesn't mix well with internet stuff, as
internet is best effort, so that gives enough uncertainty to not bother with
the rest. If you have dedicated hardware and a special network (e.g. fibre) then
it's possible, but I don't think you know enough yet to make it really real-time.

So forget about not being interrupted, it's not worth it.

If you really need real-time priority then go buy and read a good book on the
topic, don't ask vague questions on forums, but go learn the real stuff.

Offline

#12 2008-07-16 08:32 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: proccess prioritization !!

Like i3839 says, if your timing precision is absolutely critical, then yeah you're going
to need a specialized real-time OS, at least...  But, most apps don't need such
absolutely critical timing guarantees, and will be fine with just the normal simple
priority system...  If you set a high enough priority (well, "low enough", depending on
your POV, since lower negative values typically represent conceptually "higher"
priorities), such that you're higher than any other running task on the system, then
you can be assured the OS should give you first shot at a timeslice whenever you
are ready to run...  Which should be good enough for most uses...  If you need to
guarantee you'll always IMMEDIATELY gain control of the CPU within microseconds
of the timer going off or something, then you aren't going to be able to do it on a
standard desktop/server type OS, I doubt...  But, ask yourself if you really need such
precision anyway...  Because, you've certainly not talked about doing anything that
sounds like it should need such exact critical timing, anyway...

Offline

#13 2008-07-17 09:19 AM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

Offline

#14 2008-07-17 11:56 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: proccess prioritization !!

I don't know one, or I would have told you about it, but I'm sure there are some good
ones out there.

Offline

#15 2008-07-17 12:03 PM

raedbenz
Member
Registered: 2008-07-11
Posts: 13

Re: proccess prioritization !!

anyway thank you

Offline

#16 2008-11-08 07:12 AM

mrshappy
Guest

Re: proccess prioritization !!

If you really need real-time priority then go buy and read a good book on the
topic, don't ask vague questions on forums, but go learn the real stuff.

Board footer

Powered by FluxBB