How to calculate run time for fork() system call in C? -


i'm trying find run time of fork() system call. each child process needs exit, , parent needs wait() on each child before creating next. want use shell built-in command named time measure execution time of program.

i have code far, not sure if i'm doing right.

#include <sys/types.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h>  int global = 100;  int main(int argc, char** argv) {     int local = 5;      pid_t pid;      pid = fork();     if (pid < 0) {         fprintf(stderr, "error -- failed fork()");         return 1;     }      if (pid > 0) {          int child_ret;          waitpid(pid, &child_ret, 0);         printf("parent -- global: %i, local: %i\n", global, local);         printf("parent -- child exited code %i\n", child_ret);     } else {         global++;         local++;          printf("child -- global: %i, local: %i\n", global, local);         exit (0);     } } 

read first time(7) , consider using clock_gettime(2) & getrusage(2); use clock_gettime around fork (in parent process) if wanted measure time fork(2) syscall (it small - perhaps millisecond -, , not significant).

use errno (e.g. print strerror(errno) or use perror) in failure cases.

notice fork timing might depends upon size of virtual address space , resident set size. might use strace timing abilities; in practice fork quick enough (often, less 1 millisecond, , few dozens of microseconds; because using lazy copy-on-write techniques)

so fork quick, child execve take few more microseconds, program startup time in child significant (ld-linux(8) performing relocation before main of exec-ed program starts...). what exactly want measure?

in general, child process calls execve(2) takes time (and of course executed program may take arbitrarily long time finish, , might "never" finish if server like). perhaps matter time between before fork , first meaningful instruction in main of execve-d program in child process...

in fact, can start many processes; rule of thumb fork @ few hundred times per second, , if child quickly execve-ing quick program (like /bin/ls of few dozen files, or date). however, don't want have more dozen or 2 of runnable processes @ once (on laptop or desktop), , perhaps less 5 runnable processes...


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -