Junior — Middle
How is a process signal sent to control the operating system?
sobes.tech AI
Answer from AI
In Unix/Linux-based operating systems, sending a signal to a process is done using the system call kill(). This call allows sending a specific signal (e.g., SIGINT, SIGTERM, SIGKILL) to a process by its process ID (PID).
Example usage in C:
#include <signal.h>
#include <unistd.h>
int main() {
pid_t pid = 1234; // PID of the process to send the signal to
int sig = SIGTERM; // termination signal
int result = kill(pid, sig);
if (result == 0) {
// signal successfully sent
} else {
// error sending signal
}
return 0;
}
Besides kill(), there are other methods, such as using the kill command in the terminal or the raise() function to send a signal to oneself. Signals are used for process control: interruption, stopping, resuming, termination, etc.