Linux interprocess communication-Named pipes

Source: Internet
Author: User

In the front we talked about one way of interprocess communication, anonymous pipelines.
We know that anonymous pipes can only be used between processes for parent-child relationships. So how can data be passed between processes without such a relationship?

1. What is a named pipe

An anonymous pipeline is a space that opens up the output and input file streams in the cache, and can only be used between processes in a parent-child relationship. Because the input and output file descriptors of the parent-child process are consistent.
A named pipe is a real-world FIFO file, called a "pipe file," that is used between different processes to open the same FIFO file between the named pipe processes for data transfer.
We can operate FIFO files just like normal files.
Different processes, referencing the same FIFO file for data transfer.

2. Create a named pipe
Mkfifo function: Create a named pipe

int Mkfifo (constchar *filename,mode_t mode);

FileName: Specify the name of the FIFO file
Mode: Specify read and Write permissions for the file

3. Accessing Named Pipes
There are four ways to open a FIFO file:

Open (constChar *filename,o_rdonly); open (constchar *filename,o_ rdonly| o_nonblock), open (constChar *filename,o_wronly), open (constChar *filename,o_wronly| O_nonblock);

It is important to note that FIFO files cannot be opened in O_RDWR mode .
Because such a process writes data that is read by the process, the FIFO is generally used only for one-way data transfer.

The second argument of the Open function, which indicates whether it is a read pipeline or a write pipeline.
O_nonblock indicates that the FIFO pipeline read/write is non-blocking and, by default, is blocked.
So what is blocking?
A process write mode must have another process open in read mode when the pipeline is opened;
Or read mode, there must be another process in write mode open, otherwise the open function of the process is blocked until the above relationship is satisfied.

Non-blocking means that the open function returns immediately, and if no other process is opened in read-only mode, open returns 1 and the FIFO is not opened.

4.FIFO Pipe Use Example
The following example has two programs, fifowrite.c and FIFOREAD.C, respectively, to write pipelines and read pipelines.
fifowrite.c a text file Data.txt, write to the pipeline.
FIFOREAD.C reads the data from the pipeline and writes it to the Dataformfifo.txt file.
The program uses the default blocking mode.
The sample code is as follows:

Fifowrite.c

#include <sys/types.h>#include<stdlib.h>#include<stdio.h>#include<fcntl.h>#include<limits.h>intMain () {Const Char*fifo_name ="/tmp/my_fifo"; intPIPE_FD =-1; intDATA_FD =-1; intres =0; Const intOpen_mode =o_wronly; Charbuffer[pipe_buf+1]; if(Access (FIFO_NAME,F_OK) ==-1) {res= Mkfifo (Fifo_name,0777); if(res!=0) {fprintf (stderr,"could not create fifo\n");        Exit (Exit_failure); }} printf ("process%d opening FIFO o_wronly\n", Getpid ()); PIPE_FD=open (Fifo_name,open_mode); DATA_FD= Open ("Data.txt", o_rdonly); printf ("process%d result%d\n", Getpid (), PIPE_FD); if(pipe_fd!=-1)    {        intBytes_read =0; Bytes_read=read (DATA_FD,BUFFER,PIPE_BUF);  while(bytes_read>0) {res=write (Pipe_fd,buffer,bytes_read); if(res==-1) {fprintf (stderr,"Write error\n");            Exit (Exit_failure); } bytes_read=read (DATA_FD,BUFFER,PIPE_BUF); Buffer[bytes_read]=' /';        } close (PIPE_FD);    Close (DATA_FD); }    Else{exit (exit_failure); } printf ("Process%d finished.\n", Getpid ()); Exit (exit_success);}

Fiforead.c

#include <stdlib.h>#include<stdio.h>#include<sys/types.h>#include<fcntl.h>#include<limits.h>intMain () {Const Char*fifo_name ="/tmp/my_fifo"; intPIPE_FD =-1; intDATA_FD =-1; intres =0; intOpen_mode =o_rdonly; Charbuffer[pipe_buf+1]; intBytes_read =0; intBytes_write =0; memset (Buffer,' /',sizeof(buffer)); printf ("process%d opening FIFO o_rdonly\n", Getpid ()); PIPE_FD=open (Fifo_name,open_mode); DATA_FD= Open ("Dataformfifo.txt", o_wronly| O_creat,0644); printf ("process%d result%d\n", Getpid (), PIPE_FD); if(pipe_fd!=-1)    {         Do{res=read (PIPE_FD,BUFFER,PIPE_BUF); Bytes_write=write (data_fd,buffer,res); Bytes_read+=Res; } while(res>0);        Close (PIPE_FD);    Close (DATA_FD); }    Else{exit (exit_failure); } printf ("process%d finished,%d bytes read\n", Getpid (), bytes_read); Exit (exit_success);}

Output Result:

We enter commands in the Shell ls-l/tmp/my_fifo View the properties of the FIFO pipeline file

As you can see, theFIFO file is generated, the first character ' P ', which indicates that the file is a pipe file .

5. Simultaneous write pipeline for multiple processes
When multiple processes write pipelines at the same time, the data obtained by the read pipeline is cluttered.
At this point, we can control each process, write the pipeline when the data to be written exceeds a certain size, and open the FIFO in a blocking manner. Ensure the atomicity of the write operation.

Linux interprocess communication-Named 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.