473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A problem with pcntl_fork

I'm having some issues with forking and defunct children. This is my code:

#!/usr/bin/php
<?php
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);

/*
* LISTEN FOR CONNECTIONS
*/
while(TRUE){
while($conn = stream_socket_accept($socket,-1)){
$pid = pcntl_fork();
if($pid==-1){
fwrite(STDOUT,"Fork failed. Exiting!\n");
exit;
}elseif($pid){
/*
* We are parent.
* Close connection
*/
fclose($conn);
}else{
fwrite($conn, "Hello!\n");
fwrite($conn, "I am PID: ".posix_getpid()."\n");
sleep(2);
fclose($conn);
exit;
}
}
}

function sig_handler($signo){
switch ($signo) {
case SIGTERM:
exit;
break;
case SIGCHLD:
exit;
break;
default:
// handle all other signals
}
}

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGCHLD, "sig_handler");
?>

When the child exits (after the 2 seconds sleep), it leaves a defunct
zombie running ([scriptname] <defunct>), and I can't get rid of it
unless I kill the parent script.

What I'm hoping to accomplish is removing the child completely on exit.
How can I do this?

I've read the manual on pcntl_fork on php.net (and all the comments),
but nothing seems to work. I know it's me doing something completely
wrong, I just can't figure out what.

I hope someone can point me in the right direction. :o)

Regards
Thomas
Nov 17 '06 #1
4 5629
Hi,

The OS keeps the zombie around in case the parent process eventually
decides to retrieve the child's exit status information. You can tell
the OS you're not interested and to not keep zombies around using this:

pcntl_signal(SIGCHLD, SIG_IGN);

You could also trap SIGCHLD and check the child's exit status:

function sigchld($signo) {
while (($pid = pcntl_waitpid(-1, $status, WNOHANG)) 0) {
echo "Child with PID $pid returned " .
pcntl_wexitstatus($status) . ".\n";
}
}

pcntl_signal(SIGCHLD, 'sigchld');

Thomas wrote:
I'm having some issues with forking and defunct children. This is my code:

#!/usr/bin/php
<?php
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);

/*
* LISTEN FOR CONNECTIONS
*/
while(TRUE){
while($conn = stream_socket_accept($socket,-1)){
$pid = pcntl_fork();
if($pid==-1){
fwrite(STDOUT,"Fork failed. Exiting!\n");
exit;
}elseif($pid){
/*
* We are parent.
* Close connection
*/
fclose($conn);
}else{
fwrite($conn, "Hello!\n");
fwrite($conn, "I am PID: ".posix_getpid()."\n");
sleep(2);
fclose($conn);
exit;
}
}
}

function sig_handler($signo){
switch ($signo) {
case SIGTERM:
exit;
break;
case SIGCHLD:
exit;
break;
default:
// handle all other signals
}
}

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGCHLD, "sig_handler");
?>

When the child exits (after the 2 seconds sleep), it leaves a defunct
zombie running ([scriptname] <defunct>), and I can't get rid of it
unless I kill the parent script.

What I'm hoping to accomplish is removing the child completely on exit.
How can I do this?

I've read the manual on pcntl_fork on php.net (and all the comments),
but nothing seems to work. I know it's me doing something completely
wrong, I just can't figure out what.

I hope someone can point me in the right direction. :o)

Regards
Thomas
Nov 19 '06 #2
Hi Peter,

I ended up with this solution:

function sig_handler($signo) {
switch($signo){
case SIGCHLD:
$child = pcntl_wait($status,WNOHANG);
if($child 0){
// Do something
}
}
}

pcntl_signal(SIGCHLD, "sig_handler");

It seems to work like a charm, but it looks a bit different from your
solutions. You sound like you know A LOT more about this than I do, so
perhaps I could trouble you for a short explanation on the differences
between the three solutions? :o)

From my chair you second solution looks like the most "correct" way to
go. Am I right?

Sincerely,
Thomas
petersprc wrote:
Hi,

