***

Master of Science in Computer Science - Computer Science Introductory Course
Laboratory Work 2: Signals

Translated from a French version designed by Bertrand Dupouy


1. Reminder

Send a signal

kill (process_number, signal_number) from C,
kill -signal_number process_number from Shell.

Upon receipt 

ignore the signal signal(signal_number,SIG_IGN)
reset default treatmentsignal(signal_number,SIG_DFL)
define specific treatment signal(signal_number,function)

see /usr/include/sys/signal.h or /usr/include/bits/signum.h or /usr/include/sys/iso/signal_iso.h for a list of signals.

2. Ignore "all" signals

Write a program that ignores all signals. The skeleton is given below. while(1) is there to wait forever for signals.

#include <signal.h>
void main(void)
{
int Nb_Sig ;
for (Nb_Sig = 1; Nb_Sig < NSIG ; Nb_Sig ++)
{
***
}
while(1); /* wait for signals */
}


TO BE DONE::

  1. Test the return value of the "signal" function to identify the "rare" signals that cannot be ignored. See /usr/include/sys/iso/signal_iso.h that is included by #include <signal.h> to identify the signal<function based on the signal number (Nb_Sig).
  2. Hit <CTRL>C in the window attached to the running program. Send signals to this program using kill from another window. See that SIGKILL (signal number 9) terminates the program.

3. Specific signal treatment

Modify the previous program in order to execute the Traite_Sig function when receiving a signal.

The skeleton is given below. while(1) is there to wait forever for signals.


#include <signal.h>
void main(void)
{
void Traite_Sig (int Number);
int Nb_Sig ;

for (Nb_Sig = 1; Nb_Sig < NSIG ; Nb_Sig ++)
{
***
}
while(1); /* wait for signals */
}

/************* Treatment function **************/
void Traite_Sig (int Number)
{
printf("Hello. Received signal %d !\n", Number);
***
}

DO THE SAME AS ABOVE

Warning:
On System V UNIX systems such as Solaris, you have to "rearm" the signal treatment by calling signal(sig_num, function) once a signal has been received once (otherwise the default treatment is used).

4. SIGFPE treatment

Write a program that defines a function to execute upon SIGFPE reception.

Program skeleton :

/*********** SIGFPE specific treatment ***************/
#include <stdio.h>
#include <signal.h>

void main(void)
{
***
signal(SIGFPE, Traite_FPE);
***
while (1)
{
***
/* trigger a SIGFPE signal for example by dividing and integer by zero */
***
}
}


/*********** SIGFPE signal treatment ********/
void Traite_FPE (int Num)
{
printf("Pid %d receives signal %d\n", getpid(), Num);
***
}


Questions :

  1. Verify that we go through Traite_FPE each time the error occurs,
  2. Using setjmp and longjmp restart execution BEFORE the "while" when the error occurs. Call man setjmp or xman setjmp to see details about these functions usage.
    The second longjmp parameter will be the value returned by the previous setjmp.

5. EXERCISE : using SIGUSR1 and SIGUSR2

Write a program that :

Start the program and send it signals including SIGUSR1 and SIGUSR2, from another window using kill.

Program skeleton:

#include <signal.h>

void main (void)
{
***
/* Put here treatment for all signals (except SIGUSR1,SIGUSR2) */
***
/* Put here treatment for SIGUSR1 and SIGUSR2 */
***
while (1); /* wait for signals */

}

/*************** func function **************/
void func (int SignalNum)
{
***
}

/***************
func1 function **************/
void func1 (int
SignalNum)
{
***
}

/***************
func2 function **************/
void func2 (int
SignalNum)
{
***
}


Licence de droits d'usage ©(Copyright) Bertrand Dupouy- Contact : Petr Kuznetsov (surname . name @ telecom-paristech.fr) - Last Modified, sept 26, 2013.