Linux programming | C — Signals

Did you hear before about signals? are you a little confused about how they work or how we can handle signals?

In this blog, we take a look at the concept of signals. We will try to describe how signals work in Linux and how to handle signals, We’ll go also over the basics of signals and I really hope this article helps you develop, or refine your skills. So, let’s get started!!

To keep things organized let's ask some questions and try to answer them one by one

  • What is a signal ?
  • Why do they exist ?
  • When are they delivered and by whom ?
  • What are the default actions of signals ?
  • What happens to a process when it receives a signal without handling it ?
  • What happens to a process when it receives a signal and handles it ?

what is a signal?

In daily life, when we walk on the road, the green light we see is a signal, which can remind us how to cross the road safely. For another example, the timetable issued by the school to each class at the beginning of the new semester is also a signal, which can remind students to go to the corresponding course at the appropriate time and place instead of wasting time… In fact, we ignore a lot of signals in life. It is the existence of these signals that make our lives convenient and orderly.
In summary, you will find what a signal is. The signal is when you see it, you know what it is, and you know what to do after seeing the signal. As for whether you comply or not, it is yourself The signal in the computer is no exception….

  • The signal in the computer

First, we can say that signals are software interrupts. A signal is a software notification to a process of an event, when any process receives any signal, the operating system will automatically know what to do with each signal.

Why do they exist?

Signals are used for communication between processes. If you start a new process, how can you communicate with it through your shell or any other program or process while it is running? A robust program needs to handle signals. A signal can be sent by Kernel or a Process

Different ways for the reception of a signal

When are they delivered and by whom ?

A signal is delivered when the process takes action based on that signal, the process must be running on a processor at the time of signal delivery.

Normally a signal is delivered to a process as soon as possible by the kernel. However, if the process has blocked the signal, it will remain pending until unblocked.

You can send various signals to commands/process and shell scripts using the `pkill` command, `kill` command, and `killall` command.

There also exists another function raise() that sends a signal to the calling process itself. The signal is sent as such that the signal is received by the process even before the raise function returns.

What are the essential facts about Linux signals?

There are 31 standard signals, numbered 1–31. Each signal is named as “SIG” followed by a suffix. Starting from version 2.2, the Linux kernel supports 33 different real-time signals. These have numbers 32–64 but programmers should instead use SIGRTMIN+n notation. Standard signals have specific purposes but the use of SIGUSR1 and SIGUSR2 can be defined by applications. Real-time signals are also defined by applications.

What are the default actions of signals?

Every signal has a default action associated with it. The default action for a signal is the action that a script or program performs when it receives a signal.

Some of the possible default actions are :

Terminate the process.

Ignore the signal.

Dump core. This creates a file called core containing the memory image of the process when it received the signal.

Stop the process.

Continue a stopped process.

What happens to a process when it receives a signal without handling it?

Each process has a list (bitmask) of currently blocked signals. When a signal is blocked, it is not delivered that is, no signal handling routine is called but remains pending. … The`sigsuspend()` system call suspends the calling process until a specified signal is received. which means unhandled signals are pending until they are handled.

What happens to a process when it receives a signal and handles it

If a process receives a signal, the process has a choice of action for that kind of signal. The process can ignore the signal, can specify a handler function, or accept the default action for that kind of signal.

If the specified action for the signal is ignored, then the signal is discarded immediately.

The program can register a handler function using functions such as signal or sigaction. This is called a handler catches the signal.

If the signal has not been neither handled nor ignored, its default action takes place.

We can handle the signal using the signal or sigaction function. Here we see how the simplest signal() function is used for handling signals.

int signal () (int signum, void (*func)(int))

So you can see that a signal handler is a function that accepts an integer argument but returns void. The signal handler can be registered with the kernel using the signal() function (described above) that accepts a particular signal number and signal handler function name

Here is a working example of signal handling in Linux through the signal() function :

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void sig_handler(int sig_num)
if(sig_num == SIGINT)
printf("\n Caught the SIGINT signal\n");
printf("\n Caught the signal number [%d]\n", sig_num);
int main(void)
signal(SIGINT, sig_handler);
return 0;

Here is the output of this program :

$ ./sig_example
Caught the SIGINT signal

So you can see that the program (sig_example) was executed and then the signal SIGINT is passed to it by pressing the Ctrl c key combination. As can be seen from the debug print in the output, the signal handler was executed as soon as the signal SIGINT was delivered to the process.

At the end of this article, I hope I helped to make things clear about signals in the C programming language, and now you have a better idea about signals process and kernel and what happens under the hood.

Thanks for reading! and if you are interested let’s connect ;)

LinkedIn :

Junior Blockchain developer, passionate about P2P Decentralized Networks and System programming & Algorithms.