Signals are the interrupts that force an OS to stop its ongoing task and attend the task for which the interrupt has been sent. These interrupts can pause service in any program of an OS.
Similarly, C++ also offers various signals which it can catch and process in a program. Each signal has its own default behaviour.
Example:
C++
#include <iostream>
using namespace std;
int main() {
while(1){
cout << "GFG!\n";
}
return 0;
}
The above 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) |
Signal Handling in C++
Each of the signal in C++ has its default behaviour, but this behaviour can be changed. The process of handling the signals manually is called signal handling in C++. It is the done with the help of a signal handler function which is assigned to the corresponding signal using signal() function.
Syntax
C++
signal(signal_type, signal_handler);
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:
C++
#include <csignal>
#include <iostream>
using namespace std;
// Signal handler function
void signalHandler(int sig) {
cout << "Interrupt handle " << sig << endl;
// Optionally exit the program after handling
exit(sig);
}
int main() {
// Handle signal
signal(SIGINT, signalHandler);
// Loop that waits for the signal
while (true) {
cout << "Geeks\n";
}
return 0;
}
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".
Raise Signal Manually
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.
raise() Function
The raise() function allows you to generate the signal in your program. It receives the signal type and set to the current process.
Syntax:
C++
It returns 0 on success, or a non-zero value on failure.
Example:
C++
#include <csignal>
#include <iostream>
using namespace std;
// Signal handler function
void signalHandler(int sig) {
cout << "Interrupt handle " << sig << endl;
// Optionally exit the program after handling
exit(sig);
}
int main() {
// Handle signal
signal(SIGINT, signalHandler);
// Automatically signal generate
raise(SIGINT);
return 0;
}
kill() Function
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:
C++
where,
- pid: The process ID of the target process to which the signal should be sent.
Example:
C++
#include <iostream>
#include <csignal>
#include <unistd.h>
using namespace std;
void handle_signal(int signal_num) {
cout << "Received signal: " << signal_num << endl;
}
int main() {
signal(SIGINT, handle_signal);
// Get current process ID
pid_t pid = getpid();
// Generate signal using kill()
kill(pid, SIGINT);
return 0;
}
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems