Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-17-2004, 07:35 PM   #1
LQ Newbie
Registered: Jan 2004
Location: UK
Distribution: LFS
Posts: 15

Rep: Reputation: 0
multi-threading with signals


I'm writing a multi-threaded daemon which needs to handle the odd signal (such as SIGTERM and SIGHUP). After doing lots of research, I've found that Linux doesn't guarantee which thread an asynchronous signal goes to, so instead you have to wait for them synchronously and put your main accept() loop in another thread. The problem though, is that this signal-handling thread needs to interrupt the accept() call in the server thread with a signal when it gets a SIGTERM, and this presents a nasty race-condition with regard to child (client-handling) threads. In pseudo-code, it goes something like this:

main thread:
block SIGTERM, SIGHUP, etc
spawn server thread
while (sigwait)
send SIGINT to server thread // it's a daemon so SIGINT is unused
yada yada

server thread:
unblock SIGINT
while (accept)
if (errno == EINTR)
exit gracefully
spawn client thread

client thread:
block SIGINT
yada yada

Now, the problem is that the new client threads will inherit the sigset from the server thread, which when sent before the client gets a chance to block it, will mean it could end up missing the accept() call and killing the whole daemon un-gracefully.

Have I got this right? If so, is there a way to prevent it, (even if it means a bit of a re-design)?

Thanks for any help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
dlopen in multi-threading environnement yakotey Programming 2 06-30-2005 08:36 AM
ftp client with support for fxp, multi-threading and... NonSumPisces Linux - Networking 1 08-28-2004 01:18 PM
help! perl multi-threading? eph Programming 0 05-03-2004 09:15 PM
Multi-threading rch Programming 3 03-30-2003 10:27 PM
Multi threading Mohsen Programming 5 03-01-2003 11:13 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration