The pipeline for Linux process communication

Source: Internet
Author: User

A pipeline is one-way, first-out, which connects the output of one process with the input of another. One process (write process) writes data at the end of the pipeline, and another process (read process) reads the data from the head of the pipe. Once the data is read by a process, it will be removed from the pipeline and other read processes will no longer be able to read the data. The pipeline provides a simple flow control mechanism that the process will block when it tries to read the empty pipeline. Similarly, when the pipeline is full, the process attempts to write data to the pipeline, and the process blocks.
Pipelines include nameless pipes and two of known pipelines, and nameless pipes can only be used for communication between parent and child processes, while well-known pipelines may be used for communication between any two processes in the same system.

    • Nameless pipe created by the pipe () function
int pipe(int pipefd[2]);#当一个无名管道建立时,它会创建两个文件描述符:#pipefd[0]  用于读管道,#pipefd[1]  用于写管道。#pipe()函数创建的管道默认是打开的.

Sample program:

#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <errno.h>#include <stdlib.h>intMainintARGC, Char**argv) {intpipe_fd[2]; pid_t pid;if(Pipe(PIPE_FD) = =-1) {fprintf (stderr,"%s\ t \%s\%d\ n", Strerror (errno), __file__, __line__);Exit(-1); }if(PID =Fork()) ==0){Close(pipe_fd[0]);Write(pipe_fd[1],"Hello",6);Write(pipe_fd[1],"World",7);printf("Child process write done!\n");Close(pipe_fd[1]);Exit(0); }Else if(PID >0){Close(pipe_fd[1]);Sleep(3); Char buf[ -]; memset (BUF,0, -);Read(pipe_fd[0],buf, -);printf("Parent process in: %s\ n", buf);Close(pipe_fd[0]); }return 0;}The #对于无名管道的读, write, and close operations are the same as normal files. 
    • There are two ways to create a well-known pipeline
      1, use shell command, the format is as follows:
mkfifo[OPTION]FILENAME

2. Call the Mkfifo () function in the program

int mkfifo(constchar *pathname, mode_t mode);

The Mkfifo () function establishes a special FIFO file (named pipe) based on the parameter pathname, which must not exist, and the parameter mode is the permission of the file. Mkfifo () The FIFO file created by other processes can be accessed by means of read-write generic file. The FIFO file established by Mkfifo () is turned off by default, and when opening a file with the open () function, you need to be aware of the differences between ordinary files:
1, can not open the FIFO file in O_rdwr mode to read and write operations, the behavior is undefined. Because pipelines are unidirectional, if you need to pass data between programs in two directions, you can use a pair of well-known pipelines.
2, the use of the Mark O_nonblock mark.
There are four legal combinations of o_rdonly, o_wronly and O_nonblock symbols:
Flags=o_rdonly:open will call blocking unless another process opens the same FIFO in writing, otherwise it waits.
Flags=o_wronly:open will call blocking unless another process opens the same FIFO in the read mode, otherwise it waits.
flags=o_rdonly| O_nonblock: If no other process opens the FIFO in writing at this time, open also returns successfully, at which point the FIFO is read open without returning an error.
flags=o_wronly| O_nonblock: Returns immediately, if no other process is open at this time, open fails, and the FIFO is not opened, returning-1.

Sample program:

#fifo_read. C#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define Fifo_file "/tmp/myfifo"intMainintARGC, Char*ARGV[]){intFD; Char buf[ -];if(Access (FIFO_FILE,F_OK)! =0){if(Mkfifo (Fifo_file,0644) == -1) {fprintf (stderr,"%s\ t \%s\%d\ n", Strerror (errno), __file__, __line__);return-1; }    }if(FD =Open(Fifo_file,o_rdonly | O_nonblock)) = =-1) {fprintf (stderr,"%s\ t \%s\%d\ n", Strerror (errno), __file__, __line__);return-1; } while(1) {memset (buf,0, sizeof (BUF));if(Read(Fd,buf,sizeof (buf)) = =-1) {fprintf (stderr,"%s\ t \%s\%d\ n", Strerror (errno), __file__, __line__);return-1; }Sleep(1);printf("Read is %s !\n", buf); }unlink(Fifo_file);return 0;}
# fifo_write.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define Fifo_file "/tmp/myfifo"intMainintargcChar*argv[]) {intFD; if (fd = open (Fifo_file,o_wronly | O_nonblock)) = =-1) {fprintf (stderr,"%s\t%s\t%d\n", Strerror (errno), __file__,__line__);return-1; } if (Write (FD,"Hello World", A) == -1) {fprintf (stderr,"%s\t%s\t%d\n", Strerror (errno), __file__,__line__);return-1; }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The pipeline for Linux process communication

Related Article

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.