The OS keeps the zombie around in case the parent process eventually
decides to retrieve the child's exit status information. You can tell
the OS you're not interested and to not keep zombies around using this:

pcntl_signal(SIGCHLD, SIG_IGN);

You could also trap SIGCHLD and check the child's exit status:

function sigchld($signo) {
while (($pid = pcntl_waitpid(-1, $status, WNOHANG)) 0) {
echo "Child with PID $pid returned " .
pcntl_wexitstatus($status) . ".\n";
}
}

pcntl_signal(SIGCHLD, 'sigchld');
Nov 19 '06 #3
Hi,

pcntl_wait is actually equivalent to calling pcntl_waitpid with -1, so
that will work no problem.

Thomas wrote:
Hi Peter,

I ended up with this solution:

function sig_handler($signo) {
switch($signo){
case SIGCHLD:
$child = pcntl_wait($status,WNOHANG);
if($child 0){
// Do something
}
}
}

pcntl_signal(SIGCHLD, "sig_handler");

It seems to work like a charm, but it looks a bit different from your
solutions. You sound like you know A LOT more about this than I do, so
perhaps I could trouble you for a short explanation on the differences
between the three solutions? :o)

From my chair you second solution looks like the most "correct" way to
go. Am I right?

Sincerely,
Thomas
petersprc wrote:
Hi,

The OS keeps the zombie around in case the parent process eventually
decides to retrieve the child's exit status information. You can tell
the OS you're not interested and to not keep zombies around using this:

pcntl_signal(SIGCHLD, SIG_IGN);

You could also trap SIGCHLD and check the child's exit status:

function sigchld($signo) {
while (($pid = pcntl_waitpid(-1, $status, WNOHANG)) 0) {
echo "Child with PID $pid returned " .
pcntl_wexitstatus($status) . ".\n";
}
}

pcntl_signal(SIGCHLD, 'sigchld');
Nov 19 '06 #4
Ok Peter.

Thanks a bunch for the help. IMHO this stuff is quite hard to understand
- it's nice when someone steps up and lends a hand. :o)

Regards,
Thomas
petersprc wrote:
Hi,

pcntl_wait is actually equivalent to calling pcntl_waitpid with -1, so
that will work no problem.
Nov 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Davis | last post by:
Hi, I have written a script that basically sends an email (using sockets). I want this script to be able to run multiple copies of itself so that it doesn't have to wait for one mail sending to...
2
by: yawnmoth | last post by:
php.net's docs on pcntl_fork aren't exactly clear on how to spawn multiple child processes... i tried something like this, but it didn't seem to work: <? $pid1 = pcntl_fork(); $pid2 =...
0
by: giuliano | last post by:
Hi, I'm trying to use pcntl_fork to speedup some checks I do on remote servers. The problem is the 2nd child is created only when the 1st has finished the execution of the whole inner for block. ...
1
by: Lisa Pearlson | last post by:
Hi, I wish to write a little server script. It receives binary data and sends it back. Communication goes back and forth, so using apache is not possible. My little script will be a shell...
3
by: cmarvel | last post by:
All: I have a script using php-cgi 4.3 module. The script attempts to use the pcntl_fork() call but gets the error message "call to undefined function: pcntl_fork()" instead. Anyone know what's...
0
by: David | last post by:
Hello, I'm trying to run a system call('ls' in this simple case) that is taking quite a long. I would like to fork the system call into a child process and waits display the output of the call...
17
Ganon11
by: Ganon11 | last post by:
Hey guys, OK, taking care of this beforehand; I AM a student in a university. This IS part of my homework, and (as a moderator), I'm doing my best to follow the posting guidelines I work so hard...
1
by: chri | last post by:
I've compiled php with the option --enable-pcntl but when i call the function "pcntl_fork()" i obtain the error: "Call to undefined function: pcntl_fork()" Where is the bug? Have i to modify...
4
by: gr4v3n | last post by:
Greetings. First of all I must apologyse for my english. I have used multi processing before in C but know I have a project that it would be usefull to use it in php. Here comes the problema I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.