#include #include #include #include #include #define LOOPCOUNT 20000 int main(int argc, char** argv) { int forkcount = 0; struct timeval before, after; gettimeofday(&before, nullptr); // once to warm up call path gettimeofday(&before, nullptr); // once for real-sies while (1) { pid_t p = fork(); if (p > 0) { // parent int loc; p = wait(&loc); forkcount++; if (forkcount == LOOPCOUNT) break; } else { // child exit(EXIT_SUCCESS); } } gettimeofday(&after, nullptr); float diff_us = (after.tv_sec - before.tv_sec) * 1000000.0; diff_us += (after.tv_usec - before.tv_usec); diff_us /= LOOPCOUNT; std::cout << "microseconds per fork: " << diff_us << std::endl; return EXIT_SUCCESS; }