![]() |
VOOZH | about |
Signals are a mechanism used by the operating system to notify a process that a specific event has occurred, such as an interrupt from the user (e.g., Ctrl+C) or an error condition. A process can handle these signals using signal handlers or allow the default action to occur.
The following program prints the statement continuously. When you press (Ctrl+C), the SIGINT signal is generated, and it terminates the program.
Here is a list of various useful signals and their operations that C++ provides the user to work with.
Signal Name | Description | Default Behaviour |
|---|---|---|
SIGABRT | Abnormal termination triggered by abort() function | Program terminates |
SIGFPE | Floating-point exception (e.g., division by zero or overflow) | Program terminates |
SIGILL | Illegal instruction, typically caused by invalid machine instructions | Program terminates |
SIGINT | Interrupt signal sent when CTRL + C is pressed by the user | Program terminates |
SIGSEGV | Segmentation fault caused by accessing memory in an invalid way | Program terminates |
SIGTERM | Termination request sent by kill or other methods | Program terminates |
SIGKILL | Kill signal for forceful termination of a process | Program terminates immediately (cannot be caught or ignored) |
SIGQUIT | Quit signal (similar to SIGINT but causes core dump). | Program terminates and generates a core dump |
SIGCHLD | Child process terminated or stopped. | No action (ignored by default) |
SIGSTOP | Stop signal that stops the process. It cannot be caught or ignored | Program pauses execution (cannot be caught or ignored) |
SIGSYS | Bad system call (invalid system call invoked). | Program terminates |
SIGUSR1 | User-defined signal 1, available for application use | Program terminates (can be caught or ignored) |
Each signal in C++ has a default behavior, but it can be changed. The process of handling signals manually is called signal handling in C++. This is done using a signal handler function, which is assigned to a signal using the signal() function.
The signal() function takes two arguments, the first is the signal type, and the second is the function that handles the signal when it occurs.
Example:
Output
Geeks
Geeks
Geeks
....
Ctrl+C (Enter by user)
Interrupt handle 2
In the above code, program prints "Geeks" infinitely, when user enter Ctrl+C then signal method handle this signal by singalHandler function, which prints the statement with signal number "Interrupt handle 2".
In the above example, we see that the signal is automatically generated when the user presses Ctrl+C. If we want to achieve this in the program, there are functions available to provide this functionality.
The raise() function allows you to generate the signal in your program. It receives the signal type and set to the current process.
Syntax:
It returns 0 on success, or a non-zero value on failure.
Example:
Interrupt handle 2
The kill() function is used to send signals to other processes, not just the current process. It is available in UNIX like systems and can send a signal to a specific process or group of processes.
Syntax:
where,
Example:
Received signal: 2