Detailed description of pipe and dup2 in Linux

Source: Internet
Author: User
Tags function prototype

1, what is the pipeline

The pipe is half-duplex, the data can only flow in one direction, need to establish two pipelines when the two sides communicate, can only be used for parent-child process or sibling process (affinity process); Separate file system: Pipeline is a file for the process at both ends of the pipeline, but it is not an ordinary file , it does not belong to a file system, but to itself, constitutes a file system, and only exists in memory. Read and write data: a process that writes to the pipeline is read out by the process at the other end of the pipeline. The content that is written is added to the end of the pipe buffer every time, and the data is read from the head of the buffer each time.

2, the creation of pipelines

int pipe (int fd[2])

The function creates a pipeline with two ends in an intermediate process that does not make much sense in practice, typically after pipe () is created, and then fork () a subprocess, and then the communication between parent and child processes is implemented through the pipeline.

3, the pipe read and write rules

  The two ends of the pipe can be described by the description character Fd[0] and fd[1], and it is important to note that the ends of the pipe are fixed on the task. That is, one end can only be used for reading, represented by the description word fd[0], which is called the pipe reading end, and the other end can only be used for writing, by the description word fd[1] to be said to be the pipe write end.

4. Pipe function

  Header files: #include <unistd.h>

function prototype: int pipe (int fd[2])

function parameter: fd[2], the two file descriptor of the pipeline, after which the two file descriptors can be manipulated directly. Where Fd[0] is the read end, Fd[1] is the write end

Return value: Returns 0 successfully, otherwise returns-1

Read Fd[0]: Close (fd[1]); Read (Fd[0], buf, buf_size);

Write fd[1]: Close (fd[0]); Read (Fd[1], buf, strlen (BUF));

5. Example

  

1#include <unistd.h>2#include <stdio.h>3 4 intMain ()5 {6     intfd[2];7     Charbuf[1024x768];8 pid_t pid;9 Ten     if(Pipe (FD)! =0) One     { Aprintf"Pipe error\n"); -         return 0; -     } the  -PID =fork (); -  -     if(PID = =-1) +     { -printf"Fork error\n"); +         return 0; A     } at  -     Else if(PID >0) -     { -printf"This is the father process, where write a string to the pipe\n"); -         CharS[] ="hello! Write a string in the Father process\n"; -Close (fd[0]); inWrite (fd[1], S,sizeof(s));//write data to the pipeline -  to     } +     Else -     { theprintf"the the child process, where read a string from the pipe\n"); *Close (fd[1]); $Read (fd[0], buf,sizeof(BUF));//reading data from the pipelinePanax Notoginsengprintf"%s", buf); -     } theWaitpid (PID, NULL,0); +     return 0; A}
View Code

The output is:

The father process, here write a string to the pipe

The the child process, where read a string from the pipe

Hello! Write a string in the Father process

When the data in the pipeline is read, the pipeline is empty. After that, the read operation will be blocked by default, waiting for some data to be written. If you need to set to non-blocking, you can set the following:

Fcntl (Filedes[0], F_SETFL, O_nonblock);

Fcntl (Filedes[1], F_SETFL, O_nonblock);

6, Dup2 detailed

Used to copy an existing file descriptor so that two file descriptors point to the same file struct.

Which header file: #include <unistd.h>

function prototypes: int dup2 (int oldhandle, int newhandle);

Example or use the example above, as follows:

1#include <unistd.h>2#include <stdio.h>3 4 intMain ()5 {6     intfd[2];7     Charbuf[1024x768];8 pid_t pid;9     intNEWFD;Ten     if(Pipe (FD)! =0) One     { Aprintf"Pipe error\n"); -         return 0; -     } the  -PID =fork (); -  -     if(PID = =-1) +     { -printf"Fork error\n"); +         return 0; A     } at  -     Else if(PID >0) -     { -printf"This is the father process, where write a string to the pipe\n"); -         CharS[] ="hello! Write a string in the Father process\n"; -Close (fd[0]); inWrite (fd[1], S,sizeof(s));//write data to the pipeline -  to     } +     Else -     { theprintf"the the child process, where read a string from the pipe\n"); *Close (fd[1]); $Dup2 (fd[0], NEWFD);Panax NotoginsengRead (NEWFD, buf,sizeof(BUF));//reading data from the pipeline -printf"%s", buf); the     } +Waitpid (PID, NULL,0); A     return 0; the}
View Code

The result of the operation is still the same. Just call dup2 in 36 lines of code and copy fd[0] to NEWFD.


Detailed description of pipe and dup2 in Linux

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.