|
|
|
|
|
by adrianratnapala
3636 days ago
|
|
Signals are the user-space equivalent of a hardware interrupt. It's true that almost any computer that does anything useful in the real world needs to be notified about that world using interrupt service routines (ISRs). But that doesn't mean user programs need interrupts. OSes can and do use interrupts as a cues to notify processes via very different abstractions. Heres one way: User Process 99:
read socket Foo.
Kernel:
socket Foo has no data yet,
move 99 from the "running" list to the "sleeping" list.
run process 86
User Process 86:
do stuff
<< HARDWARE INTERRUPT!>>
Kernel:
Notice that 99 is waiting for this packet (via socket Foo).
Copy network data into 99's read buffer.
Move 99 back to the "running" list.
Run some processes, soon enough 99 gets a turn.
User process 99:
Oh good! I have data.
This sort of interface is usually much more useful for user applications than having the equivalent of their own ISR becuase it doesn't just send a notification -- it also controls the flow of the main application in a sane way. Simple, non-interactive applications can do this kind of blocking I/O all day long.More responsive applications need some kind of event loop. I.e. instead of blocking on an concrete I/O resource, they block notification service which tells them what I/O is available. In very different ways, Windows messages and Unix select()/poll() both do this. The end result is usually a callback driven program. This is slightly similar to signals/ISRs (since signals are a kind of callback) -- but the game-changing difference is that the callbacks are only called when the application has voluntarily gone back to the event loop. |
|
The application can integrate signals with its event loop with the self-pipe trick, or it can use Linux-specific APIs to have signals delivered over a file descriptor.
http://man7.org/linux/man-pages/man2/signalfd.2.html
I'm not saying signals are fun, but I don't think you've proposed anything different/better.