Is there an easy way of clearing a pipe in C

喜欢而已 提交于 2020-01-24 06:37:26

问题


I have a pipe that all my child processes use, but before a child uses the pipe to talk to the parent I need to clear it so that the parent reads from it correctly. Is there a simple function in C to do this?


回答1:


The way to "clear" a pipe is to read from it until the buffer is empty. This doesn't help you. I am guessing that your real problem is that the parent might read data that is mixed from multiple clients. There are two easy solutions to your problem.

  1. Always write messages less than PIPE_BUF bytes long, and do this in a single call to write. This will ensure that writes to the pipe are atomic.

  2. Use a separate pipe for each child process. On the server side, either use threads or use nonblocking IO with select or poll. Equivalently, you could use a Unix domain socket, and have each client connect to the socket (this is really just a different way of creating the separate pipes).



来源:https://stackoverflow.com/questions/9441290/is-there-an-easy-way-of-clearing-a-pipe-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!