2. A signal is used to notify a process that a particular
event has occurred.
Signals are software Interrupts
A Signal may be received synchronously or
asynchronously.
3. Linux Signals
In Linux, every signal has a name that begins with
characters SIG.
A SIGINT signal that is generated when a user
presses ctrl+c. This is the way to terminate programs
from terminal.
A SIGFPE is generated when the divide by zero
occurs.
4. Signal Handling
Every signal may be handled by one of two possible
handlers.
A default signal handler
A user-defined signal handler
5. User-defined signal handler
Every signal has a default signal handler that is run
by the kernel when handling that signal.
The default action can be overridden by a user-
defined signal handler that is called to handle the
signal.
Provide the handler function which has the syntax
void handler_name(int signo);
Register the signal handler with the signal
Signal(Signal_Number , handler_name);
18. Ignoring a Signal SIG_IGN
void myhandler(int a)
{
printf("ENTERED MYHANDLER n");
printf("signal recieved : %d n",a);
exit(1);
}
int main()
{
int x = 0;
printf("1. Default 2. Ignore 3. Handler n");
scanf(" %d",&x);
switch(x)
{
case 1 : signal(SIGINT , SIG_DFL); break;
case 2 : signal(SIGINT , SIG_IGN); break;
case 3 : signal(SIGINT , myhandler); break;
}
sleep(5);
printf("END OF PROGRAM ");
}
Notes de l'éditeur
What is a signal?
Signals are software interrupts.
signals are a way to deliver asynchronous events to the application.
A user hitting ctrl+c, referring a invalid address etc are all such cases where a process needs to do signal handling.
When the signal occurs, the process has to tell the kernel what to do with it. There can be three options through which a signal can be disposed :
The signal can be ignored. By ignoring we mean that nothing will be done when signal occurs. Most of the signals can be ignored but signals generated by hardware exceptions like divide by zero, if ignored can have weird consequences.
SIGKILL and SIGSTOP signals cannot be ignored.
The signal can be caught. When this option is chosen, then the process registers a function with kernel. This function is called by kernel when that signal occurs. If the signal is non fatal for the process then in that function the process can handle the signal properly or otherwise it can chose to terminate gracefully.
Let the default action apply. Every signal has a default action. This could be process terminate, ignore etc.
As we already stated that two signals SIGKILL and SIGSTOP cannot be ignored. This is because these two signals provide a way for root user or the kernel to kill or stop any process in any situation .The default action of these signals is to terminate the process. Neither these signals can be caught nor can be ignored.
Floaterror.c
Sigfpe.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void myhandler(int signo)
{
printf("\n Signal Recieved \n");
printf(" Signal Number = %d \n" , signo);
exit(1);
}
int main()
{
signal(SIGFPE , myhandler);
int x = 0 , y = 0,res;
printf("Enter the numerator :");
scanf(" %d",&x);
printf("Enter the denominator : ");
scanf("%d",&y);
printf("WE ARE PERFORMING DIVISION ");
res = x / y;
printf("\n result = %d \n" , res);
}
Sigint.c
SIGCHLD – Indicates the death of a child process.
Without sleep, child ends before parent. Signal caught
With sleep, parent ends before child. No Signal
#include <signal.h>
#include <unistd.h>
int flag = 0;
void myfunc(int a);
int main()
{
signal(SIGCHLD , myfunc);
int ret = fork();
if(ret == 0)
{
printf("CHILD id = %d \n" , getpid());
sleep(2);
printf("AFTER SLEEP \n");
}
else
{
while(flag == 0)
pause();
printf("CHILD TERMINATED \n");
}
}
void myfunc(int a)
{
printf("ENTERED MYHANDLER \n");
printf("signal recieved : %d \n",a);
if(a == SIGCHLD)
flag = 1;
printf(" id = %d \n",getpid());
}