Is there an easy way of clearing a pipe in C
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.
Always write messages less than
PIPE_BUF
bytes long, and do this in a single call towrite
. This will ensure that writes to the pipe are atomic.Use a separate pipe for each child process. On the server side, either use threads or use nonblocking IO with
select
orpoll
. 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).