Skip to content

Commit 254421a

Browse files
author
邵瑞娟
committed
进程间的管道通信
1 parent 9e691d2 commit 254421a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

5namePipe.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
// 定义管道路径与创建管道
3+
$pipe_path = '/tmp/test.pipe';
4+
5+
if (!file_exists($pipe_path)) {
6+
if (!posix_mkfifo($pipe_path, 0664)) {
7+
exit("create pipe error!");
8+
}
9+
}
10+
11+
$pid = pcntl_fork();
12+
if ($pid == 0) {
13+
// 子进程,向管道写数据
14+
$file = fopen($pipe_path, 'w');
15+
while (true) {
16+
fwrite($file, 'Hello world!');
17+
$rand = rand(1, 3);
18+
sleep($rand);
19+
}
20+
exit("child end");
21+
} else {
22+
// 从管道读数据
23+
$file = fopen($pipe_path, 'r');
24+
while (true) {
25+
$rel = fread($file, 20);
26+
echo "{$rel}\n";
27+
$rand = rand(1, 2);
28+
sleep($rand);
29+
}
30+
}

0 commit comments

Comments
 (0)