#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

static void terminate(const char *msg, int code)
{
    perror(msg);
    exit(code);
}

int main()
{
    int r;
    struct timespec duration, rem;
    struct timeval tv;

    duration.tv_sec = 3;
    duration.tv_nsec = 5e8;

    r = gettimeofday(&tv, NULL);
    if (r == -1)
        terminate("gettimeofday", 1);
    printf("%ld %ld\n", tv.tv_sec, tv.tv_usec);

    r = nanosleep(&duration, &rem);
    if (r == -1)
        terminate("nanosleep", 1);

    r = gettimeofday(&tv, NULL);
    if (r == -1)
        terminate("gettimeofday", 1);
    printf("%ld %ld\n", tv.tv_sec, tv.tv_usec);

    return 0;
}
