From owner-freebsd-hackers@freebsd.org Wed Dec 14 07:43:06 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F0CB0C76A1B for ; Wed, 14 Dec 2016 07:43:06 +0000 (UTC) (envelope-from meka@tilda.center) Received: from mail.tilda.center (mail.tilda.center [188.226.130.133]) by mx1.freebsd.org (Postfix) with ESMTP id BE6731C07 for ; Wed, 14 Dec 2016 07:43:06 +0000 (UTC) (envelope-from meka@tilda.center) Received: from hal9000.meka.no-ip.org (unknown [87.116.180.115]) by mail.tilda.center (Postfix) with ESMTPSA id 4C661614E8 for ; Wed, 14 Dec 2016 08:42:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=tilda.center; s=mail; t=1481701348; bh=tiltg+JLu/dxahssI3J6n11wQ5r8TslZWNBQkmZI5i8=; h=Date:From:To:Subject:From; b=tcv0UMdHaDhbnAFoZyKTx8CP8cSQyDLOJY2Ylrl4KFtZfr8wOg4MSlvjTmeu7lAhg r6JmYdmrDjDU5x4vxM9WwXXg2Au3ChLhj0U88gsFDnmzXf5B8v6NwUZpYhJ8zDYnL0 AzFfTrW4zmD3kKWzsIQnPU3d+ud2RB0E4MryhaHs= Date: Wed, 14 Dec 2016 08:42:28 +0100 From: Goran =?utf-8?B?TWVracSH?= To: freebsd-hackers@freebsd.org Subject: How to use sem_timedwait? Message-ID: <20161214074228.zh6q5zya2gszw4g6@hal9000.meka.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="w6urz3f5fqxdq7wx" Content-Disposition: inline User-Agent: NeoMutt/20161126 (1.7.1) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Dec 2016 07:43:07 -0000 --w6urz3f5fqxdq7wx Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Hello, I'm trying to understand how sem_timedwait() works. For that I assambled a little program: #include #include #include #include #include int main() { sem_t semaphore; struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 3; sem_init(&semaphore, 0, 0); int result = sem_timedwait(&semaphore, &ts); int error = errno; printf("The sem_timedwait() should have ended at %lds and %ldns\n", ts.tv_sec, ts.tv_nsec); clock_gettime(CLOCK_REALTIME, &ts); printf("The sem_timedwait() exited at %lds and %ldns with %d status\n", ts.tv_sec, ts.tv_nsec, result); clock_getres(CLOCK_REALTIME, &ts); printf("Clock resolution is %lds and %ldns\n", ts.tv_sec, ts.tv_nsec); if (result == -1) { switch (error) { case EINVAL: printf("The semaphore does not refer to valid structure\n"); break; case ETIMEDOUT: printf("Timeout occured\n"); break; case EINTR: printf("Interupted\n"); break; default: printf("Unknown error\n"); break; } } } Running it I get the following output: The sem_timedwait() should have ended at 1481700752s and 2050646ns The sem_timedwait() exited at 1481700752s and 3813780ns with -1 status Clock resolution is 0s and 1ns Timeout occured What I want to know is why the number of nanoseconds reported first and second time is not the same if the resolution reported on line 3 is 1ns? It doesn't change much if I use CLOCK_RELATIME_PRECISE. Thank you! Regards, meka --w6urz3f5fqxdq7wx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEE1WIFkXy2ZeMKjjKEWj1TknovrLYFAlhQ9+EACgkQWj1Tknov rLaQIA/+NAqFeNUcpW3k3tevvBxbg8o87fHtgzBfg4MR8cwT36bsaQtIC8KBRoh8 fRepid8Uq72pH+WiPw/NESTpYtAEDlILu4aoYxuUfsyC5YuCMdd01WPNtbP6BIsZ NhC59MQxpq/VKp7u2WrY34I/pX99/7zSremOuyjXfUrvQW2HVs47+ROFFB0Z3YGa rmlMFqZhnDAjUP9+5BfbpPdaHrjAFSMMRmRrf+mIosb0dbdFuf5kvnDSQF9ftip2 u/WrgA9a6TO1N98380YG95YcxbZTg73rM1ctyKVjg8g9RVWsebOflkEe6ZkCqR3B lYuNs3DUf4IRno7tz6xeDP8Dr8qRmeH1fqFtSSsO+I8NJBVwuk9wtKT6IR63dJEZ x0RVklRWI+n6g5zj/mpvUtt9vUPpTjqsaPv3Y1egqKPRNQJdrlTgVzEgx+M7XiNP 5KnGX/cjkhnefKhwUsKV0dimoEKfaMlfdOjY10kOTUddMcZVW8IT0hy6VhKhcj8E vUjltXxI352Y2bA5FMFs5XyLGS20PxWpiMt/oyYo4gXFiNpprL3RqIcuIXjKDNNE q/9LLjB+v93UvDMpzLl3nVoMXV4fO6h6UtIRBPdJB1H1DdqnKusMQegIB+E4KEGG tnjxR8SQp4nznG9qxMmravGUeKYs05couScC321TlEmpU81bnTk= =YaVL -----END PGP SIGNATURE----- --w6urz3f5fqxdq7wx--