Signals
This is Software Interrupt to processor. For Example: Cntl^C, sends SIGINT to running process. Process should handle this. All signals start from SIG.
Signal Examples
| Signal | Signal Number | Details |
|---|---|---|
| SIGHUP | 1 | Hang Up When terminal controlling the process is closed |
| SIGINT | 2 |
keyboard Interrupt(Cntrl C) Can be caught?Yes If not caught, Terminate the process. Used for graceful termination |
| SIGQUIT | 3 |
Keyboard Quit Cntl +\ OR Cntl Z Can be caught? Yes Similar to SIGINT but Generates core dump on termination, cleans up resources |
| SIGBUS | signal is sent to a process when it causes a bus error(access memory that he CPU cannot physically address: an invalid address for the address bus) | |
| SIGTRAP | 5 |
Breakpoint Debuggers register and handle this signal.This is sent to debugger process when trap() occurs, for example, when a particular function is executed, or when a particular variable changes value |
| SIGABRT | 6 |
Abort current process abort() system call
|
| SIGKILL | 9 |
This CANNOT be caught. Will always terminate the process. Terminates
immediately, does not allows process to do clean up
|
| SIGSEGV(Segmentation fault) | 11 |
Program accessed a memory location that was not assigned.
|
| SIGALRM | 14 |
Alarm clock
|
| SIGTERM | 15 |
If not caught, Terminate the process. Used for graceful termination.
|
| SIGCHLD | 17 |
Child Process has stopped or Exited. It can be caught. When child dies it send SIGCHLD to parent.
|
| SIGCONT | 18 |
Continue the suspended process. It cannot be caught Resume the stopped/suspended process. It always resumes the process. |
| SIGSTOP | 19 |
Suspend the process donot Kill kill -STOP pid It cannot be caught |
| SIGPIPE |
Scenario-1: When process attempts to write to a pipe whose Read-end
is closed.
Scenario-2: Attempt to write to socket what is no longer open for reading. |
|
| SIGUSR1, SIGUSR2 |
User Defined Signals. They donot have any special meaning as
SIGTERM, SIGKILL etc. Kernel will never send SIGUSR to process. Meaning of signal can be set up by user as per need of application. |
|
| SIGWINCH | Signals Window Change sent automatically when a terminal detects a change in it's windows size to allow for a redraw. |
Signal Mask
Collection of Signals blocked is called SM of process. sigprocmask() API is used to define Signal mask but in Single Threaded App. use pthread_sigmask() instead of sigprocmask()
Catching Signals
// This program will not terminate on Cntrl^C.
#include <signal.h>
#include <iostream>
void SIGINT_Handler(int signalNumber){
std::cout << "Got Cntrl^C\n";
return;
}
int main(){
struct sigaction stAction;
stAction.sa_flags = 0;
sigemptyset (&stAction.sa_mask);
//Adding Handler for SIGINT
stAction.sa_handler = SIGINT_Handler;
sigaction (SIGINT, &stAction, NULL);
while(1);
}