From owner-freebsd-hackers@FreeBSD.ORG Sun Mar 30 18:39:09 2003 Return-Path: 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 6CD9E37B401 for ; Sun, 30 Mar 2003 18:39:08 -0800 (PST) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id D3EA443F75 for ; Sun, 30 Mar 2003 18:39:07 -0800 (PST) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.9/8.12.9) id h2V2cuVD058549; Sun, 30 Mar 2003 20:38:56 -0600 (CST) (envelope-from dan) Date: Sun, 30 Mar 2003 20:38:56 -0600 From: Dan Nelson To: Sean Hamilton Message-ID: <20030331023856.GL74971@dan.emsphone.com> References: <001101c2f71d$8d9e4fb0$0300000a@slugabed.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <001101c2f71d$8d9e4fb0$0300000a@slugabed.org> X-OS: FreeBSD 5.0-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.4i cc: hackers@freebsd.org Subject: Re: wait()/alarm() race condition X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Mar 2003 02:39:12 -0000 In the last episode (Mar 30), Sean Hamilton said: > [asked in comp.unix.programmer without much luck] > > I have a loop which calls wait(), and I want another function to be > called as close to once per minute as possible. Pseudo code: [snip] > My concern is there is a small possibility that the alarm signal is > delivered after the if() but before the wait. So it is possible that > this wait takes several minutes or longer. > > Moving the "if (alarmed)" above the wait() makes no difference, of course. > > Is there a better way of doing something like this? Ideally wait() has a > timeout parameter, but, no such luck. Just make sure your signal handler has the SA_RESTART flag unset (either via siginterrupt() if the handler was installed with signal(), or directly if the signal was installed with sigaction() ), and the signal will interrupt the wait() call. -- Dan Nelson dnelson@allantgroup.com