SIGINT in Linux

This article aims to provide a comprehensive overview of SIGINT and signal handling in Linux.

1. Introduction

In the world of Linux systems, signal handling plays a crucial role in managing processes effectively. One such signal, SIGINT, holds significant importance for both developers and users. This article dives into the fundamentals of signal handling, focusing specifically on SIGINT, its behavior, applications, and best practices.

2. Basics of Signals in Linux

2.1 Overview of signals and their types

Signals are software interrupts sent to processes in order to notify them of specific events or conditions. They can be used to control program execution, handle exceptional situations, or facilitate interprocess communication. Common signals include SIGTERM, SIGHUP, SIGKILL, and SIGINT.

Signal NameSignal NumberDescription
SIGINT2Interrupt signal. Sent by the terminal to request program termination, typically triggered by pressing Ctrl+C.
SIGTERM15Termination signal. Sent to request a program’s termination gracefully. Allows cleanup operations to be performed before exiting.
SIGHUP1Hangup signal. Sent when a controlling terminal is closed or disconnected. Often used to instruct daemons to reload their configuration files.
SIGKILL9Kill signal. Forces immediate termination of a program without giving it a chance to clean up or save any state.
SIGSTOP19Stop signal. Suspends the execution of a process. Can be used for debugging or to pause a process temporarily.
SIGCONT18Continue signal. Resumes the execution of a previously stopped process. Used to continue a process after receiving a SIGSTOP or SIGTSTP signal.
SIGUSR110User-defined signal 1. Can be used by programs to establish custom communication or trigger specific actions.
SIGUSR212User-defined signal 2. Similar to SIGUSR1, it provides an additional user-defined signal for program-specific purposes.
SIGALRM14Alarm clock signal. Sent when a timer set by the alarm() function or the setitimer() system call expires.
SIGSEGV11Segmentation fault signal. Indicates a memory access violation, such as accessing an invalid memory location.
SIGILL4Illegal instruction signal. Indicates the execution of an illegal or undefined instruction.
SIGPIPE13Broken pipe signal. Sent when writing to a pipe or socket that has been closed on the other end.
SIGBUS7Bus error signal. Indicates a memory alignment or bus architecture-related error.
SIGFPE8Floating-point exception signal. Indicates arithmetic errors, such as division by zero or overflow.
SIGCHLD17Child process status change signal. Sent to the parent process when a child process terminates or stops.

Note: The table provides an overview of commonly used signals and their associated signal numbers. Signals can vary depending on the specific operating system and its implementation.

3. Understanding SIGINT

SIGINT, derived from “signal interrupt,” is triggered by a keyboard input, specifically the Ctrl+C combination. Its primary purpose is to allow users to interrupt a program’s execution and request its termination gracefully.

3.1 Triggering SIGINT with keyboard input

Pressing Ctrl+C in a terminal sends a SIGINT signal to the currently active process, indicating the desire to interrupt its execution.

4. Signal Handling in Linux

Signal handling in Linux allows processes to receive and respond to different signals sent by the operating system or other processes. It enables programs to handle exceptional conditions, control program execution, and facilitate interprocess communication. Understanding how to handle signals is crucial for developing robust and responsive Linux applications.

In Linux, signal handling involves two main components: signal handlers and the process of setting up and managing these handlers.

4.1 Signal Handlers

Signal handlers are functions that are executed when a process receives a specific signal. They allow programs to define custom actions to be taken in response to a signal. Signal handlers can be used to perform cleanup operations, save state, or handle exceptional conditions gracefully before exiting.

4.2 Setting up Signal Handlers

Setting up signal handlers involves associating a signal with a specific handler function. This process varies depending on the programming language and system API used. Here’s a general overview of how signal handlers can be set up in C/C++ programs.

4.2.1 Using the signal() function

The signal() function is a simple and widely supported mechanism for setting up signal handlers. It takes two arguments: the signal number and the handler function. For example, to set up a signal handler for SIGINT (interrupt signal):


