Pipe of IPC

Source: Internet
Author: User

A pipeline is a way to allow only inter-process communication between relationships, and a pipe is created by the function pipe to read,write read and write operations.

       #include <unistd.h>       int pipe (int pipefd[2]);

Parameter Pipefd[2] Array returns open read-write descriptor, pipefd[0] is read, pipefd[1] is write.

First question: How does a file descriptor appear with a read-only, one that can only be written? Conjecture is opened 2 times for a file, one with read-only open, and one with write-only. Use FCNTL to verify the following:

       #include <unistd.h>       <fcntl.h>       int fcntl (intint/ **/ );
       F_GETFL (void)              is ignored.

When CMD is F_GETFL, the last parameter, ARG, is ignored. Test code:

1#include <sys/types.h>2#include <sys/stat.h>3#include <stdio.h>4#include <fcntl.h>5#include <signal.h>6#include <unistd.h>7#include <stdlib.h>8 9 intMainvoid)Ten { One     intflags; A     intfd[2]; -  -     if(Pipe (FD) <0) the     { -Perror ("Pipe Error"); -     } -  +Flags = FCNTL (fd[0], F_GETFL,0); -     if(Flags <0 ) +     { APerror ("Fcntl"); atClose (fd[0]); -Close (fd[1]); -     } -     Switch(Flags &O_accmode) -     { -          Caseo_rdonly: inprintf"Read only\n"); -              Break; to  +          Caseo_wronly: -printf"Write only\n"); the              Break; *  $          CaseO_rdwr:Panax Notoginsengprintf"Read write\n"); -              Break; the  +         default: Aprintf"Unknown access mode\n"); the     } +  -Flags = FCNTL (fd[1], F_GETFL,0); $     if(Flags <0 ) $     { -Perror ("Fcntl"); -Close (fd[0]); theClose (fd[1]); -     }Wuyi     Switch(Flags &O_accmode) the     { -          Caseo_rdonly: Wuprintf"Read only\n"); -              Break; About  $          Caseo_wronly: -printf"Write only\n"); -              Break; -  A          CaseO_rdwr: +printf"Read write\n"); the              Break; -  $         default: theprintf"Unknown access mode\n"); the     } theClose (fd[0]); theClose (fd[1]); -Exit0); in}
View Code

Operation Result:

Read Onlywrite only

Consistent with conjecture.

Flow of data:

As can be seen, the process can be written in pipefd[1], and then read in Pipefd[0, write their own read, this data flow is through. Verify:

1#include <sys/types.h>2#include <sys/stat.h>3#include <stdio.h>4#include <fcntl.h>5#include <signal.h>6#include <unistd.h>7#include <stdlib.h>8 9 #defineMAXLINE 4096Ten intMainvoid) One { A     intflags; -     intfd[2], N; -     CharBuf[maxline]; the     if(Pipe (FD) <0) -     { -Perror ("Pipe Error"); -     } +  -n = Write (fd[1],"Hello world\n", MAXLINE); +     if(N <0 ) A     { atPerror ("Write"); -         Gotoend; -     } -n = Read (fd[0],buf, n); -     if(N <0 ) -     { inPerror ("Read"); -         Gotoend; to     } +printf"read:%s\n", buf); -  the End: *Close (fd[0]); $Close (fd[1]);Panax NotoginsengExit0); -}
View Code

Output:

Read:hello World

Since it is interprocess communication, it is meaningless for pipelines to read and write in the same process, and it is common for pipelines to create a pipeline first, then fork, and the parent-child process shares the pipeline. Data flow:

In this way, there are 2 process operations on the write end of the pipeline and 2 process operations at the read end. But then there is a problem, assuming that the parent process reads, then this data is written in it itself? Or is the subprocess written in? cannot be distinguished. Usually a process closes its read, another process shuts down its write, so that the data flow is only one direction, and the data from who is obvious. :

Test code:

1#include <sys/types.h>2#include <sys/stat.h>3#include <stdio.h>4#include <fcntl.h>5#include <signal.h>6#include <unistd.h>7#include <stdlib.h>8#include <string.h>9 Ten #defineMAXLINE 4096 One  A intMainvoid) - { -     intN; the     intfd[2]; - pid_t pid; -     CharLine[maxline]; -  +     if(Pipe (FD) <0) -Perror ("Pipe Error"); +     if(PID = fork ()) <0) A     { atPerror ("Fork Error"); -     } -     Else if(PID >0)/*Parent*/ -     { -Close (fd[0]); -Write (fd[1],"Hello world\n", A); in     } -     Else                      /* Child*/ to     { +Close (fd[1]); -n = Read (fd[0], line, MAXLINE); the Write (Stdout_fileno, line, n); *     } $Exit0);Panax Notoginseng}
View Code

Results:

Hello World

Pipe of IPC

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.