Using alarm() and other signals.

This is a discussion on Using alarm() and other signals. within the Linux Programming forums, part of the Platform Specific Boards category; So I'm having a hard time trying to figure out how alarm() works. Let's say I wanted to make a ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    37

    Using alarm() and other signals.

    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.

    Code:
    #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;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    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

    Code:
    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);

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Quote Originally Posted by ArcadeEdge View Post
    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.
    Code:
    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.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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:

    Code:
    ++counter;
    if (counter % 3 == 0)
        printf(...);
    Note that calling printf() inside a signal handler is technically incorrect.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alarm()
    By TaiL in forum C Programming
    Replies: 1
    Last Post: 07-16-2009, 09:55 AM
  2. Alarm
    By kky2k in forum C Programming
    Replies: 10
    Last Post: 05-29-2007, 11:58 PM
  3. 2-3 Second delay with alarm()
    By Longie in forum C Programming
    Replies: 11
    Last Post: 06-20-2004, 08:46 PM
  4. Settimer or Alarm?????
    By gvector1 in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 09:56 AM
  5. alarm
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 08:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21