Like Tree1Likes
  • 1 Post By Malcolm McLean

Creating / receiving timers

This is a discussion on Creating / receiving timers within the C Programming forums, part of the General Programming Boards category; Hi, Please could someone explain this simple timing code (to be used under Linux). I get most of it, but ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    19

    Creating / receiving timers

    Hi,

    Please could someone explain this simple timing code (to be used under Linux). I get most of it, but some of the struct members like sa_flags confuses me and I can't find something that explains it clearly.

    Code:
    #include <signal.h>
    #include <unistd.h>
    #include <stdio.h>
    
    
    volatile int stop=0;
    
    
    void sigalrm_handler( int sig )
    {
        stop = 1;
    }
    
    
    int main(int argc, char **argv)
    {
        struct sigaction sact;
        int num_sent = 0;
    
    
        sigemptyset (&sact.sa_mask);
        sact.sa_flags = 0;
        sact.sa_handler = sigalrm_handler;
        sigaction(SIGALRM, &sact, NULL);
    
    
        alarm(1);  /* Request SIGALRM in 60 seconds */
        while (!stop) {
            printf("Sent");
            num_sent++;
        }
    
    
        printf("sent %d packets\n", num_sent);
        return(0);
    }
    I hope to use timing code in some of my projects. Is it possible to create multiple sigalarms at once?

    Thanks in advance,
    Joshun

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You should always read the documentation for all the functions you use. On Linux, this is usually available in sections 2 or 3 of the man pages (i.e. type "man 2 sigaction" or "man 3 sigaction" from the command line). For example, the sigaction man pages (sigaction(2): examine/change signal action - Linux man page and sigaction(3): examine/change signal action - Linux man page) discuss the flags
    Quote Originally Posted by man 2 sigaction
    sa_flags specifies a set of flags which modify the behavior of the signal. It is formed
    by the bitwise OR of zero or more of the following:


    SA_NOCLDSTOP
    If signum is SIGCHLD, do not receive notification when child processes stop
    (i.e., when they receive one of SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU) or resume
    (i.e., they receive SIGCONT) (see wait(2)). This flag is only meaningful when
    establishing a handler for SIGCHLD.


    SA_NOCLDWAIT (Since Linux 2.6)
    If signum is SIGCHLD, do not transform children into zombies when they termi‐
    nate. See also waitpid(2). This flag is only meaningful when establishing a
    handler for SIGCHLD, or when setting that signal's disposition to SIG_DFL.


    If the SA_NOCLDWAIT flag is set when establishing a handler for SIGCHLD,
    POSIX.1 leaves it unspecified whether a SIGCHLD signal is generated when a
    child process terminates. On Linux, a SIGCHLD signal is generated in this
    case; on some other implementations, it is not.


    SA_NODEFER
    Do not prevent the signal from being received from within its own signal han‐
    dler. This flag is only meaningful when establishing a signal handler.
    SA_NOMASK is an obsolete, non-standard synonym for this flag.


    SA_ONSTACK
    Call the signal handler on an alternate signal stack provided by sigalt‐
    stack(2). If an alternate stack is not available, the default stack will be
    used. This flag is only meaningful when establishing a signal handler.


    SA_RESETHAND
    Restore the signal action to the default state once the signal handler has been
    called. This flag is only meaningful when establishing a signal handler.
    SA_ONESHOT is an obsolete, non-standard synonym for this flag.


    SA_RESTART
    Provide behavior compatible with BSD signal semantics by making certain system
    calls restartable across signals. This flag is only meaningful when establish‐
    ing a signal handler. See signal(7) for a discussion of system call restart‐
    ing.


    SA_SIGINFO (since Linux 2.2)
    The signal handler takes 3 arguments, not one. In this case, sa_sigaction
    should be set instead of sa_handler. This flag is only meaningful when estab‐
    lishing a signal handler.
    A few other notes:
    1. alarm(1) triggers a SIGALRM in 1 second, not 1 minute (60 sec).
    2. alarm() and the SIGALRM don't play nicely with some of the other timing functions, like sleep, usleep, etc. Read the man page (sections 2 and 3) for details: alarm(2) - Linux man page and alarm(3): schedule alarm signal - Linux man page.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    You've got to read the documentation.
    The flags are being set to all zero, which is likely to be the default state

    This link will tell you what they do, which I found by a web search on sigaction.

    sigaction(2) - Linux manual page
    Salem likes this.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timers in c
    By kiros88 in forum C Programming
    Replies: 13
    Last Post: 09-09-2009, 06:22 PM
  2. Replies: 1
    Last Post: 03-21-2008, 08:15 PM
  3. Timers
    By Deb in forum C Programming
    Replies: 16
    Last Post: 04-23-2007, 10:15 AM

Tags for this Thread


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