-
Notifications
You must be signed in to change notification settings - Fork 8
/
socket.php
76 lines (70 loc) · 1.94 KB
/
socket.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Created by PhpStorm.
* User: sibenx
* Date: 16/11/16
* Time: 上午2:40
*/
function demo(array $phoneList){
$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$cnt = count($phoneList); //测试数组大小
$slice = 3; //需要调用的进程数量
$childList = [];
$i = 0;
while($slice >= 0)
{
$pid = pcntl_fork();
if($pid > 0){
$childList[$pid] = 1;
if($slice == 0)
{
while(true)
{
if(is_resource($sockets[1]))
{
$i++;
if($i >= $cnt){
fwrite($sockets[1], "c\n");
fclose($sockets[1]);
exit();
}else
{
fwrite($sockets[1], "{$i}\n");
}
}
}
}
}elseif($pid == 0){
while(true)
{
if(is_resource($sockets[0]))
{
$tmp = fgets($sockets[0]);
if(trim($tmp) == 'c')
{
echo "child-die\r\n";
fclose($sockets[0]);
exit();
}else
{
//这里发短信
printf("Slice id:%s,phone:%s \r\n",$slice,$phoneList[trim($tmp)]);
}
}
}
}else
{
//程序发生错误也需要关闭程序
exit();
}
$slice--;
}
// 等待所有子进程结束后回收资源
while(!empty($childList)){
$childPid = pcntl_wait($status);
if ($childPid > 0){
unset($childList[$childPid]);
}
}
}
demo(range(0,3));