Here is a possible solution to what - tech at kwur dot com- mentioned:
I faced the problem where i had a process (a server) that needed to take care of socket connection, and in the meanwhile get some data from the database. I didn't wanted to make the clients wait for the query execution time, so i decided to make a separate process that executes the query on the DB, and the two would communicate over a pipe. Of course i didn't wanted the server blocking if no data was available. So what i come up with is to use stream_select() , and to overcome the mentioned problem, i would fork the process, open up the pipe for writing in the child, this way the parent won't block when it opens the pipe.
here is some code
<code>
<?php
if (pcntl_fork() == 0)
{
// kid
$file = fopen("JobsQueue","w");
sleep(1);
exit(0);
}
else
{
usleep(15); // make sure that the child executes first
$file = fopen("JobsQueue","r");
}
echo "opened the queue \n";
while (true)
{
$reders = array($file);
if (stream_select($reders,$writers=null,$except=null,0,15) < 1)
{
continue;
}
else
{
// read data from the fifo
$data = fread($file,1024);
echo $data;
}
sleep(1);
}
?>
</code>
posix_mkfifo
(PHP 4, PHP 5)
posix_mkfifo — Erzeugt eine "FIFO special"-Datei (named pipe)
Beschreibung
posix_mkfifo() erzeugt eine spezielle FIFO-Datei, die im Dateisystem existiert und als bidirektionaler Kommunikationsendpunkt für Prozesse agiert.
- Pfadname
-
Der Pfad zur FIFO-Datei.
- Modus
-
Der zweite Parameter, Modus , muss in oktaler Schreibweise angegeben werden (z.B. 0644). Die Zugriffsberechtigungen des neu erzeugten FIFO hängen auch von der Einstellung der aktuellen umask() ab. Die Zugriffsberechtigungen der erzeugten Datei sind (mode & ~umask).
Rückgabewerte
Gibt bei Erfolg TRUE zurück, im Fehlerfall FALSE.
Anmerkungen
Hinweis: Wenn Safe Mode aktiviert ist, überprüft PHP, ob die Dateien/Verzeichnisse die mit dem Skript bearbeitet werden sollen, die gleiche UID (Eigentümer) haben wie das Skript selbst.
posix_mkfifo
05-Apr-2008 10:11
20-Sep-2007 07:44
This is still not a solution: if I listen to commands on a pipe and output status on a separate pipe, PHP will block on both opens because something else has not already connected to this pipe. Because I can't do a low-level fcntl() to to set O_NONBLOCK or something like it, this always locks up and is really stupid. The only way I can get it to work is to spawn seperate subshells with system() and have them cat, or echo respectively and then the pipes work properly...usually? Its alot of trouble that we can't set the blocking on the open!!
16-Aug-2007 06:22
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);
}
?>
18-May-2007 11:12
Note (quoted from `man 7 pipe` on debian linux):
"On some systems (but not Linux), pipes are bidirectional: data can be transmitted in both directions between the pipe ends. According to POSIX.1-2001, pipes only need to be unidirectional. Portable applications should avoid reliance on bidirectional pipe semantics."
Linux pipes are NOT bidirectional.
Also, it appears to me that the use of fifo (named) pipes in php is pretty pointless as there appears to be NO way of determining whether opening (let alone reading) from it will block. stream_select SHOULD be able to accomplish this, unfortunatly you cannot get to this point because even trying to OPEN a pipe for read will block until there is a writer.
I even tried to use popen("cat $name_of_pipe", 'r'), and even it blocked until it was opened for write by another process.
