-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.php
More file actions
68 lines (61 loc) · 1.24 KB
/
daemon.php
File metadata and controls
68 lines (61 loc) · 1.24 KB
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
<?php
function createProcess($time)
{
$parentPid = getmypid();
$pid = pcntl_fork();
if ($pid == -1) {
dir('fork process failed');
} else if ($pid == 0) {
$myPid = getmypid();
log_write('i am child my pid = ' . $myPid . chr(10));
work($time);
} else {
log_write('i am parent ->' . $parentPid . ' and child is ' . $pid);
exit(0);
}
}
function createWork()
{
for ($i = 0; $i < 2; $i++) {
$time = gettimeofday(true);
createProcess($time);
}
}
function signalHandler($signal)
{
switch ($signal) {
case SIGINT:
echo 'signal received' . chr(10);
break;
default:
break;
}
}
function log_write($contents)
{
file_put_contents('daemon.log', $contents, FILE_APPEND);
}
//pcntl_signal(SIGINT, 'signalHandler');
function work($time)
{
while (true) {
log_write($time . chr(10));
sleep(2);
}
}
function processHolder()
{
$pid = pcntl_waitpid(-1, $status, WNOHANG);
log_write($status . chr(10));
if ($pid > 0) {
createWork();
} else if ($pid === 0) {
#没有可用子进程
sleep(10);
}
unset($pid, $status);
}
createWork();
while (true) {
processHolder();
}