Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758003AbYFXFTB (ORCPT ); Tue, 24 Jun 2008 01:19:01 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1756106AbYFXFSu (ORCPT ); Tue, 24 Jun 2008 01:18:50 -0400 Received: from wx-out-0506.google.com ([66.249.82.226]:61701 "EHLO wx-out-0506.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755863AbYFXFSs (ORCPT ); Tue, 24 Jun 2008 01:18:48 -0400 DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=RRvENdTrvqnx70MxmX+Hn9QA5aiI5+E2ZY69AM28JHNxSXZ9iy2RvBG94snvCWJxk+ xH+ZGAKatiOZRKqjwvtEpNxbvwLeORUC0u87FQeIeEAnRRAuJZMPN5Kwqpn5DLoQkqbd /RpBf+fdqNz5x/8Jonj/1gMLG7M82jSkt+ehQ= Message-ID: Date: Tue, 24 Jun 2008 07:18:47 +0200 From: "Michael Kerrisk" To: "Bart Van Assche" Subject: Re: nanosleep() uses CLOCK_MONOTONIC, should be CLOCK_REALTIME? Cc: "Michael Kerrisk" , lkml , "Thomas Gleixner" , "john stultz" , "Ingo Molnar" , "Roman Zippel" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <485E00CD.9060503@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3354 Lines: 72 On Mon, Jun 23, 2008 at 1:56 PM, Bart Van Assche wrote: > On Mon, Jun 23, 2008 at 11:48 AM, Michael Kerrisk > wrote: >> On Mon, Jun 23, 2008 at 10:34 AM, Bart Van Assche >> wrote: >>> On Sun, Jun 22, 2008 at 9:35 AM, Michael Kerrisk wrote: >>>> The POSIX.1 specification of nanosleep() says: >>>> >>>> But, except for the case of being interrupted by a signal, the >>>> suspension time shall not be less than the time specified by >>>> rqtp, as measured by the system clock CLOCK_REALTIME. >>>> >>>> >>>> However, reading kernel/hrtimer.c:sys_nanosleep(), it appears that >>>> CLOCK_MONOTONIC is used. >>>> >>>> return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); >>>> >>>> Is there a reason to use CLOCK_MONOTONIC, instead of CLOCK_REALTIME? Is it >>>> intentional? If yes, then I should document this in the man-pages. If not, >>>> then it should be fixed. >>> >>> CLOCK_MONOTONIC works fine even if ntpd steps the clock forward or >>> backward, CLOCK_REALTIME not. So the man page should be fixed. >> >> Thanks for your reply, but I'm not quite convinced yet. The things >> is: the Solaris man page also says "CLOCK_REALTIME". (Of course that >> man page may just be parroting the standard.) Could there not be some >> reasonable semantics for a nanosleep() that was based on >> CLOCK_REALTIME? > > Sorry, but I don't think that a nanosleep() based on CLOCK_REALTIME > would have reasonable semantics. The first line of the description in > nanosleep()'s manpage says: > "nanosleep() delays the execution of the program for at least the > time specified in *req". So you really need CLOCK_MONOTONIC and not > CLOCK_REALTIME. I meant to add. It seems to me that there really could be reasonable semantics here: nanosleep() sleeps until the specified time has elapsed, as measured by CLOCK_REALTIME. That may mean, for example, that the sleep finishes "early" if CLOCK_REALTIME jumps forward for some reason. That seems perfectly reasonable as possible semantics for sleeping (i.e., we are interested in sleeping for an interval based on what the system understands the time to be in the "outside world"). Note that POSIX also specifies clcok_nanosleep(), which allows CLOCK_REALTIME and CLOCK_MONOTONIC, and is again explicit about what nanosleep() should be doing: APPLICATION USAGE Calling clock_nanosleep() with the value TIMER_ABSTIME not set in the flags argument and with a clock_id of CLOCK_REALTIME is equivalent to calling nanosleep() with the same rqtp and rmtp arguments. RATIONALE The nanosleep() function specifies that the system-wide clock CLOCK_REALTIME is used to measure the elapsed time for this time service. However, with the introduction of the monotonic clock CLOCK_MONOTONIC a new relative sleep function is needed to allow an application to take advantage of the special char- acteristics of this clock. Cheers, Michael -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/