linux - PHP forked process not exit completely and become zombie -
this question has answer here:
- php forking issue 2 answers
this simplified program.
the test.php daemon program, running there. forks process work on task. once finishing work, forked process exits. when forked process exits, becomes zombie.
how make forked process exit without becoming zombie?
#!/usr/bin/php <?php while (1) { sleep(1); $pid = pcntl_fork(); if (!$pid) { $mypid = getmypid(); sleep(5); print "pid=$mypid finish work \n"; exit(); } sleep(1); } // while ?> ./test.php ... daemon running ... $ ps -ef | grep mqp ubuntu 10084 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php ubuntu 10085 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php ubuntu 10074 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct> ubuntu 10075 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct>
in parent process need call pcntl_wait()
or pcntl_waitpid()
time time. these functions wait until child exits return id , status. resources used child way released os , child not become zombie.
the status of exited child can used pcntl_wexitstatus()
, other functions pcntl
extension find out how child ended execution (normal termination , exit code returned, terminated ^c, received signal didn't handle a.s.o.)
Comments
Post a Comment