Comment by adrianmonk
1 month ago
Great question. That was my first idea, but I couldn't get it to work.
I tried with no signal handler, and the whole process (the parent) gets killed and a generic "Alarm clock" message is printed.
I tried with a (no-op) signal handler, and wait() keeps waiting.
"man 2 wait" does seem to say it should work. When I tried it, I installed my handler with signal(). Maybe with sigaction() it would work since "man 7 signal" says it should if I don't pass SA_RESTART.
[ Replying to myself since I'm past the edit window. ]
That was it. I tested it with sigaction(), and when SIGALRM is delivered, wait() returns immediately and sets errno to EINTR.
But if I pass the SA_RESTART flag to sigaction(), wait() keeps waiting just like when I used signal().
So yeah, it would work, and you can the same method for timeouts on lots of other system calls too.
Linux kernel signal syscall works without restart, but glibc wrapper for signal calls sigaction with SA_RESTART (BSD semantics). I had impression all wait functions are unconditionally interrupted, but wait(2) indeed respects SA_RESTART.