Linux fasync / SIGIO / eventfd Notes¶
Overview¶
This note summarizes Linux asynchronous notification mechanisms used with character device drivers.
Topics:
- blocking read
- poll / epoll
- fasync / SIGIO
- eventfd
- userspace event loop integration
1. Blocking Read Model¶
Traditional blocking read flow:
Characteristics:
- simple design
- one thread per blocking operation
- difficult to scale for many event sources
2. poll / epoll Model¶
poll/epoll provides fd-based event multiplexing.
Flow:
Advantages:
- scalable
- multiple fd integration
- deterministic event loop
- preferred Linux event-driven architecture
3. fasync / SIGIO¶
Linux supports asynchronous signal notification through:
Userspace enables async notification using:
Driver event flow:
Important:
- SIGIO is only a notification
- event data is still stored inside the driver queue
- userspace must still call read()
4. struct fasync_struct¶
Drivers maintain async listeners using:
Kernel helper:
Signal delivery:
Typical driver release cleanup:
5. Why Signal Handler Should Stay Minimal¶
Signal handlers execute asynchronously.
Unsafe operations inside signal handler may cause:
- deadlock
- reentrancy problems
- libc internal corruption
- race conditions
Avoid inside signal handler:
- printf()
- malloc()
- mutex
- complex parsing
Recommended pattern:
6. eventfd¶
eventfd is a lightweight kernel event counter object exposed as a file descriptor.
Create:
Write:
Read:
Characteristics:
- fd-based
- epoll compatible
- aggregation counter
- not a message queue
7. eventfd Counter Behavior¶
eventfd accumulates notifications.
Example:
Single read:
After read:
eventfd stores:
NOT:
8. SIGIO + eventfd Bridge¶
Recommended architecture:
Advantages:
- signal-safe
- epoll-friendly
- scalable event loop
- unified event dispatch
9. Linux Event Architecture Philosophy¶
Linux commonly converts event sources into file descriptors:
- socket
- timerfd
- signalfd
- eventfd
- char device
- pipe
This allows:
to manage heterogeneous event sources in a unified event loop.
10. Comparison Summary¶
| Mechanism | Purpose |
|---|---|
| wait queue | kernel sleep/wakeup |
| poll/epoll | fd event multiplexing |
| SIGIO | asynchronous signal notify |
| eventfd | fd-based event counter |
| POSIX MQ | message transport |
| pipe/socket | stream/message IPC |
11. Key Takeaways¶
- SIGIO is notification only
- event data still lives in driver queue
- eventfd is ideal for signal-safe wakeup
- epoll is the center of Linux event-driven architecture
- Linux prefers fd-based event aggregation