c - how to correctly wait for execve to finish? -


a c source code (compiled , running linux centos 6.3) has line:

execve(cmd, argv, envp); 

execve not return, want modify code know when finished. this:

if (child = fork()) {     waitpid(child, null, 0);     /*now know execve finished*/     exit(0); }  execve(cmd, argv, envp); 

when this, resulting program works 99% of time, exhibits strange errors.

is wrong above?? expect above code run precisely (except little slower) before. correct?

if want know background, modified code dash. execve call used run simple command, after dash has figured out string run. when modify precisely above (without running after waiting) , recompile , run programs under modified dash, of time run fine. however, recompilation of 1 particular kernel module called "biosutility" gives me error

cc1: error: unrecognized command line option "-mfentry" 

here's 1 possibility.

dash does, in fact, need know when child process terminates. must reap child (by waiting it) avoid filling process table zombies, , anyway cares exit status of process.

now, knows pid of process started was, , can use when wait figure out process terminated , therefore exit status.

but doing fork. dash thinks started process pid, say, 368. fork new child, pid 723. wait child, ignore status code. finally, process terminates successfully. dash notices process 368 terminated successfully. if didn't.

now suppose dash executing script like

do_something && do_something_else 

the programmer has specified shell shouldn't do_something_else if do_something failed. terrible things happen. or @ least mysterious things. yet, have hidden failure. dash cheerfully fires do_something_else. et voilĂ 

well, it's theory. have no idea, really, shows sort of thing can happen.

the bottom line dash has mechanism lets know when child processes have finished, , if want hook exit handling of child process, you'd better off figuring out how mechanism works can hook it. trying add own additional mechanism end in tears.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -