![]() |
VOOZH | about |
A signal is a software-generated interrupt sent to a process by the OS when the user presses Ctrl-C or another process sends a signal to this process. There is a fixed set of signals that can be sent to a process. Signals are identified by integers value, for example, the value of SIGINT is 2.
In C, signals are handled by their default behavior, but the language also provides a way to manage them manually. This process, known as signal handling, allows us to define custom actions for specific signals. By using a signal handler function, we can control how a signal is processed, giving us flexibility in how the program responds to events like errors or interruptions.
There are several default signal handler functions. Each signal is associated with one of these default handler routines. The different default handler routines typically have one of the following actions:
Output
hello world
hello world
hello world
. .
. .
Above program prints "hello world" infinitely. When the user presses Ctrl + C, the SIGINT signal is sent, and the default handler terminates the process.
A process can replace the default signal handler for almost all signals (except SIGKILL) with its own handler function. A signal handler can have any name, but it must return void and accept a single int parameter, representing the signal number.
To trigger the signal handler when the signal has occurred, we use signal() function that is provided by <signal.h> header file.
Syntax:
signal(type, signalHandler);
where,
Output
Hello World!
Hello World!
Hello World!
....
Ctrl+C (Enter by user)
Caught SIGINT
In the above code, the program prints "Hello World!" continuously. When the user presses Ctrl+C, the OS sends a SIGINT signal to the process. The registered signalHandler function is invoked, which safely prints "Caught SIGINT" and terminates the program.
In the above example, you can see that the signal is automatically generated when the user presses Ctrl+C. There are functions available that provide the functionality to generate signals manually.
The raise() function in C enables you to send a signal to the current process. It takes the signal type as an argument and triggers that signal within the same process.
Syntax:
raise(signal_type);
It returns 0 on success, or a non-zero value on failure. Example:
Interrupt handled: 2
The kill() function allows us to send the signals to other processes or group of processes by using process id.
Syntax:
kill(pid, signal_type);
where, pid: The process ID of the target process to which the signal should be sent. Example:
Output:
Received Signal: 2C offers various types of interruptions, which are listed below:
Signal Name | Description | Default Behaviour |
|---|---|---|
SIGABRT | Abnormal termination (abort) by the program. | Program terminates |
SIGFPE | Floating-point exception (e.g., division by zero or overflow). | Program terminates |
SIGILL | Illegal instruction (e.g., invalid opcode). | Program terminates |
SIGINT | Interrupt signal (sent when Ctrl+C is pressed by the user). | Program terminates |
SIGSEGV | Segmentation fault (invalid memory access). | Program terminates |
SIGTERM | Termination signal (request to terminate the process). | Program terminates |
SIGKILL | Kill signal (forceful termination of a process). | Program terminates |
SIGBUS | Bus error (e.g., misaligned memory access). | Program terminates |
SIGQUIT | Quit signal (similar to SIGINT but causes core dump). | Program terminates (Core dump) |
SIGCHLD | Child process terminated or stopped. | No action (handled by parent) |
SIGCONT | Continue a stopped process. | Process resumed |
SIGTSTP | Terminal stop signal (sent by pressing Ctrl+Z). | Process Stop |
Example: In the example below, the SIGINT (2) signal is blocked, and no signals are pending.
👁 ImageA signal is sent to a process by setting the corresponding bit in its pending signals integer. When the OS selects a process to run, it checks the pending and blocked signals. If no signals are pending, the process continues normally. If signals are pending but blocked, the process continues, and the signals remain pending. If any signals are pending and not blocked, the OS handles them using the process’s signal routines.