define(
"PC"
, 10);
define(
"TO"
, 4);
define(
"TS"
, 4);
if
(!function_exists(
'pcntl_fork'
)) {
die
(
"pcntl_fork not existing"
);
}
$sPipePath
=
"my_pipe."
.posix_getpid();
if
(!posix_mkfifo(
$sPipePath
, 0666)) {
die
(
"create pipe {$sPipePath} error"
);
}
for
(
$i
= 0;
$i
< PC; ++
$i
) {
$nPID
= pcntl_fork();
if
(
$nPID
== 0) {
sleep(rand(1,TS));
$oW
=
fopen
(
$sPipePath
,
'w'
);
fwrite(
$oW
,
$i
.
"\n"
);
fclose(
$oW
);
exit
(0);
}
}
$oR
=
fopen
(
$sPipePath
,
'r'
);
stream_set_blocking(
$oR
, FALSE);
$sData
=
''
;
$nLine
= 0;
$nStart
= time();
while
(
$nLine
< PC && (time() -
$nStart
) < TO) {
$sLine
=
fread
(
$oR
, 1024);
if
(
empty
(
$sLine
)) {
continue
;
}
echo
"current line: {$sLine}\n"
;
foreach
(
str_split
(
$sLine
)
as
$c
) {
if
(
"\n"
==
$c
) {
++
$nLine
;
}
}
$sData
.=
$sLine
;
}
echo
"Final line count:$nLine\n"
;
fclose(
$oR
);
unlink(
$sPipePath
);
$n
= 0;
while
(
$n
< PC) {
$nStatus
= -1;
$nPID
= pcntl_wait(
$nStatus
, WNOHANG);
if
(
$nPID
> 0) {
echo
"{$nPID} exit\n"
;
++
$n
;
}
}
$arr2
=
array
();
foreach
(
explode
(
"\n"
,
$sData
)
as
$i
) {
if
(
is_numeric
(trim(
$i
))) {
array_push
(
$arr2
,
$i
);
}
}
$arr2
=
array_unique
(
$arr2
);
if
(
count
(
$arr2
) == PC) {
echo
'ok'
;
}
else
{
echo
"error count "
.
count
(
$arr2
) .
"\n"
;
var_dump(
$arr2
);
}