Possible Duplicate:
Wake up thread blocked on accept() call
I am writing a small server which listening for connections (accepting them and passing them to worker threads) until a custom stop signal is sent.
If I use blocking sockets, then my main accept loop cannot break on the custom stop signal being sent. However, I wish to avoid having a busy-wait/spinlock loop with a non-blocking socket.
What I want is for my main accept loop to block until either a connection is received or the stop signal is sent.
Is this possible in C on Linux?
Many Thanks.
If I understand correctly then you are open to using any kind of “signal”, not necessarily a POSIX signal. Actually, POSIX signals are a poor choice because checking if you have received one in a loop has unavoidable race conditions.
What you need to use is anything that can be monitored through a file descriptor. It could be:
The later entries in the list of examples aren’t generally practical. They’re just to illustrate that it can be any type of object as long as it is has monitorable file descriptor. The simplest, cheapest, and most popular way is a pipe.
If you are already using nonblocking sockets, then you certainly already have some kind of polling loop to check when they’re ready to accept connections. I’m going to assume you’re using
poll()to do this.Before you start your loop, set up a pipe like this:
The
fcntlis to set the read end of the pipe to non blocking mode. You’re going to be using that end of the pipe in yourpollcall together with your socket, so it needs to be nonblocking.Then just add the read end of the pipe to the list of tile descriptors that you monitor in your accept loop:
The example uses
pollbut you might be usingselect(or evenepoll) instead.selecthas a different interface but the idea is the same.This technique is known as the self-pipe trick.