0

Why is this so darn confusing. PHP's fopen will not open a file for reading unless there is data in the pipe waiting to be read (according to this comment). The POSIX specification for C's open states that, depending on the state of the O_NONBLOCK flag, when opening a pipe for writing, open will either block the calling process or return an error if no process has the file open for reading. Is this difference reconcilable?

conartist6
  • 417
  • 4
  • 15
  • People elsewhere have suggested for this type of situation that a bash script be invoked to write useless data to the pipe that can be discarded once the open is accomplished. I currently have php opening and closing a C thread to do this read, but since the read is in a loop that will be run every second or so, I would like to try to do this The Right Way and not perform any very expensive operations like thread creation. – conartist6 Nov 01 '10 at 21:43
  • 1
    Does [this comment](http://nl.php.net/manual/en/function.posix-mkfifo.php#89642) help? (Essentially, doing a `fopen` with `r+` so you're a writer too...) – Wrikken Nov 02 '10 at 18:41
  • Unfortunately I still haven't been able to get this to work. Once I have the file open I use stream_select to loop until I have collected all my input. I would think stream select might never return since there is always a writer with an open handle to the stream handle, however the poster says that his stream select works normally. MY stream select has decided instead to NEVER enter its loop. `while(($ss = stream_select($r, $w = null, $e = null, 0, 5000)) !=0){read...}` – conartist6 Nov 03 '10 at 02:05
  • Actually I discovered by more thorough testing that the r+ approach doesn't work at all. – conartist6 Nov 07 '10 at 21:06

1 Answers1

0

No. Php should signal C that it is expecting a connection and then block for input. C should loop while the open call returns -1 and errno == 6, and then fdopen the descriptor. Enjoy.

conartist6
  • 417
  • 4
  • 15