Date: Wed, 14 Dec 2016 08:42:28 +0100 From: Goran =?utf-8?B?TWVracSH?= <meka@tilda.center> To: freebsd-hackers@freebsd.org Subject: How to use sem_timedwait? Message-ID: <20161214074228.zh6q5zya2gszw4g6@hal9000.meka.no-ip.org>
next in thread | raw e-mail | index | archive | help
--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 <stdio.h> #include <sys/types.h> #include <semaphore.h> #include <errno.h> #include <time.h> 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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20161214074228.zh6q5zya2gszw4g6>