I wrote a Perl program that wags and calls a background program in a child process and has an infinite while loop that does some things in the parent process. Now the while loop should be left when the background program in the child process exits:
$pid = fork(); if ( !$pid ) { exec( "program args" ); } else { while ( 1 ) { # do something # needs help: if program terminated, break } }
Well, forkit gives your parent process a child PID (which you obediently get in your code). This is how a parent can keep track of a child.
fork
To leave the loop when it completes, you can use kill 0 $pidto check if the child still exists. See perldoc -f kill.
kill 0 $pid
perldoc -f kill
, CHLD, , . ( perl . perldoc perlipc).
perldoc perlipc
- else, .
... } else { $SIG{CHLD} = \&reaper } # hash to store exit status of child processes our %child; sub reaper { my $x; while (($x = waitpid(-1,WNOHANG)) >0) { $child{$x} = $? >> 8; } }
>
$SIG{CHLD}='IGNORE', , .
$SIG{CHLD}='IGNORE'
, , . posix waitpid() WNOHANG , , , (, ). waitpid(). perlipc.
I assume that exec () is blocking, so it won’t return before the program closes, but I could be wrong.
Source: https://habr.com/ru/post/1731180/More articles:Convert Java string encoding to web page - javaHow to allow your data structure to accept objects of any class - C ++ - c ++How to save a set in a TStringList object? - delphihttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1731178/set-focus-to-the-end-of-table-inside-of-div-when-adding-a-new-row&usg=ALkJrhh5mwkIiAFotqzzIkLzDyFBExzPxQImageButton selector not working? - androidHow to get a link to the current class from the class? - pythonCan I use alias field names in django templates? - djangoAn iterator for a custom container with derived classes - c ++Are absolute file paths allowed in mod_rewrite? - apacheC ++ - passing a std :: ostream function to a function - c ++All Articles