Sending Signals to Linux Processes
In the Linux operating system, processes can communicate with each other and with the kernel using signals. Signals are a form of inter-process communication (IPC) that allow a process to notify another process of an event or condition. This can be useful for a variety of purposes, such as terminating a process, suspending a process, or triggering a specific action.
Understanding Signals
Signals in Linux are represented by integer values, and each signal has a specific meaning and purpose. Some common signals include:
SIGINT(Interrupt): Sent when a user interrupts a process (e.g., by pressing Ctrl+C).SIGTERM(Terminate): Sent to request the termination of a process.SIGKILL(Kill): Sent to unconditionally terminate a process.SIGSTOP(Stop): Sent to suspend the execution of a process.SIGCONT(Continue): Sent to resume the execution of a suspended process.
Processes can also define their own custom signals for specific purposes.
Sending Signals to Processes
There are several ways to send signals to Linux processes:
-
Using the
killcommand: Thekillcommand is a powerful tool for sending signals to processes. The basic syntax is:kill -<signal> <pid>For example, to send a
SIGTERMsignal to a process with PID 1234, you would use:kill -TERM 1234 -
Using the
kill()system call: In your own programs, you can use thekill()system call to send signals to other processes. The function prototype is:int kill(pid_t pid, int sig);Here,
pidis the process ID of the target process, andsigis the signal to be sent. -
Using the
raise()function: If you want to send a signal to the current process, you can use theraise()function:int raise(int sig);This will send the specified signal to the calling process.
Handling Signals in Your Programs
To handle signals in your own programs, you can use the signal() function to register a signal handler:
void (*signal(int sig, void (*handler)(int)))(int);
The signal() function takes two arguments: the signal number and a pointer to a signal handler function. The signal handler function will be called whenever the specified signal is received by the process.
Here's an example of a simple signal handler that catches the SIGINT signal (Ctrl+C):
#include <signal.h>
#include <stdio.h>
void signal_handler(int signum) {
printf("Caught signal %d, exiting...\n", signum);
exit(0);
}
int main() {
signal(SIGINT, signal_handler);
printf("Press Ctrl+C to exit...\n");
while (1) {
// Do some work
}
return 0;
}
In this example, when the user presses Ctrl+C, the signal_handler() function is called, which prints a message and exits the program.
Visualizing Signal Handling
Here's a Mermaid diagram that illustrates the process of sending and handling signals in Linux:
This diagram shows how a process can receive signals from the kernel or from a user command (like kill), and how the process can handle those signals using a signal handler function.
In summary, sending signals to Linux processes is a powerful way to communicate between processes and control their execution. By understanding how to use the kill command, the kill() system call, and signal handling in your own programs, you can build more robust and flexible Linux applications.
