PDA

View Full Version : Using alarm() and other signals.



ArcadeEdge
05-04-2012, 10:57 PM
So I'm having a hard time trying to figure out how alarm() works.
Let's say I wanted to make a distance meter that decrements every 1 second and every 3 seconds an update will be printed out.

My problem is that the the distance never changes. I'm not sure if I'm supposed to be passing distance to update. And I'm not sure what signum is actually doing. If any light can be shed, it'd be very much appreciated.



#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int distance = 60;

void meter(int signum) {
distance = distance - 1;
alarm(1);
}

void update(int signum) {
printf("\nYou have %d feet left!\n", distance);
alarm(3);
}


int main(void) {

signal(SIGALRM, meter);
signal(SIGALRM, update);
alarm(1);

getchar();

return 0;
}

Salem
05-04-2012, 11:33 PM
Did you read the manual pages?


DESCRIPTION
alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds.

If seconds is zero, no new alarm() is scheduled.

In any event any previously set alarm() is canceled.


Plus, signal() can only attach ONE function to each signal.

ArcadeEdge
05-05-2012, 12:06 AM
Yes I did and I've looked at several examples but it just didn't seem to clear to me. What I got from my search is that alarm() will decrement and then send a signal to do whatever the function says.
I don't feel like switching operating systems to test all this out right now since it's like 2am, but earlier I did try putting the two in one function and that did seem to do what I wanted. But can I have something like this



distance = distance - 1;
alarm(1); //would be better to just use sleep() here? can you use alarm twice like this?
printf("print distance");
alarm(3);

anduril462
05-05-2012, 05:13 PM
Yes I did and I've looked at several examples but it just didn't seem to clear to me. What I got from my search is that alarm() will decrement and then send a signal to do whatever the function says.
I don't feel like switching operating systems to test all this out right now since it's like 2am, but earlier I did try putting the two in one function and that did seem to do what I wanted. But can I have something like this
Whether to use alarm or sleep depends on whether you want to do other stuff in the mean time or just wait. Alarm lets you do other stuff, sleep makes your program wait. However, read the man pages for both functions, they don't play nicely together.


distance = distance - 1;
alarm(1); //would be better to just use sleep() here? can you use alarm twice like this?
printf("print distance");
alarm(3);

Signals are asynchronous. That means they don't happen in the normal flow of code, they interrupt it. That's why, for example, you never actually call the signal handler functions you set up. They simply get called when your program receives a signal. In your example, the alarm(1) call would schedule the alarm signal to be delivered to your program in 1 second, then immediately go to the printf, print that out, and go to the alarm(3) call, which probably takes a few tens of nanoseconds on a modern cpu. The alarm(3) call would cancel your old alarm which was set to go off in 1s, and reschedule a new alarm to go off in 3s. Then it would return and go on to any instructions after that.

brewbuck
05-14-2012, 05:32 PM
If you set the alarm to go off once per second, then every third time it fires, it's been three seconds. So you do it all in one handler:



++counter;
if (counter % 3 == 0)
printf(...);


Note that calling printf() inside a signal handler is technically incorrect.