What Are UNIX Signals?
Signals are software interrupts that provide a way for processes to communicate with each other in UNIX-like operating systems. In Minitalk, we use two specific signals to transmit binary data:- SIGUSR1: User-defined signal 1 (represents binary
0) - SIGUSR2: User-defined signal 2 (represents binary
1)
These signals are called “user-defined” because they have no predefined meaning in the operating system. We can assign them any purpose we want, making them perfect for custom IPC protocols.
Signal Handler Implementation
The server registers signal handlers to respond to incoming signals. Here’s the actual implementation fromserver.c:38-39:
handle_sigusr function.”
The Signal Handler Function
Here’s the complete signal handler fromserver.c:16-32:
Understanding Static Variables
Understanding Static Variables
The
static keyword means these variables persist between function calls:c: Accumulates the bits of the current character being receivedi: Counts how many bits have been received (0-7)
static, these would reset to 0 every time the function is called, losing all progress.How Signals Enable IPC
The key to signal-based IPC is thekill() system call, which sends signals between processes. Despite its intimidating name, kill() is just the standard way to send signals.
Sending Signals from the Client
Fromclient.c:53-62, here’s how the client sends each bit:
The
kill() function takes two parameters:- pid: The process ID of the target process (the server)
- signal: Which signal to send (SIGUSR1 or SIGUSR2)
The Complete IPC Flow
- Server starts and registers signal handlers (
server.c:38-39) - Server enters pause loop (
server.c:40-43), waiting for signals - Client sends signal using
kill(pid, SIGUSR1)orkill(pid, SIGUSR2) - Operating system delivers signal to the server process
- Signal handler executes (
handle_sigusrfunction) - Server returns to pause, waiting for the next signal
Why Signals for Minitalk?
Advantages
- Lightweight: No need for pipes, sockets, or shared memory
- Simple: Just two signal types make the protocol easy to understand
- Educational: Teaches fundamental UNIX IPC concepts
Limitations
- Slow: Each signal requires a system call and context switch
- No data payload: Signals carry no data, only their type
- Unreliable: Signals can be lost if sent too quickly (hence the
usleep())
Key Takeaways
SIGUSR1 = Bit 0
When the client needs to send a 0 bit, it uses
kill(pid, SIGUSR1)SIGUSR2 = Bit 1
When the client needs to send a 1 bit, it uses
kill(pid, SIGUSR2)