Day49 - signalfd + epoll (Graceful Shutdown)¶
Overview¶
In this lab, we integrate Linux signalfd into the epoll-based TCP server.
This allows the server to:
- Handle signals (SIGINT / SIGTERM) as file descriptor events
- Avoid traditional signal handlers
- Perform graceful shutdown inside the main event loop
Why signalfd?¶
Traditional signal handling:
Problems:
- asynchronous execution context
- limited safe APIs
- race conditions
- difficult to integrate with event-driven design
Core Concept¶
Instead of:
Key APIs¶
1. sigset_t¶
Defines which signals we want to handle.
2. sigprocmask()¶
Block signals so they are not handled by default behavior.
3. signalfd()¶
Creates a file descriptor that receives signal events.
4. read()¶
Reads signal events.
Important Behavior¶
- Signals must be blocked (
sigprocmask) before using signalfd - signalfd becomes readable when a signal is pending
- read() must be called to clear the event
- otherwise epoll will continuously trigger
Integration with epoll¶
We convert all event sources into epoll_item:
Event Flow¶
Ctrl+C (SIGINT)
↓
kernel queues signal
↓
signalfd becomes readable
↓
epoll_wait()
↓
read(signalfd)
↓
graceful shutdown
Graceful Shutdown Design¶
Main loop exits:
Resource Cleanup¶
All resources are released in order:
- client list
- epoll items
- timerfd
- signalfd
- epoll fd
- server socket
Comparison with timerfd¶
| Feature | timerfd | signalfd |
|---|---|---|
| Source | time | signal |
| Trigger | periodic | external event |
| read() | expiration count | signal info |
Summary¶
This lab completes the event-driven model:
- network I/O → socket
- timer → timerfd
- signal → signalfd
All unified under epoll.