How do I execute a function once every 1000 milliseconds using alarm() or sleep? I want the program to do something else if the function does not execute or complete in 1000 milliseconds.
EDIT: added Pseudocode
while(true) {
alarm(1000);
execute function;
sleep(1000);
alarm(0);
}
Now if alarm(1000) signals SIGALRM is this where I could call the other function?
I’m new to this sort of stuff so not even sure if I am using it right.
How crisp is the requirement, ie, how much jitter can you tolerate?
What version of UNIX?
Basically, if this is a hard deadline — it sounds like one — you’re going to need to do some special stuff, because basic UNIX isn’t really a hard-real-time system.
Let’s assume for the moment that you mean Linux. You’ll want to
That will probably do. If you need finer grained, or more predictable, timing, then you probably need to write a kernel extension to it can be driven with a kernel timer. (Solaris has some improved support for hard-real-time, but it’s still not really a hard real-time system.)
Life will get considerably easier if you can use a real-time Linux or UNIX. Here’s a list of some options. Here’s an article you might find usefui.
Update
You should also look at nanosleep(2) or setitimer(2). Notice that all of these say the interval is at least the one in the argument. If you have a hard deadline, you need to wait for somewhat less than the actual interval and then figure out what to do with any change on your thousand millisecs.