本文实例讲述了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
|
declare (ticks=1); function sigHandler( $signal ) { echo "a child exited\n" ; } pcntl_signal(SIGCHLD, sigHandler, false); echo "this is " . posix_getpid() . PHP_EOL; for ( $i =0; $i <3; $i ++) { $pid = pcntl_fork(); if ( $pid == -1) { echo 'fork failed ' . PHP_EOL; } else if ( $pid ) { } else { $pid = posix_getpid(); echo 'child ' . $pid . ' ' . time() . PHP_EOL; sleep(rand(2,5)); echo 'child ' . $pid . ' done ' . time() . PHP_EOL; exit (0); } } do { $pid = pcntl_wait( $status ); echo 'child quit ' . $pid . PHP_EOL; } while ( $pid > 0); echo 'parent done' . PHP_EOL; |
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/zhaozhi406/article/details/14379905