The pipeline of Linux system Programming (II) Pipe reading and writing rules and PIPE Capacity, pipe_buf

Source: Internet
Author: User
Tags exit sleep linux

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.