0% found this document useful (0 votes)
42 views3 pages

Signals Lab

The document discusses signals in Linux, including how signals are generated, ignored, and processed with specific functions. It provides code examples for ignoring all signals, processing signals with a custom function, and sending signals between parent and child processes. The examples demonstrate how signals are handled asynchronously and how sending too many signals quickly can cause issues.

Uploaded by

Asif Talukder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Signals Lab

The document discusses signals in Linux, including how signals are generated, ignored, and processed with specific functions. It provides code examples for ignoring all signals, processing signals with a custom function, and sending signals between parent and child processes. The examples demonstrate how signals are handled asynchronously and how sending too many signals quickly can cause issues.

Uploaded by

Asif Talukder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab on signals

1. Presentation of signals
The signals have various origins, they can be:

1. retransmitted by the kernel: division by zero, overflow, prohibited instruction,


2. sent from the keyboard by the user ( keys: <CTRL>Z, <CTRL>C, <CTRL>\.. )...
3. issued by the kill command from the shell or from C by calling the kill primitive:

• Sending:
kill (num_du_processus, num_du_signal) in C,
kill -num_du_signal num_du_processus, in Shell.

Note: The sender cannot know whether the receiver has received the signal or not.

• Receiving:

possible behaviors of the signal recipient:

ignore signal signal(num_du_signal,SIG_IGN)

reposition the default treatment signal(num_du_signal,SIG_DFL)

define a specific treatment signal(num_du_signal,fonction)

The signals are described in /usr/include/sys/signal.h or /usr/include/sys/iso/signal_iso.h

Remarks :

1. On UNIXs of the Berkeley family, for example Solaris from Sun, after running the specific function
defined by the user, the default option is restored. If you want to keep the specific option, you have to recall
signal(num_sig, fonction) in function.
2. We are not allowed to change the default processing of certain signals, such as SIGKILL.

2.Ignore Signals
Write a program that ignores ALL signals. The programming scheme is given below. The role of while(1) is to loop
to wait for the reception of a signal.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main(void){
int Nb_Sig;
for(Nb_Sig = 1; Nb_Sig < NSIG ; Nb_Sig ++){
...
} while(1){
sleep(5);
} /* Attendre des signaux */
return 0;
}
To do:

1. Test the return value of the signal function to identify the (rare) signals that cannot be ignored. Consult
/usr/include/sys/iso/signal_iso.h which is included by #include <signal.h> to identify the signal
according to its number (Nb_Sig).
2. Press <CTRL>C in the window where the program is running. Also send signals to this program using kill from
another terminal. Note that SIGKILL (signal number 9) terminates this program.

3. Specific signal processing


Modify the previous program: the signals will no longer be ignored, but processed by a special function, which will
be called Traite_Sig.
The programming scheme is given below. The role of while(1 is to loop to wait for the reception of a signal.
#include <signal.h>
#include <unistd.h> int
main(void){
void Traite_Sig(int Numero); int
Nb_Sig ;
for(Nb_Sig = 1; Nb_Sig < NSIG ; Nb_Sig
++){
...
}
while(1){
sleep(5); /* Attendre des signaux */
}
}

/************* Fonction de traitement **************/ void


Traite_Sig (int Numero){
printf("Coucou, recu signal %d !\n", Numero);
... }

Proceed as before, i.e.:

1. Test the return value of the signal function to identify the (rare) signals for which specific processing cannot
be defined.
2. Press <CTRL>C in the window where the program is running. Also send signals to this program using kill from
another terminal. Note that SIGKILL (signal number 9) terminates this program.

4. Signals between parent and child processes:


1) write a program that does a "fork()". The child sends a “SIGUSR1” signal 20 times to the parent. Each time the
parent receives the "SIGUSR1" signal, he displays on the screen: "I am the parent, I received a SIGUSR1 signal from
my child for the Nth time" (where N is the number of times the signal is received). After the 20 signal sends, the
child ends. The parent then displays "My child process with pid X has just terminated, and returned status Y", and the
parent terminates.
Launch this program. What do you observe? Why ?
2) modify the code of the child to no longer send a "SIGUSR1" signal 20 times, but 200,000 times. What's going on ?
Why ?
3) modify the child again to only send the "SIGUSR1" signal 20 times to the parent (as in question 1). But this time,
in the child's code, after the call to the "kill()" function, do a "sleep(1)" (which blocks the process for 1 second).
What's going on ? Why ?

You might also like