Voting

: nine minus five?
(Example: nine)

The Note You're Voting On

Enric Jaen
17 years ago
A way to have a non-blocking pipe reader is to check first if the pipe exists. If so, then read from the pipe, otherwise do other stuff. This will work assuming that the writer creates the pipe, writes on it, and after that deletes the pipe.

This is a blocking writer:

<?php
$pipe
="/tmp/pipe";
$mode=0600;
if(!
file_exists($pipe)) {
// create the pipe
umask(0);
posix_mkfifo($pipe,$mode);
}
$f = fopen($pipe,"w");
fwrite($f,"hello"); //block until there is a reader
unlink($pipe); //delete pipe

?>

And this is the non-blocking reader:

<?php
$pipe
="/tmp/pipe";
if(!
file_exists($pipe)) {
echo
"I am not blocked!";
}
else {
//block and read from the pipe
$f = fopen($pipe,"r");
echo
fread($f,10);
}
?>

<< Back to user notes page

To Top