Process Communications-nameless pipes

Source: Internet
Author: User

Nameless pipe: Primarily for process communication (I feel it has a lot of limitations)

Features: It is not a file system and cannot be accessed by name, which is the biggest difference between it and the famous pipe. A nameless pipe is just something inside a system's memory.

Half-duplex mode, the data can only flow to One direction (the teacher gave us an example is the water plant water to the residents of water, it is not possible to flow backwards, right).

Communication between processes, but only affinity processes can communicate? For example, a parent-child process: Because in a parent-child process, the child process copies the data segment of the parent process, which allows the two processes to have a connected relationship when communicating.

Let's take a look: nameless pipe creation, write, read, close (I'm using the Liunx system CentOS)

    

    

This function is to create a pipeline that will build the return value of the successful function is 0, otherwise the failure is to return-1, and return the error number, errno.

When creating this nameless pipe, define an array with a size of 2, which is used to hold the handle (the file descriptor).

The nameless pipe has a read end, pipefd[0]---------------------read (fd,buf,sizeof (BUF)), and the Read function is the system's reading function, which reads the contents of the file into the BUF, Note that when communicating between processes using nameless pipes, we need to call the kernel functions of the system and not call the library functions.

     

The nameless pipe has a write end, pipefd[1]---------------------write (fd,buf,sizeof (BUF)), and the Write function is the writing function of the system, which writes the contents of the buf inside the file.

     

The next step is to close the read and write ends. Close (Fd[0]), close (fd[1]);

     

I wrote an example of a nameless pipe myself:

     

     

     

This program is a child process that writes data, and the parent process reads the data so that communication between processes is realized. The operating result is:

     

     

This is I write a very see of the program, I am also a novice, there must be a lot of shortcomings, but also ask you to mention good suggestions.

Process Communications-nameless pipes

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.