Course contentsShow
← Linux
Lesson 292 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Signal Handling and Process Control

Manage process lifecycles and asynchronous events using standardized POSIX signal protocols.

In this lesson, you will learn how Linux processes communicate asynchronously using POSIX signals to manage their lifecycle and respond to system events.

Understanding Asynchronous Signals

Processes in Linux do not always run in isolation; they often need to react to external events, like a user requesting a shutdown or a parent process checking on a child's status. Signals are the POSIX standard mechanism for this. Think of a signal as an "interrupt" or a "notification" sent to a process to tell it that something has occurred.

When a signal is sent to a process, the process temporarily pauses its current execution to handle the signal. Depending on how the program is written, it can perform one of three actions:

  1. Default Action: Perform the standard behavior, such as terminating the process (e.g., SIGKILL).
  2. Ignore: Simply disregard the signal.
  3. Catch: Execute a custom function (a "signal handler") to perform cleanup, such as closing open file descriptors or saving state before exiting.

Sending Signals via the Shell

You are likely familiar with using the kill command in your terminal. Despite the name, kill is a general utility for sending POSIX signals, not just for destroying processes.

For example, when you press Ctrl+C in your terminal, the shell sends a SIGINT (Interrupt) signal to the foreground process, asking it to stop. You can mimic this manually by finding a Process ID (PID) and sending a signal:

# Send a standard termination request (SIGTERM) to a process
kill -15 1234

# Forcefully kill a process if it refuses to stop (SIGKILL)
kill -9 1234

Using SIGTERM is preferred because it allows the process to perform a graceful shutdown—closing files, releasing locks, and cleaning up temporary data—whereas SIGKILL is a harsh command that forces the kernel to remove the process immediately, often leaving the filesystem in an inconsistent state. By understanding signals, you gain control over the lifecycle of your applications, ensuring they handle interruptions predictably according to POSIX standards.

Key Takeaway: Signals are the standard POSIX method for asynchronous process control, allowing you to influence process behavior gracefully using SIGTERM or forcefully using SIGKILL.