Junior — Middle
Explain what signals are in the Linux operating system and how to work with them.
sobes.tech AI
Answer from AI
In Linux, signals are a mechanism for asynchronous notification of processes about events (for example, interruption, termination of a child process, timeout).
Main points:
- Signals have numeric identifiers (e.g., SIGINT, SIGTERM).
- A process can handle a signal using a signal handler or ignore it.
- If a signal is not handled, the default action is applied (for example, process termination).
Example of handling SIGINT (Ctrl+C) in C:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void handler(int signum) {
printf("Received signal %d, terminating\n", signum);
_exit(0);
}
int main() {
signal(SIGINT, handler);
while(1) {
printf("Working...\n");
sleep(1);
}
return 0;
}
In shell, signals can be sent using the kill command by PID, for example, kill -SIGTERM 1234.