I am programming a http server. There is the main daemon spawning a bunch of listeners, which are threads or processes, depending on user settings. Upon creation of a listener, the socket descriptor is passed to it, and its job is just to listen for connections (duh). A semaphore is wrapping the call to listen as to avoid the thundering herd effect.
My problem is how to quit the server. In this situation, where the listeners are blocked on a semaphore, how does the daemon is going to tell them to close? The daemon can’t just kill them, maybe someone is responding to a request…
I want to keep the design as simple as possible, but I can’t find a solution to this problem.
Here are some ugly workaround:
- Set a timeout for the semaphore. Wake up. Should I close? No? Ok, back to sleep;
- Just kill them;
- Array of booleans in shared memory, meaning responding/blocked, the daemon kills accordingly. The best so far, but not so simple.
What do you say?
Thanks.
A clean way to solve this problem is to make each listener wait on two semaphores. The first one it the current one you now use, and a second one, that when become signaled, means it’s time to quit. I believe your system is linux since you used the term
daemon. The functionselectdoes just that – waits on multiple objects (file-descriptors like), and returns when one of them becomes signaled. You also know from the function which one got signaled, so here is your solution.On Windows the function is
WaitForMultipleObjects()