-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_fork_socket.php
More file actions
50 lines (43 loc) · 939 Bytes
/
Copy pathio_fork_socket.php
File metadata and controls
50 lines (43 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
$pids = [];
$MAX_PROCESS = 3;
$pid = pcntl_fork();
if($pid < 0){
exit('fork fail');
}else if($pid > 0){
}else{
if(posix_setsid() == -1){
exit('could not detach from terminal');
}
$id = getmygid();
echo time().'master process, pid '.$id.'\n';
umask(0);
$socket = stream_socket_server('tcp://0.0.0.0:8000',$errno,$errstr);
for ($i = 0; $i < $MAX_PROCESS; $i++) {
start_worker_process();
}
while (1) {
foreach ($pids as $pid) {
if($pid){
$res = pcntl_waitpid($pid, $status,WNOHANG);
if($res == -1 || $res > 0){
echo time().'worker process'.$pid.'exit';
start_worker_process();
unset($pids[$pid]);
}
}
}
}
}
function start_worker_process(){
$pid = pcntl_fork();
if($pid){
$pids[] = $pid;
}else if($pid < 0){
exit('fork fail');
}else{
while (1) {
$conn = stream_socket_accept($socket,-1);
}
}
}