Jump to content

Signal Handling in C Over Linux

- - - - -

  • Please log in to reply
No replies to this topic

#1
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Introduction

A signal is a software interrupt that is delivered to a running process because of an event. For example, if a process is running, and user presses Ctrl-C, Process Termination signal is generated which in turn terminates the process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses like Segment Faults; others report asynchronous events, such as disconnection of a phone line.

Types of Signals

A signal that is delivered by operating system to a running program reports the occurrence of an event. These are some of the events that can cause (or generate, or raise) a signal:

1) - A program error such as dividing by zero or issuing an address outside the valid range.
2) - A user request to interrupt or terminate the program. Most environments are set up to let a user suspend the program by typing C-z, or terminate it with C-c.
3)- A Process termination signal that terminates a process.
4)- A signal to report the Expiration of a alarm set by the process.
5)- User defined signals

This tutorial would show we a specific signal can be handled by a running program.

Signal Handling

Whenever an exceptional events occurs, for example, user has press Ctrl-C to terminate the process, operating system reports it to running process. Now there are following choices for the process

1)- Ignore the signal, if signal has been added to blocked list; it would be ignored
2)- Define a signal handler for the signal, and that handler would be executed when signal is fired.
3)- Let the default action executed; this occurs, when there is no particular handler is defined for raised signal.

In this tutorial, we ll see how to specify a signal handler for a signal. Look at the following function definition


signal(int SignalNumber, void (*SignalHandler)(int sig));


The above function is used to define a signal handler for a signal specified by parameter SignalNumber.
void (*SignalHandler)(int sig) is a function pointer that defines pointer to Signal Handler function: This handler is executed when event is reported by operating system to the running process.

A detail about standard signals can be found over Standard Signals - The GNU C Library.

The following is a complete example about how to set an alarm, and setting a signal handler for Signal Alarm.


#include <signal.h>

#include <stdio.h>

#include <stdlib.h>


/* This flag controls termination of the main loop.  */

volatile sig_atomic_t keep_going = 1;


/* The signal handler just clears the flag and re-enables itself.  */

void 

catch_alarm (int sig)

{

  keep_going = 0;

  signal (sig, catch_alarm);

}


void 

do_stuff (void)

{

  puts ("Doing stuff while waiting for alarm....");

}


int

main (void)

{

  /* Establish a handler for SIGALRM signals.  */

  signal (SIGALRM, catch_alarm);


  /* Set an alarm to go off in a little while.  */

  alarm (2);


  /* Check the flag once in a while to see when to quit.  */

  while (keep_going)

    do_stuff ();


  return EXIT_SUCCESS;

}



References
1. The GNU C Library - Signal Handling
2. Signal Handling - The GNU C Library




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users