我的 C++ 程序中有一个命名管道。子进程在其中写入一个值,父进程读取它。我通过 mkfifo 创建了管道,并且所有操作都是阻塞的(在尝试打开写入和 vv 之前,无法打开 fifo 进行读取)
不幸的是,有时我的子进程不会因为子进程运行的程序中的错误而停止。我的任务不是修复此外部程序中的此错误,但我想确保父进程不会在阻塞的打开 fifo 调用上无限期停止,而是在一段时间后继续运行(不读取管道中的值) )。
所以我需要的是类似 WaitNamedPipe 函数的东西。此函数将等待,直到超时间隔过去或指定的命名管道的实例可用于连接。 http://ist.marshall.edu/ist480acp/namedpipes.html#WaitNamedPipe
当然,实现这一点的另一种方法也是可行的。我在父进程中尝试了一个循环,其中它总是尝试打开管道进行读取,然后在无法打开的情况下休眠。
这似乎没有效果,可能是因为父进程在第一次打开调用时阻塞。
感谢您的任何帮助。
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 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.
发布评论
评论(2)
您希望在 O_NONBLOCK >
open(2)
标志,请参阅fifo(7)
。然后使用select(2)
或
投票(2)
等待输入(有超时)。You want
O_NONBLOCK
in youropen(2)
flags, seefifo(7)
. Then useselect(2)
orpoll(2)
to wait for input (with a timeout).您可以使用非阻塞管道和带有超时的
select()
调用。或者,您可以使用之前调用过alarm()
的阻塞read()
调用。You can use a non-blocking pipe and
select()
call with a timeout. Or you can use a blockingread()
call having calledalarm()
before it.