#include #include #include #include #include #include #define NUM_GROWTHS 100 #define FORK_COUNT 10000 static double timespec_to_nanos(struct timespec *ts) { double nanos = ts->tv_sec * 1e9; nanos += ts->tv_nsec; return nanos; } static double delta_timespec_ms(struct timespec *before, struct timespec *after) { return (timespec_to_nanos(after) - timespec_to_nanos(before)) / 1e6; } static void time_forks(void) { struct timespec before, after; int i; assert(clock_gettime(CLOCK_MONOTONIC, &before) == 0); for(i = 0; i < FORK_COUNT; i++) { pid_t child; int status; child = fork(); assert(child != -1); if(child) { waitpid(child, &status, 0); } else { exit(0); } } assert(clock_gettime(CLOCK_MONOTONIC, &after) == 0); printf("%.5f\n", delta_timespec_ms(&before, &after) / FORK_COUNT); fflush(stdout); /* flush so that the child process doesn't get a copy of buffered output */ } int main(void) { int i; for(i = 0; i < NUM_GROWTHS; i++) { time_forks(); assert(malloc(1024 * 1024)); } return 0; }