473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 5632
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.