Linux uses nameless pipes to read and write files

Source: Internet
Author: User

1 What the pipeline is:

Pipes everyone knows that there are two ends, at which end is used to read one end to write, the output of the end as the input of the other end.

2 function prototypes

int pipe (int pipefd[2]);//The two ends of the argument represented separately

3 Example: One end of the pipeline as the writing of the other end as a read-father process implementation

1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMain ()6 {7 pid_t pid;8     inttemp;9     intpipedes[2];Ten     Chars[ -]="Letgo"; One     Chard[ -]; A     if(Pipe (pipedes)) ==-1) -     { -Perror ("Pipe"); the exit (exit_failure); -     } -      -     if((Pid=fork ()) ==-1) +     { -Perror ("Fork Error"); + exit (exit_failure); A}Else if(pid==0) at     { -         //printf ("sub-process writes data to the pipe Reader"); -printf"Sub-process writes data to the pipe read \ n"); -         if(Write (pipedes[1],s, -))==-1) -         { -Perror ("Write"); in exit (exit_failure); -}Else to         { +printf"the data written is%s\n.", s); - exit (exit_success); the         } *}Else if(pid>0) $     {Panax NotoginsengSleep2);//wait for the child process to end -printf"the parent process reads data from the pipeline \ n"); the         if(Read (pipedes[0],d, -))==-1) +         { APerror ("Read"); the exit (exit_failure); +         } -printf"The data read from the pipe read is%s\n.", d); $     } $      -     return 1; -}

Operation Result:

Linux uses nameless pipes to read and write files

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.