First, when there is no data to read
O_nonblock Disable:read Call blocking, the process suspends execution until the data arrives.
The O_nonblock enable:read call returns a -1,errno value of Eagain.
The sample program is as follows:
/*************************************************************************
> File name:process_.c
> Author:simba
> Mail:dameng34@163.com
> Created time:sat Feb 2013 02:34:02 PM CST
* * * /
#include <sys/types.h>
# Include<sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h
#define ERR_EXIT (m) \
Do {\
Perror (m), \
EXIT (exit_failure); \
} while (0)
int main (int argc, char *argv[])
{
int pipefd[2];
if (pipe (PIPEFD) = = 1)
Err_exit ("pipe error");
pid_t pid;
pid = fork ();
if (pid = = 1)
Err_exit ("fork Error");
if (PID = = 0)
{
Sleep (3);
Close (pipefd[0]);
WritE (pipefd[1], "Hello", 5);
Close (pipefd[1]);
Exit (exit_success);
}
Close (pipefd[1]);
Char buf[10] = {0};
INT flags = FCNTL (pipefd[0], F_GETFL);
Fcntl (pipefd[0], F_SETFL, Flags | O_nonblock); The o_nonblock
int ret = read (Pipefd[0], buf, 10) of the Enable FD;///default is disable FD O_nonblock
if (ret = 1)//parent process not will block, error returns
Err_exit ("read error");
printf ("buf=%s\n", buf);
return 0;
}
Deliberately sleep in the subprocess 3s, so that the parent process is scheduled to run, and the read-end file descriptor flag set to Non-blocking, that is, immediately error return, as follows.
simba@ubuntu:~/documents/code/linux_programming/apue/pipe$./pipe_block
Read Error:resource temporarily unavailable