IPC Inter-process communication pipeline

Source: Internet
Author: User

Pipe Pipes are the oldest form of communication between IPC processes in UNIX systems, and all UNIX systems provide such communication mechanisms.

The pipeline has the following two limitations:
1. Historically, pipelines are half-duplex, data can only flow in one direction, and some systems provide full-duplex piping.
2. Pipelines can only be used between processes that have a common ancestor.

Although the half-duplex pipeline has its own limitations, it is still the most commonly used form of IPC. Whenever you type a sequence of commands executed by the shell in a pipe line, the shell creates a separate process for each command, and then connects the standard output of the previous command process with the standard input of the latter command.

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

The pipe is created by the pipe function, parameter PIPEFD returns two file descriptors, Pipefd[0] is open for reading, pipefd[1] is open for writing, Pipefd[1] 's output is pipefd[0] input.

Pipelines in a single process are of little use, and the process of calling the pipe is usually called fork. For pipelines from the parent process to the child process, the parent process closes the read side of the pipe pipefd[0], and the child process closes the write end of the pipeline pipefd[1]. When reading a pipe that has been closed by a write end, after all data has been read, read returns 0 to indicate that the end of the file has been reached. If you write a pipe that has been closed at the read end, it generates a sigpipe signal. When writing a pipeline, the constant PIPE_BUF specifies the size of the pipe buffer in the kernel, which needs our attention.

The following example illustrates the simple use of pipelines.

#include <stdio.h>#include <stdlib.h>#include <unistd.h>intMain (void) {intNintfd[2];    pid_t pid; Char line[4096];if(Pipe(FD) <0) {printf("Pipe error\n");return-1; }if(PID =Fork()) <0) {printf("Fork Error");return-1; }Else if(PID >0) {Close(fd[0]);Write(fd[1],"pipe from parent\n", -); }Else{Close(fd[1]); n =Read(fd[0], line,4096);Write(Stdout_fileno, line, N); }Exit(0);}

example, the data "pipe from parent\n" is transferred from the parent process to the child process, and the child process writes the data to the standard output.

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

IPC Inter-process communication pipeline

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.