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

#define LOOPS 100000000

static void loop(volatile unsigned int count) {
    while(count--) { }
}

static int times_dispatched(long *vol, long *invol) {
    struct rusage usage;
    int err;

    if ((err = getrusage(RUSAGE_SELF, &usage)) != 0) {
        return err;
    }

    *vol   = usage.ru_nvcsw;
    *invol = usage.ru_nivcsw;

    return 0;
}

int main(void) {
    long vol, invol;

    loop(LOOPS);

    if (times_dispatched(&vol, &invol) != 0) {
        fprintf(stderr, "Unable to get dispatch stats");
        exit(1);
    }
    printf("Context switches: %ld voluntarily, %ld involuntarily\n",
        vol, invol);

    return 0;
}
