Is there a function like WaitNamedPipe or a way to realize this on C++/linux? (so the process is not blocking on the pipe for infinite time)
I have a named pipe in my C++ program. A childprocess writes a value in it and the parent process reads it. I created the pipe by mkfifo and all operations are blocking (fifo cannot be opened for reading before it is tried to open for writing and v.v. unfortunately sometimes my childprocess does开发者_如何学Go not stop because of an error in a program the childprocess runs. It is not my task to fix this error in this external program but I want to make sure that the parent process does not stop for infinite time on the blocked open fifo call, but goes on after some time (without reading the value in the pipe).
So what I need it somethings like the WaitNamedPipe function. This function waits until either a time-out interval elapses or an instance of the specified named pipe is available for connection. http://ist.marshall.edu/ist480acp/namedpipes.html#WaitNamedPipe
Another way to realize this of course also works. I tried it with a loop in the parent process in which it always tries to open the pipe for reading and then sleeps if open is not possible. That seems to have no effect, probably because the parent process is blocking on the first open call.
Thanks for any help.
You want O_NONBLOCK
in your open(2)
flags, see fifo(7)
. Then use select(2)
or poll(2)
to wait for input (with a timeout).
You can use a non-blocking pipe and select()
call with a timeout. Or you can use a blocking read()
call having called alarm()
before it.
精彩评论