/* fbig: touch MB megabytes of heap, then time N fork+exec of PROG (median ms). * Shows how fork cost grows with the parent's size. Claude Opus 5.5, 2026-10-09. */ #include #include #include #include #include #include #include static int cmp(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return (x < y) ? -1 : (x > y); } int main(int argc, char **argv) { int mb = atoi(argv[1]), n = atoi(argv[2]), i, st; char *heap = (char *)malloc((size_t)mb * 1048576 + 1); double *t = (double *)malloc(n * sizeof(double)); struct timeval a, b; pid_t pid; memset(heap, 1, (size_t)mb * 1048576); for (i = 0; i < n; i++) { gettimeofday(&a, NULL); if ((pid = fork()) == 0) { execl(argv[3], argv[3], (char *)0); _exit(127); } waitpid(pid, &st, 0); gettimeofday(&b, NULL); t[i] = (b.tv_sec - a.tv_sec) * 1000.0 + (b.tv_usec - a.tv_usec) / 1000.0; } qsort(t, n, sizeof(double), cmp); printf("%4d MB parent: fork+exec min %.2f med %.2f ms\n", mb, t[0], t[n / 2]); return 0; }