Given all the options to have processes or threads interact with each other (locks, mutex, semaphores, message queues, shared memory, etc), I'm a bit lost about what's best to do what I want.
I want several processes to wait for a certain event to happen. That is, I want them to block either until
At any time, there can be an arbitrary number of such waiting processes and when the wake-up event happens, all of them must wake up, not just one.
And the one restriction that probably makes this much harder is: It has to be PHP and it must also work from mod_php running in apache.
Well, it's a bit "hacky", but you could do this with sockets. It really depends on what you're trying to do... I really wonder if you actually need this kind of system (rather than trying to simplify the processes to where they don't need IPC at all)...
Create a "listener" deamon that does nothing but accept socket connections and put them in a queue. It would run socket_select
waiting for either a new connection, or data to be written to the sockets. If data was written, it writes that data to all of its active connections then closes them and starts over. If a new connection is received, it puts it in the queue and then goes back to selecting...
So then in your "child", all you need to do is connect to the master, set blocking socket_set_block($sock)
, and then set your timeout:
socket_set_option(
$sock,
SOL_SOCKET, // socket level
SO_SNDTIMEO, // timeout option
array(
"sec"=>10, // Timeout in seconds
"usec"=>0 // I assume timeout in microseconds
)
);
Then, simply read from the socket (socket_read($sock)
). It'll block for up to the timeout you set or until the "master" writes back to it. After passing that call, just close the socket and continue on doing what you want to do...