Something like this might work (assuming *nix).
Code:
#define _BSD_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
struct timespec ts1, ts2;
clock_gettime(CLOCK_REALTIME, &ts1);
pid_t pid = fork();
if (pid == (pid_t)-1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // child
execlp("./aprog", "aprog", "afile", (const char *)NULL);
perror("exec");
exit(EXIT_FAILURE);
}
// parent
int status = 0;
wait(&status);
clock_gettime(CLOCK_REALTIME, &ts2);
if (ts2.tv_nsec < ts1.tv_nsec) {
ts2.tv_nsec += 1000000000;
ts2.tv_sec--;
}
printf("%ld.%09ld\n", (long)(ts2.tv_sec - ts1.tv_sec),
ts2.tv_nsec - ts1.tv_nsec);
return 0;
}
This is also timing the fork and exec, but I don't see a way around that.