写入管道死锁程序
我的程序在使用管道时遇到问题。
我正在做的是使用管道和 fork/exec 将数据发送到另一个进程
我所拥有的是这样的:
//pipes are created up here if(fork() == 0) //child process { ... execlp(...); } else { ... fprintf(stderr, "Writing to pipe now\n"); write(pipe, buffer, BUFFER_SIZE); fprintf(stderr, "Wrote to pipe!"); ... }
这对于大多数消息来说效果很好,但是当消息非常大时,写入管道会死锁。
我认为管道可能已满,但我不知道如何清理它。我尝试使用 fsync 但没用。
谁能帮助我吗?
I am having a problem in my program that uses pipes.
What I am doing is using pipes along with fork/exec to send data to another process
What I have is something like this:
//pipes are created up here if(fork() == 0) //child process { ... execlp(...); } else { ... fprintf(stderr, "Writing to pipe now\n"); write(pipe, buffer, BUFFER_SIZE); fprintf(stderr, "Wrote to pipe!"); ... }
This works fine for most messages, but when the message is very large, the write into the pipe deadlocks.
I think the pipe might be full, but I do not know how to clear it. I tried using fsync but that didn't work.
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在写入过程中关闭管道的读取端。操作系统会将写入管道的数据保留在管道的缓冲区中,直到所有打开管道读取端的进程实际读取其中的内容。
You need to close the read end of the pipe in the process doing the writing. The OS will keep data written to the pipe in the pipe's buffer until all processes that have the read end of the pipe open actually read what's there.