/* * non_blocking.c -- slightly modified from APUE book by Muhammad Abdulla. */ #include #include #include #include #include #include char buf[1000 * 1000]; int main() { int ntowrite, nwrite; char *str; int val; ntowrite = read(STDIN_FILENO, buf, sizeof(buf)); fprintf(stderr, "read %d bytes\n", ntowrite); // set standard output as non-blocking val = fcntl(STDOUT_FILENO, F_GETFL, 0); if ( val < 0 ) { fprintf(stderr, "F_GETFL error.\n"); exit(EXIT_FAILURE); } val = fcntl(STDOUT_FILENO, F_SETFL, val | O_NONBLOCK); if ( val < 0 ) { fprintf(stderr, "F_SETFL error.\n"); exit(EXIT_FAILURE); } for ( str = buf; ntowrite > 0; ) { errno = 0; // clear the global errno variable nwrite = write ( STDOUT_FILENO, str, ntowrite); fprintf(stderr, "nwrite = %d, errno = %d\n", nwrite, errno); if ( nwrite > 0 ) { str += nwrite; ntowrite -= nwrite; } } exit(0); }