Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux Common Signals (Interprocess Communication)

In this tutorial, we will discuss Linux signals, which are a form of interprocess communication. Signals allow processes to communicate and react to various events. We will cover some common signals, how to send them using the kill command, and how to handle signals in a simple Bash script.

1. Understanding Linux Signals

Signals are software interrupts delivered to a process by the operating system. They provide a way for processes to be notified of asynchronous events, such as user requests, process termination, or child process termination.

2. Common Linux Signals

Here are some common Linux signals:

  • SIGHUP (Signal 1): Hang up. This signal is sent when the controlling terminal is closed, or when the parent process dies. It is often used to tell a process to reload its configuration file.
  • SIGINT (Signal 2): Interrupt. This signal is sent when the user presses Ctrl+C in the terminal. It tells the process to stop running.
  • SIGQUIT (Signal 3): Quit. This signal is sent when the user presses Ctrl+\ in the terminal. It tells the process to terminate and produce a core dump.
  • SIGKILL (Signal 9): Kill. This signal forcefully terminates the process and cannot be caught or ignored by the process.
  • SIGTERM (Signal 15): Terminate. This is the default signal sent by the kill command. It asks the process to terminate gracefully, allowing it to perform cleanup operations before exiting.
  • SIGSTOP (Signal 17, 19, 23): Stop. This signal stops the process without terminating it, allowing it to be resumed later with the SIGCONT signal.
  • SIGCONT (Signal 18, 20, 26): Continue. This signal resumes a process previously stopped by the SIGSTOP signal.

3. Sending Signals with the kill Command

The kill command allows you to send signals to processes. To send a signal, you need to know the process ID (PID). You can find the PID using commands like ps, top, or pgrep.

Syntax: kill -SIGNAL PID

Examples:

  • Send the SIGTERM signal to the process with PID 1234:

    kill -15 1234
    
  • Send the SIGKILL signal to forcefully terminate the process with PID 1234:

    kill -9 1234
    
  • Send the SIGHUP signal to reload the configuration of the process with PID 1234:

    kill -1 1234
    

4. Handling Signals in a Bash Script

In a Bash script, you can use the trap command to catch and handle signals. Here's a simple example:

Create a Bash script named signal_handling.sh:

#!/bin/bash

# Define a signal handler function
handle_signal() {
  echo "Signal received: ${1}"
  exit 1
}

# Set up the signal handler for SIGINT and SIGTERM
trap 'handle_signal SIGINT' SIGINT
trap 'handle_signal SIGTERM' SIGTERM

# Infinite loop
while true; do
  echo "Running..."
  sleep 2
done

Make the script executable:

chmod +x signal_handling.sh

Run the script:

./signal_handling.sh

Now, if you press Ctrl+C or send a SIGTERM signal to the script, it will print the received signal and exit gracefully.

In conclusion, understanding Linux signals and how to use them is essential for managing processes and interprocess communication. Signals provide

  1. List of common signals in Unix/Linux:

    • Description: Common signals include SIGTERM for termination, SIGKILL for forced termination, SIGINT for interrupt, SIGHUP for hangup, and more. Use kill -l to list all signals.
    • Code:
      kill -l
      
  2. How to send signals between processes in Linux:

    • Description: The kill command is commonly used to send signals between processes. For example, to send SIGTERM:
    • Code:
      kill -15 PID
      
  3. Signal handling mechanisms in C programming:

    • Description: In C programming, signal handling is implemented using the signal function. Handlers can be defined to execute specific actions when a signal is received.
    • Code:
      #include <signal.h>
      
      void signal_handler(int signum) {
          // Handle the signal
      }
      
      int main() {
          signal(SIGTERM, signal_handler);
          // Rest of the program
          return 0;
      }
      
  4. Interpreting signal numbers and names in Linux:

    • Description: Signal numbers and names can be interpreted using the kill -l command. The strsignal function in C provides the name associated with a signal number.
    • Code:
      kill -l
      
  5. Examples of using signals for process control:

    • Description: Signals can be used for process control, such as terminating a process gracefully, handling interruptions, and reconfiguring processes. Example:
    • Code:
      // Example: Graceful termination
      void signal_handler(int signum) {
          if (signum == SIGTERM) {
              // Cleanup and exit gracefully
          }
      }
      
  6. Signal-based communication in Linux programming:

    • Description: Signal-based communication involves processes using signals to communicate specific events. It is often used for simple notifications and coordination between processes.
    • Code:
      // Example: Signaling between parent and child processes
      // Parent process
      kill(child_pid, SIGUSR1);
      
      // Child process
      void signal_handler(int signum) {
          if (signum == SIGUSR1) {
              // Handle the signal from the parent
          }
      }
      
  7. Troubleshooting signal-related issues in Linux:

    • Description: Troubleshooting signal-related issues involves checking signal handlers, ensuring proper signal handling, and analyzing system logs for any unexpected terminations.
    • Code:
      # Check logs for signal-related issues
      dmesg | grep -i signal