A signal is an indicator that is generated through software and is used to stop the usual execution of the program, and through a branch of CPU to perform some specific tasks. The signal can be generated either by a process or when the user presses Ctrl + C. When something goes wrong or some error occurs while executing an operation, the signal acts as a medium of communication between the process and the operating system. The signal is raised by the operating system and is forwarded to the process to take necessary actions. Essentially, a corresponding signal handler is executed as a corrective measure.
The following are some of the important signals that you should be aware of:
- SIGABRT (Signal Abort): This signal reports the abnormal termination of the program. The signal is raised in the case of critical errors; for example, if an assertion fails or memory could not be allocated, or any similar memory heap errors.
- SIGFPE (Signal Floating-Point Exception): This signal reports an arithmetic error. Any arithmetic error, including overflow or divide by zero, is covered by this signal.
- SIGILL (Signal Illegal Instruction): This signal reports illegal instructions. Such a signal is raised when the program tries to execute data or an executable file is corrupted. In other words, the signal is raised when the program is trying to execute a nonexecutable instruction.
- SIGINT (Signal Interrupt): This is a program interrupt signal that is generated by the user by pressing Ctrl + C.
- SIGSEGV (Signal Segmentation Violation): This signal is raised when the program tries to write into read-only memory or into a block that does not have write permissions. The signal is also raised when a program tries to read or write into memory that is outside the range that is allocated to it.
- SIGTERM (Signal Terminate): This is a termination signal that is sent to a process in order to terminate it.
To handle signals in a program, the signal() function is used. Let's look at a quick introduction to the signal function.