Skip to main content

Chapter 2.10 - Inter-Process Communication

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand inter-process communication (IPC) mechanisms
  • Use signals for communication
  • Understand pipes and redirections
  • Use sockets for network communication

What is IPC?​

IPC (Inter-Process Communication) refers to the mechanisms that allow processes to communicate and synchronize their actions.


Signals​

Signals are asynchronous notifications sent to a process.

Common signals:

  • SIGTERM (15): Graceful termination
  • SIGKILL (9): Forceful termination (cannot be ignored)
  • SIGSTOP (19): Suspend
  • SIGCONT (18): Resume
  • SIGHUP (1): Reload configuration
  • SIGINT (2): Interrupt (Ctrl+C)
# Send a signal
kill -SIGTERM PID
kill -15 PID
kill -9 PID # Force

# View available signals
kill -l

# Ignore a signal (in a script)
trap '' SIGINT

Pipes​

Pipes allow redirecting the output of one process to the input of another.

Anonymous Pipes​

# Simple pipe
command1 | command2

# Example
ps aux | grep apache
cat file.txt | sort | uniq

Named Pipes (FIFO)​

# Create a named pipe
mkfifo my_pipe

# Process 1 writes
echo "Hello" > my_pipe

# Process 2 reads
cat < my_pipe

Redirections​

Redirections allow controlling input/output:

# Output redirection
command > file # Overwrite
command >> file # Append

# Error redirection
command 2> error.log
command 2>&1 # Error to output

# Input redirection
command < file

# Here document
command << EOF
line 1
line 2
EOF

Sockets​

Sockets enable network communication between processes.

Unix Sockets (Local)​

# Create a Unix socket
nc -lU /tmp/my_socket

# Connect
nc -U /tmp/my_socket

TCP/IP Sockets (Network)​

# Server
nc -l 8080

# Client
nc localhost 8080

Shared Memory​

Shared memory allows multiple processes to access the same memory area.

Advantages:

  • Fast communication
  • No data copying
  • Efficient for large amounts

Tools:

  • shmget, shmat (System V)
  • /dev/shm (tmpfs)

Message Queues​

Message queues allow sending structured messages between processes.

Characteristics:

  • Typed messages
  • Queue
  • Optional persistence

Commands:

# View message queues
ipcs -q

# Remove
ipcrm -q ID

Semaphores​

Semaphores allow synchronization between processes.

Usage:

  • Control access to a resource
  • Avoid race conditions
  • Coordination between processes

Practical Examples​

Complex Pipeline​

# Find the 10 processes using the most CPU
ps aux --sort=-%cpu | head -11 | tail -10

# Count lines in multiple files
cat *.txt | wc -l

# Filter and format
grep ERROR /var/log/app.log | awk '{print $1, $2}' | sort

Communication with Signals​

# Script handling signals
#!/bin/bash
trap 'echo "Signal received, cleaning up..."; exit' SIGTERM SIGINT

while true; do
echo "Working..."
sleep 1
done

Summary​

In this chapter, you learned:

IPC: Inter-process communication mechanisms
Signals: Asynchronous notifications (SIGTERM, SIGKILL, etc.)
Pipes: Output/input redirection (|, >, <)
Sockets: Network communication (Unix, TCP/IP)
Shared Memory: Common memory access
Message Queues: Structured messages
Semaphores: Synchronization


Next Steps​

Module 3: File System
Module 4: Process Management


Chapter created on: December 2024