void sigintHandler(int signal) {
    // Signal handling code

int main() {
    signal(SIGINT, sigintHandler);
    // Rest of the program

Here, sigintHandler is the user-defined function that will be executed when the SIGINT signal is received.

4.2.2 Using the sigaction() function

The sigaction() function provides a more powerful and flexible way to set up signal handlers. It allows for finer control over signal handling, including features like signal masking and advanced signal handling options. Although slightly more complex, it offers better control over signal behavior. Here’s an example:


void sigintHandler(int signal) {
    // Signal handling code

int main() {
    struct sigaction sa;
    sa.sa_handler = sigintHandler;
    sa.sa_flags = 0;

    sigaction(SIGINT, &sa, NULL);
    // Rest of the program

In this example, sigaction() is used to set up the SIGINT signal handler using a struct sigaction that specifies the handler function, signal mask, and flags.

4.3 Common Signal Handling Tasks

Signal handling enables various tasks in Linux applications, such as:

4.3.1 Graceful termination

Handling signals like SIGINT and SIGTERM allows programs to perform cleanup operations, release resources, and exit gracefully.

4.3.2 Handling specific events

Custom signal handlers can be used to respond to user-defined events or specific conditions that need special handling.

4.3.3 Interprocess communication

Signals can be used to signal events or trigger actions between different processes, allowing for basic interprocess communication.

4.3.4 Error handling

Signals like SIGSEGV or SIGFPE can be caught by signal handlers to handle errors, log diagnostic information, or gracefully recover from exceptional conditions.

Overall, understanding signal handling in Linux is essential for managing program execution, handling exceptional scenarios, and ensuring the stability and responsiveness of applications. By setting up appropriate signal handlers and implementing proper handling logic, developers can enhance the robustness and reliability of their Linux programs.

5. Using signal masks to control signal delivery

Signal masks allow programs to control which signals are temporarily blocked or delivered, providing finer-grained control over signal handling behavior. By manipulating signal masks, you can choose to ignore or delay the delivery of certain signals to specific sections of code. Here’s how you can use signal masks to control signal delivery in Linux.

5.1 Initializing and modifying signal masks

Before manipulating signal masks, you need to initialize and modify them using the sigset_t data type and related functions from the <signal.h> header.


int main() {
    sigset_t mask;
    sigemptyset(&mask);  // Initialize an empty signal mask

    // Add signals to the mask using sigaddset()
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGTERM);

    // Block the signals in the mask using sigprocmask()
    sigprocmask(SIG_BLOCK, &mask, NULL);

    // Rest of the program code

    return 0;

In this example, the sigset_t type is used to represent the signal mask. The sigemptyset() function initializes an empty signal mask, and sigaddset() adds signals (e.g., SIGINT and SIGTERM) to the mask. Finally, sigprocmask() blocks the signals in the mask, preventing their delivery to the program.

5.2 Temporarily blocking or unblocking signals

Within specific sections of code, you can temporarily block or unblock signals using the sigprocmask() function. This allows you to control when signals are delivered to the program.

// Block SIGINT temporarily
sigprocmask(SIG_BLOCK, &mask, NULL);

// Code section where SIGINT is blocked

// Unblock SIGINT
sigprocmask(SIG_UNBLOCK, &mask, NULL);

// Rest of the program code

In this code snippet, sigprocmask() is used with the SIG_BLOCK flag to block the signals in the mask temporarily. The code section between blocking and unblocking the signal will not receive the blocked signals. Subsequently, SIG_UNBLOCK is used to unblock the signals, allowing their delivery to the program.

5.3 Checking the current signal mask

To determine the current signal mask, you can use the sigprocmask() function with a NULL argument for the second parameter.

sigset_t currentMask;
sigprocmask(SIG_SETMASK, NULL, ¤tMask);

// Check the current signal mask using sigismember()
if (sigismember(¤tMask, SIGINT)) {
    printf("SIGINT is blocked.\n");
} else {
    printf("SIGINT is not blocked.\n");

Here, sigprocmask() with SIG_SETMASK sets the current mask to the current process’s signal mask. The sigismember() function is then used to check if a specific signal (e.g., SIGINT) is blocked or unblocked.

By using signal masks, you can have finer control over signal delivery in different sections of your code. This allows you to manage signal handling behavior and ensure signals are delivered at appropriate times, enhancing the responsiveness and stability of your Linux programs.

6. Conclusion

Signal handling in Linux plays a crucial role in managing exceptional conditions, controlling program execution, and facilitating interprocess communication. Understanding how to handle signals, such as SIGINT, is essential for developing robust and responsive Linux applications.

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button