Tuesday, October 14, 2008

Change pipe to non-blocking mode

The default pipe(...) is one blocking fd. If you need non-blocking IO, you can refer fowling sample code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(void) {
int fd[2], pipeAttr;
/*fd[0] for reader, fd[1] for writer*/
pipe(fd);
fnctl(fd[0], pipeAttr, F_GETFL);
pipeAttr |= O_NONBLOCK;
fnctl(fd[0], F_SETFL,
pipeAttr);
do_something(fd);
close(fd[0]);
close(fd[1]);
}
viod do_something(int fd[]) {
/*......*/
}

No comments: