Pipelines and Named pipes

Source: Internet
Author: User

Named pipes (named pipe)

Because of the fork mechanism, pipelines can only be used between parent and child processes, or between two child processes that have the same ancestor (between processes that are related to each other). To solve this problem, Linux provides a FIFO way to connect the process. FIFO is also called named pipe (named pipe).

FIFO (first in, first out) is a special type of file that has a corresponding path in the file system. When a process opens the file in a read (R) manner and another process opens the file in a write (w), the kernel creates a pipeline between the two processes, so the FIFO is actually managed by the kernel and does not deal with the hard disk. The FIFO is called because the pipeline is essentially a FIFO queue data structure, the first to be put into the first read out, thus guaranteeing the order of information exchange. FIFO just borrows the file system, named pipe is a special type of file, because everything in Linux is a file, and it exists as a file name in the filesystem. ) to name the pipeline. The write-mode process writes to the FIFO file, while the read-mode process is read from the FIFO file. When the FIFO file is deleted, the pipe connection disappears as well. The advantage of FIFO is that we can identify the pipeline through the path of the file, so that there is no affinity between the processes to establish a connection

Function Prototypes:

#include <sys/types.h><sys/stat.h>int mkfifo (constChar *  FileName, mode_t mode); int mknode (constchar0 );

Where pathname is the name of the file being created,mode indicates the permission bit to be set on the file and the type of file that will be created (in this case , S_ififo). Dev is a value that is used when creating a special file for a device. Therefore, for FIFO files it has a value of 0.

#include <sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>intMainvoid){    Charbuf[ the]; intFD; Unlink ("Zieckey_fifo"); Mkfifo ("Zieckey_fifo",0777); if(Fork () >0)    {        Chars[]="hello!\n"; FD=open ("Zieckey_fifo", o_wronly); Write (Fd,s,sizeof(s)); //Close (FD);    }    Else{FD=open ("Zieckey_fifo", o_rdonly); Read (Fd,buf,sizeof(BUF)); printf ("themessagefromthepipeis:%s\n", BUF); //Close (FD);    }    return  0;}/*The execution result is themessagefromthepipeis:hello! and can be executed in the program directory generation pipeline file Zieckey_fifo*/                              

Note: The first character in the output of the LS command is P, which indicates that this is a pipe. The final | symbol is added by the-f option of the LS command, which also indicates that this is a pipeline.

FIFO read-write rules

1. Read data from FIFO: contract: If a process blocks open FIFO in order to read data from the FIFO, then the read operation in the process is called a read operation with the blocking flag set

2. Writing data from FIFO: convention: If a process blocks the open FIFO in order to write data to the FIFO, then the write operation in the process is called a write operation that sets the blocking flag.

See: http://blog.csdn.net/MONKEY_D_MENG/article/details/5570468

Pipelines and 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.