#include #include #include #include #include #define N 1000 /* number of child processes */ /* return real time in seconds since some arbitrary point in the past */ double walltime() { static int ticks_per_second = 0; if (!ticks_per_second) { ticks_per_second = sysconf(_SC_CLK_TCK); } struct tms timebuf; /* times returns the number of real time ticks passed since some arbitrary point in the past */ return (double) times(&timebuf) / ticks_per_second; } volatile char global[1048576]; int main() { int pagesize = getpagesize(); for (size_t count = 0; count < sizeof(global) / pagesize; ++count) { double t0 = walltime(); for (int i = 1; i <= N; ++i) { pid_t child = fork(); if (child == -1) { perror("unable to fork"); exit(1); } if (child == 0) { /* child process */ for (size_t index = 0; index < count; ++index) { global[index*pagesize] = i; } _exit(i); } } /* parent process */ pid_t child; int stat; while ((child = wait(&stat)) >= 0) { /* nothing to be done */ } double t1 = walltime() - t0; printf("%d %.4lf\n", (int) count, t1*1000*1000 / N); } }