Signals ======== - A signal is an event generated by UNIX/LINUX system in response to some condition. - On receipt of a signal, process may in turn take some action. - Signals are generated by some error conditions such as:- * Memory Segment Violation * Floating Point Processor error * Illegal Instruction etc. - They are generated by shell and terminal handlers. - They cause interrupts.
- They can also be used to sent from one process to another as a way of passing information or modifying behaviour.
- As default, if a process receives any signal, process will be terminated immediately. - Usually a core dump file is created.
- The signals are defined by including header file signal.h - They all begn with "SIG". - There are 64 signals in LINUX. - The can be listed using command :- kill -l
Signal Name Description SIGABORT Process Abort SIGALRM Alarm Clock SIGFPE Floating Point Exception SIGHUP Hangup SIGILL Illegal Instruction SIGINT Terminal Interrupt SIGKILL Kill (can't be caught or ignored) SIGPIPE Write on a pipe with no reader SIGQUIT Terminal Quit SIGSEGV Invalid memory segment access (Segmentation Fault) SIGTERM Termination SIGUSR1 User-defined signal 1 SIGUSR2 User-defined signal 2
SIGCHLD Child process has stopped or exited SIGCONT Continue exexuting, if stopped SIGSTOP Stop Executing (Can't be caught or ignored) SIGTSTP Terminal stop signal SIGTTIN Background process trying to read SIGTTOU Background process trying to write
Register a signal ================== - Program can handle a signal using signal library function signal(). - signal() is used to register the signal handler function with kernel on behalf of current process. - It is included in header :- #include<header.h> - Prototype of function is :- void* (signal(int signo,void(*func)(int)))(int); - signal() function takes 2 parameters, signo and func. - The signal to be caught or ignored is given as argument signo. - The function to be called when the specified signal is received is given as func. - The function must take a single int argument (the signal received) and has return type void.
The signal() command can be used in 3 modes :- (i) Default mode (Terminate the process) SIG_DFL --> is used in place of func. (ii) Ignore the signal SIG_IGN --> is used in place of func. (iii) User defined mode User defined function is used in place of func.
The signal command returns the function which is setup to handle the signal, SIG_DFL or SIG_IGN in 3rd, 1st and 2nd cases respectively.
Send a signal using Kill command ============================== - To send a signal to a process other than current task. - It has prototype :- int kill(pid_t pid, int signo); - It is included using header :- #include<signal.h> - The system data type pid_t is defined in header :- #include<sys/types.h> - signo is the signal number of signal to be send. - pid is the PID of the process to which signal is to be send. - pid can take value:- * 0 --> To send signo signal to every process in invoking process' process group * -1 -->To send signo signal to every process for which invoking process has permission to send a signal, except itself and init * n(<-1) --> All process in process group * n(>0) --> process with PID n is signalled. - This take an optional signal name or number and the PID to send the signal.
- For eg. To send 'hangup' signal to shell running on PID 1001, we use command :- kill(1001,HUP); - killall is used to send signal to all the processes running.
Return type and error checking of kill ------------------------------------------------------ - kill sends a signal only when it has permission to send the signal to that process. - The processes must have same user ID. - But superuser can send signals to any process. - It returns 0 on success. - If it fails, it returns -1 - On a caseof error it sets errno to:- * EINVAL -->if signal is not valid * EPERM -->if it do not have permission * ESRCH -->if specified process doesn't exist