Programming of inter-process communication in Linux 2----pipeline communication

Source: Internet
Author: User

First, no Named pipes

1, what is the pipeline

A pipeline is one-way, first-out, connecting the output of one process to the input of another process. One process (write process) writes data at the end of the pipeline, and another process (read process) reads the data at the head of the pipe.

2. Pipeline Creation

Nameless pipe: (only) communication between the parent and child processes.

Famous pipeline: Communication between any two processes in the same system.

Nameless pipes are created by the pipe () function;

int pipe (int filedis[2]);

When a pipeline is established, it creates two file descriptors: Filedis[0] for the read pipeline, Filedis[1] for the write pipeline.

Pipeline communication:

To close a pipe, simply close the file descriptor. You can use the close function to close each other.

Example: Create pipe Yu Close

int main ()

{

int pipe_fd[2];

if (pipe (PIPE_FD) <0)

{

printf ("Pipe Create error\n");

return-1;

}

else{

printf ("Pipe Create success\n")

}

Close (pipe_fd[0]);

Close (pipe_fd[1]);

}

3. Pipeline reading and writing

Pipelines are used for communication between different processes. Typically, you create a pipe first, and then you create a child process from the fork function that inherits the pipeline created by the parent process.

Note: pipe () must be called before the system calls fork (), otherwise the child process will not inherit the file descriptor.

Second, named pipe (FIFO)

No named pipes can be used only by parent-child processes; But named pipes, unrelated processes can also exchange data.

In essence: A named pipe is a file

1. Create

#include <sys/types.h>

#include <sys/stat.h>

int Mkfifo (const char* pathname,mode_t mode)

Pathname:fifo file name

Mode: Property s_irusr s_iwusr s_ixusr S_irwxu.

Once the FIFO is created, you can use the open close Read Write operation FIFO.

2. Operation

When FIFO is turned on, the non-blocking flag (O_nonblock) will have the following effects on later reads and writes:

1) No o_nonblock is used: The process will block when access requirements are not met. Attempting to read an empty FIFO will cause the process to block.

2) Use O_nonblock: Access requirements cannot be met without blocking, immediate error returned, errno is Enxio.

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.