Junior — Middle
Selgitage, mis on signaalid Linuxi operatsioonisüsteemis ja kuidas nendega töötada
sobes.tech AI
Vastus AI-lt
In Linux, signals are a mechanism for asynchronous notification of processes about events (for example, interruption, child process termination, timeout).
Key points:
- Signals have numeric identifiers (e.g., SIGINT, SIGTERM).
- A process can handle a signal using a signal handler or ignore it.
- If the signal is not handled, the default action is applied (e.g., process termination).
Example of handling the SIGINT (Ctrl+C) signal 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;
}
Signals can be sent in shell using the kill command with the PID, for example kill -SIGTERM 1234.