From owner-freebsd-hackers@FreeBSD.ORG Tue May 2 18:15:17 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CDB2D16A4A5 for ; Tue, 2 May 2006 18:15:17 +0000 (UTC) (envelope-from deischen@freebsd.org) Received: from mail.ntplx.net (mail.ntplx.net [204.213.176.10]) by mx1.FreeBSD.org (Postfix) with ESMTP id B929543DDC for ; Tue, 2 May 2006 18:14:03 +0000 (GMT) (envelope-from deischen@freebsd.org) Received: from sea.ntplx.net (sea.ntplx.net [204.213.176.11]) by mail.ntplx.net (8.13.6/8.13.6/NETPLEX) with ESMTP id k42IE2Ov029318; Tue, 2 May 2006 14:14:02 -0400 (EDT) Date: Tue, 2 May 2006 14:14:02 -0400 (EDT) From: Daniel Eischen X-X-Sender: eischen@sea.ntplx.net To: Alin-Adrian Anton In-Reply-To: <44579DE0.1050207@spintech.ro> Message-ID: References: <4456A5B3.2010809@spintech.ro> <44579DE0.1050207@spintech.ro> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by AMaViS and Clam AntiVirus (mail.ntplx.net) Cc: freebsd-hackers@freebsd.org Subject: Re: which running thread gests the external signal X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Daniel Eischen List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 May 2006 18:15:18 -0000 On Tue, 2 May 2006, Alin-Adrian Anton wrote: > Daniel Eischen wrote: >> POSIX states any thread that is in sigwait() (with the specified >> signal in the wait mask), or has the signal unmasked (in the threads >> signal mask) can receive the signal. If you want a certain thread >> to receive a process-wide signal, then the only sure way (POSIX) to >> do that is to block the signal in all the threads with the exception >> of the thread that is to receive the signal. >> > > OK, I was able to delegate a single thread for handling all the signals, by > using sigprocmask to block all signals at the beggining, then using > pthread_sigmask to unblock the needed signals inside the delegated thread. > This seemed to be the cleanest way of doing it.. > > However, this is not fully clean: all the other threads should *ignore* the > signals, not *block* them. Blocking a signal means the signal will be queued > and the queue will eventually fill, and so on. In my scenario I get the > result without running into problems (because each thread seems to have it's > own signal queue), however it's not... "clean". The other threads need to > simply 'drop' the signals, not cause them to be queued forever (consider an > uptime of 1 year?). I don't know the impact, however I want it to be clean.. You are entirely confused. You should go back to the POSIX standard and get Dave Butenhof's Programming with POSIX Threads book. One and only one thread gets _a_ signal. Blocking a signal in a thread does not mean it is queued on that thread, unless the signal is sent specifically to that thread (using pthread_kill(tid, sig), not kill(pid, sig)). Read the part of the POSIX standard that talks about signals pending on the process. Signals pending on the process, stay pending (or queued if they are queued signals) until a thread either unblocks the signal, calls sigwait() selecting that signal, or ignores the signal (using sigaction()). A signal handler runs in the context of the thread that is receiving (handling) the signal. > So ... would it be a way to ignore the signals from all the other threads > except the delegated one for handling them? (I'm sorry, I don't notice it, > even if it's obvious) > > Thanks for the advices and the tips, it's been really usefull. > > PS: Without using sigprocmask and pthread_sigmask, one random thread is > stopped in order for the signal handler to execute. Doesn't this mean that > the other threads are not 'seeing' the signal? In order to force a thread to > receive/see the signal, I need to block the signal inside all the other > threads (either with sigprocmask in main, or with pthread_sigmask). On Linux, > the signal gets delivered to all the running threads, unless specifically > blocked :). And I think that conformes to your mentioning of POSIX standards. No, if Linux delivers one signal to multiple threads, then it is entirely wrong and not compliant with POSIX behavior. I think Linux' NPT threads (or whatever they are called) have correct behavior. -- DE