Assuming a pipe,
int pipe_fd[2];
pipe(pipe_fd);
We fork, and expect that one process will write into the pipe at an arbitrary time. In one of the processes, we want to be able to check the contents of the pipe without blocking.
i.e. While a typical read will block if nothing is present and the write end remains open. I want to go do other stuff and potentially even read a bit at a time, do some stuff, and then check back to see if there's more, a la:
close(pipe_fd[1]);
while(1){
if(/**Check pipe contents**/){
int present_chars = 0;
while( read(pipe_fd[0],&buffer[present_chars],1) != 0)
++present_chars;
//do something
}
else
//do something else
}
pipe()
function does not allow you to set non-blocking mode, you have to do that after the pipe is created withfcntl()
andF_GETFL
andF_SETFL
plusO_NONBLOCK
. – Pulverizepipe2
function, which allows you to request nonblocking mode at the same time the pipe is created. This function is not yet standard but has been accepted for inclusion in the next version of POSIX since it's also necessary for atomically setting the close-on-exec flag at the same time the pipe is created. – Diffident