Interrupts and Signals: <signal.h> (Unix)

Discussion in 'C' started by pradeep, May 27, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    When a process terminates abnormally it usually tries to send a signal indicating what went wrong. C programs (and UNIX) can trap these for diagnostics. Also user specified communication can take place in this way.

    Signals are software generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE and SIGSEGV, but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt or stop or a kill request from another process. Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Signal delivery is analogous to hardware interrupts in that a signal can be blocked from being delivered in the future. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Each signal has a default action which is one of the following:


    1. The signal is discarded after being received
    2. The process is terminated after the signal is received
    3. A core file is written, then the process is terminated
    4. Stop the process after the signal is received
    Each signal defined by the system falls into one of five classes:


    • Hardware conditions
    • Software conditions
    • Input/output notification
    • Process control
    • Resource control
    Macros are defined in <signal.h> header file for common signals.

    These include:


    • SIGHUP 1 /* hangup */ SIGINT 2 /* interrupt */
    • SIGQUIT 3 /* quit */ SIGILL 4 /* illegal instruction */
    • SIGABRT 6 /* used by abort */ SIGKILL 9 /* hard kill */
    • SIGALRM 14 /* alarm clock */
    • SIGCONT 19 /* continue a stopped process */
    • SIGCHLD 20 /* to parent on child stop or exit */
    Signals can be numbered from 0 to 31.


    Sending Signals -- kill(), raise()

    There are two common functions used to send signals

    int kill(int pid, int signal) - a system call that send a signal to a process, pid. If pid is greater than zero, the signal is sent to the process whose process ID is equal to pid. If pid is 0, the signal is sent to all processes, except system processes.


    kill() returns 0 for a successful call, -1 otherwise and sets errno accordingly.


    int raise(int sig) sends the signal sig to the executing program. raise() actually uses kill() to send the signal to the executing program:


    kill(getpid(), sig);

    There is also a UNIX command called kill that can be used to send signals from the command line - see man pages.

    NOTE: that unless caught or ignored, the kill signal terminates the process. Therefore protection is built into the system.

    Only processes with certain access privileges can be killed off.

    Basic rule: only processes that have the same user can send/receive messages.

    The SIGKILL signal cannot be caught or ignored and will always terminate a process.

    For examplekill(getpid(),SIGINT); would send the interrupt signal to the id of the calling process.

    This would have a similar effect to exit() command. Also ctrl-c typed from the command sends a SIGINT to the process currently being.

    unsigned int alarm(unsigned int seconds) -- sends the signal SIGALRM to the invoking process after seconds seconds.

    Signal Handling -- signal()

    An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways:


    • The process can let the default action happen
    • The process can block the signal (some signals cannot be ignored)
    • The process can catch the signal with a handler.
    Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself

    Receiving signals is straighforward with the function:

    int (*signal(int sig, void (*func)()))() -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise.

    func() can have three values:


    SIG_DFL
    -- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig.
    SIG_IGN
    -- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL).
    A function address
    -- a user specified function.
    SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file.


    Thus to ignore a ctrl-c command from the command line. we could do:

    signal(SIGINT, SIG_IGN);

    TO reset system so that SIGINT causes a termination at any place in our program, we would do:

    signal(SIGINT, SIG_DFL);

    So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program:

    Code:
     #include <stdio.h>
      
     void sigproc(void);
      
     void quitproc(void); 
      
     main()
     { signal(SIGINT, sigproc);
              signal(SIGQUIT, quitproc);
              printf(``ctrl-c disabled use ctrl- to quitn'');
              for(;;); /* infinite loop */}
      
     void sigproc()
     {          signal(SIGINT, sigproc); /*  */
              /* NOTE some versions of UNIX will reset signal to default
              after each call. So for portability reset signal each time */
      
              printf(``you have pressed ctrl-c n'');
     }
      
     void quitproc()
     {          printf(``ctrl- pressed to quitn'');
              exit(0); /* normal exit status */
     }
     
    Other signal functions

    There are a few other functions defined in signal.h:

    int sighold(int sig) -- adds sig to the calling process's signal mask

    int sigrelse(int sig) -- removes sig from the calling process's signal mask

    int sigignore(int sig) -- sets the disposition of sig to SIG_IGN

    int sigpause(int sig) -- removes sig from the calling process's signal mask and suspends the calling process until a signal is received
     
  2. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Nice documentation.
     
  3. Crusader

    Crusader New Member

    Joined:
    Feb 15, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Ultimate Document...........PLease keep on posting articles like this.......It is really helpful for the beginners like me.....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice