Pipeline of interprocess communication

Source: Internet
Author: User

Pipelines are similar to the way the file reads and writes, and is an older way of communicating between processes. The use of pipelines requires the creation of a pipeline file, through which the process communicates by reading and writing the file. Pipeline files do not take up disk space. You must have information in the pipeline to read within the pipe. Otherwise the IO is blocked. The program is blocked. And both ends of the pipe are opened to read or write, otherwise the IO is blocked. Individuals prefer to use signals and piping in conjunction with When the pipeline has been written to send a signal to the relevant process, when the relevant signal came to the pipeline after the file read. This effectively avoids the IO blockage which prevents the program from doing other operations.

Pipelines are divided into famous pipes (FIFO) and nameless pipes (pipe):

    1. General steps for piping use:
    2. Initialize the piping file system
    3. Registering and loading pipelines
    4. Create a read and write pipeline
    5. To read and write
    6. Close the pipe file after use ends

One, nameless pipe: can only be used between processes that have affinity.

Header Files <unistd.h>
int pipe (int filedes[2]);//Create nameless pipes (communication between parent and child processes)

    1. int p[2]; Pipe file Descriptor
    2. Char buf[16];
    3. Char buf0[16];
    4. pid_t pid;
    5. strcpy (buf, "Hello world!");
    6. Pipe (P);//Register pipe
    7. PID = fork ()
    8. if (PID > 0) {
    9. Write (p[1], buf, strlen (BUF)); Write to a pipeline
    10. Close the pipe file descriptor
    11. Close (p[1]);
    12. Close (p[2]);
    13. }
    14. if (PID = = 0) {
    15. Read (P[0], buf0, sizeof (BUF0));//reading the pipe
    16. printf ("%s\n", buf0);
    17. Close (p[0]);
    18. Close (p[1]);
    19. }
    20. if (PID < 0) {
    21. Exit (1);
    22. }

Second, the famous pipeline: Any process can communicate in this way

Header files <sys/types.h> <sys/stat.h>//establishing a well-known pipeline (non-process communication)
int Mkfifo (const char * pathname,int mode);
Create a special FIFO file based on pathname, mode is the file's permissions
Return value: 0 success, 1 failure

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9
#define FIFO_PATH "/home/linux/myfifo"//Piping file
11
12
int main (int arge,char *argv[])
14 {
FDW int = 0; Read File descriptor
int FDR = 0;//Write file descriptor
+ Char Cwrite[8] = {0};
18
if (arge! = 2) {
printf ("parameter input error \ n");
return-1;
22}
23
if (Mkfifo (fifo_path,0666) < 0 && errno! = eexist) {//Register pipeline file
printf ("Create error \ n");
Return-2;
}else{
FDW = open (Fifo_path, O_creat | o_wronly,0666);//Open the Write pipeline
FDR = Open (Argv[1],o_creat | o_rdonly,0666);//Open the Read pipeline
30//operation of the read/write pipeline
if (Fdw > 0) {
32
if (FDR > 0) {
while (read (fdr,cwrite,7) > 0) {
Write (fdw,cwrite,7);
memset (cwrite,0,sizeof (cwrite));
37}
38
}else{
("%s File open failed \ n", argv[1]);
return-3;
42}
43
}else{
printf ("Pipe open failed \ n");
return-4;
47}
48}
49//Close File descriptor
Close (FDW);
Wuyi Close (FDR);
52
return 0;
54}

Pipeline of interprocess communication

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.