How to send signal to Linux process?

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:

  1. Using the kill command: The kill command is a powerful tool for sending signals to processes. The basic syntax is:

    kill -<signal> <pid>

    For example, to send a SIGTERM signal to a process with PID 1234, you would use:

    kill -TERM 1234
  2. Using the kill() system call: In your own programs, you can use the kill() system call to send signals to other processes. The function prototype is:

    int kill(pid_t pid, int sig);

    Here, pid is the process ID of the target process, and sig is the signal to be sent.

  3. Using the raise() function: If you want to send a signal to the current process, you can use the raise() 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:

graph TD A[Process] --> B[Kernel] B --> C[Signal Dispatcher] C --> D[Signal Handler] D --> A E[User] --> A[Process] E --> B[Kernel] B --> F[kill command] F --> C[Signal Dispatcher]

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.

0 Comments

no data
Be the first to share your comment!