如何创建多个子进程

0
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($socket);
socket_bind($socket,'0.0.0.0',8666) or die('error');
socket_listen($socket,10);
$sockets = ;
$sockets = $socket;
while(true) {
for ($i = 0; $i < 5; $i++) {
$pid = pcntl_fork();
echo $pid;
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
pcntl_wait($status); //等待上一个进程结束
} else {
$clients = $sockets;
socket_select($clients, $write, $except, NULL);
foreach ($clients as $n=>$v) {
if ($v == $socket) {
$client = socket_accept($socket);
$clients = $client;
}else{
$buf = socket_read($v, 1024);

if (preg_match('/sleep/i', $buf)) {
$html = 'HTTP/1.1 200 OK' . PHP_EOL
. 'Content-Type: text/html;charset=utf-8' . PHP_EOL . PHP_EOL;
socket_write($v, $html);
socket_write($v, "this is server,45434");
} else {
socket_write($v, "this is server");
}
unset($clients);
socket_close($v);
exit;//关闭子进程

}
}
}
}
}

socket_close($socket); 

这样创建完之后,只有一个子进程 

1552723872(1).png
已邀请:

blogdaren - 常年游走于 Linux、PHP、C、VIM 之间【http://www.phpcreeper.com】

赞同来自:

1、题示的写法,在创建了第一个子进程后,因为pcntl_wait系统调用会阻塞主进程。
2、进程部分参考代码:


<?php
for ($i = 0; $i < 5; $i++)
{
$pid = pcntl_fork();
echo $pid . PHP_EOL;
if($pid < 0) {
die('could not fork');
}elseif($pid > 0) {
}else{
while(1){sleep(1);}
exit;
}
}

while(1)
{
pcntl_wait($status);
}

wuxian

赞同来自:

感谢大神解答,但有疑惑,pcntl_wait($status); 我的写法是写在主进程执行的代码,也会阻塞子进程吗,

wuxian

赞同来自:

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($socket);
socket_bind($socket,'0.0.0.0',8666) or die('error');
socket_listen($socket,10);
$sockets = $socket;
while(true) {
for ($i = 0; $i < 5; $i++) {
$pid = pcntl_fork();
echo $pid;
if ($pid == -1) {
die('could not fork');
} else if ($pid) {


    } else {
$clients = $sockets;
socket_select($clients, $write, $except, NULL);
foreach ($clients as $n=>$v) {
if ($v == $socket) {
$client = socket_accept($socket);
$clients = $client;
}else{
$buf = socket_read($v, 1024);

if (preg_match('/sleep/i', $buf)) {
$html = 'HTTP/1.1 200 OK' . PHP_EOL
. 'Content-Type: text/html;charset=utf-8' . PHP_EOL . PHP_EOL;
socket_write($v, $html);
socket_write($v, "this is server,45434");
} else {
socket_write($v, "this is server");
}
unset($clients);
socket_close($v);
exit;//关闭子进程

}
}
}
}
while(1){
pcntl_wait($status); //等待上一个进程结束
}

}


socket_close($socket);
 这样改写之后,创建了不只5个进程 ,是5个子进程,但每个子进程又有子进程

要回复问题请先登录注册