I am thinking on a way of communication between signal handlers and the main program. Note, I use mosly non-blocking signals (a new signal can arrive even while a signal handler is executed), so I am looking for a lock-free solution.
My current idea is this:
- Creating a
socketpair(...)yet before the signal handler is installed (with
- The signal handler sends everything what I need to do, into a file descriptor of this socketpair.
- The main process polls the incoming data from the other file descriptor, and processes is as it is needed.
Of course I plan to set up a quite high internal buffer size (with
setSockOptInt(SOL_SOCKET, SO_SNDBUF, size);), and correctly terminate the program on an overflow (
send(...) in the signal handler returns some... unfeasible.
I think, possibly it might violate some Posix.* standards, because they restrict the signal-safe libc functions quite strongly. However, writing into sockets is, as far I know atomic (I am nearly 100% sure that it is the case if I use
send() to datagram sockets).
Is it correct? Could it work?