How to use sem_timedwait?

Goran Mekić meka at tilda.center
Wed Dec 14 07:43:07 UTC 2016


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20161214/2f152bb4/attachment.sig>


More information about the freebsd-hackers mailing list