c - Need clarification regarding vfork use -


i want run child process earlier parent process. want use execv call child process, using vfork instead of fork.

but suppose execv fails , returns, want return non-zero value function in calling vfork.

something this,

int start_test() {   int err = 0;   pid_t pid;   pid = vfork();   if(pid == 0) {    execv(app, args);    err = -1;    _exit(1); } else if(pid > 0) {   //do else  } return err; } 

is above code proper, or should use other mechanism run child earlier parent?

on links, have read that, should not modify parent resource in child process created through vfork(i modifying err variable).

is above code proper?

no. err variable in child's memory, parent not see modification.

we should not modify parent resource in child process created through vfork (i modifying err variable).

the resource outdated. *nix systems in past tried play tricks fork()/exec() pair, optimizations have largely backfired, resulting in unexpected, hard reproduce problems. why vfork() removed recent versions of posix.

generally can make assumptions vfork() on modern systems support it: if child unexpected os, child upgraded vfork()ed normal fork()ed one.

for example on linux difference between fork() , vfork() in later case, more of child's data made lazy cow data. effect vfork() faster fork(), child sustain performance penalty if tries access yet-not-copied data, since yet duplicated parent. (notice subtle problem: parent can modify data. trigger cow , duplication of data between parent , child processes.)

i want use execv call child process, using vfork instead of fork.

the handling should equivalent regardless of vfork() vs fork(): child should return special exit code, , parent should normal waitpid() , check exit status.

otherwise, if want write portable application, not use vfork(): not part of the posix standard.

p.s. this article might of interest. if still want use vfork(), read scary official manpage.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -