From owner-freebsd-hackers@FreeBSD.ORG Sun Mar 30 16:36:24 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 85AD237B401 for ; Sun, 30 Mar 2003 16:36:24 -0800 (PST) Received: from priv-edtnes09-hme0.telusplanet.net (outbound02.telus.net [199.185.220.221]) by mx1.FreeBSD.org (Postfix) with ESMTP id 94E0243F75 for ; Sun, 30 Mar 2003 16:36:23 -0800 (PST) (envelope-from sh@bel.bc.ca) Received: from REASON ([154.5.106.237]) by priv-edtnes09-hme0.telusplanet.netSMTP <20030331003623.DRLG26116.priv-edtnes09-hme0.telusplanet.net@REASON> for ; Sun, 30 Mar 2003 17:36:23 -0700 Message-ID: <001101c2f71d$8d9e4fb0$0300000a@slugabed.org> From: "Sean Hamilton" To: Date: Sun, 30 Mar 2003 16:36:21 -0800 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Subject: 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 00:36:25 -0000 [asked in comp.unix.programmer without much luck] Greetings, 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: int alarmed = 0; void handle_sigalrm (int sig) { alarmed = 1; } while (1) { alarmed = 0; alarm (60); while (1) { wait (...); if (alarmed || wait was interrupted) { break; } } /* minutely code */ } 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. sh