August 23, 2025
This article explores signals in UNIX, focusing on real-time signals (RT-signals), their differences with normal signals, and how to send and handle them.
Introduction to Signals in UNIX
Signals are a mechanism in UNIX that allows processes to communicate with each other. Normal signals are limited, so real-time signals were created in POSIX.1b for more reliable communication.
Difference Between RT-Signals and Normal Signals
In normal signals, if multiple signals are sent before being handled, the kernel merges them and delivers only once. RT-signals, however, are queued individually in the kernel's sigqueue
structure, allowing each signal to be handled separately. This is useful when each signal carries important data.
struct task_struct
{
…
struct sigpending;
…
};
struct sigpending
{
sigset_t signal;
struct list_head list;
spinlock_t lock;
};
struct sigqueue
{
struct k_siginfo info;
struct list_head list;
struct task_struct * task;
};
Sending RT-Signals in Shell
Example:
$ kill -SIGRTMIN+3 {pid}
RT-Signal Range
You can use signals from SIGRTMIN
to SIGRTMAX
. Access them with SIGRTMIN+n
or SIGRTMAX-n
.
Sending RT-Signals with kill System Call
#include <signal.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
argc--; argv++;
int pid = atoi(argv[argc]);
kill(pid, SIGRTMIN + 3);
return 0;
}
Handling RT-Signals
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void handler(int signum)
{
printf("signal received\n");
exit(0);
}
int main()
{
signal(SIGRTMIN + 1, handler);
printf("pid : %d\n", getpid());
while(1)
pause();
}
You can send the signal with:
$ kill -SIGRTMIN+1 {pid}
Summary
Real-time signals allow sending multiple signals without losing any, making them useful when signal delivery and data are critical.
Check out a small kill command using the kill system call: GitHub - shellStone/kill
See also: signal(7), kill(2), signal.h