Linux interprocess communication-known pipeline (FIFO)

Source: Internet
Author: User

Famous Pipes (FIFO)

Named Pipes are also known as FIFO files and are a special kind of file. Because everything in Linux can be treated as a file, the use of named pipes has become very uniform with file operations.

(1) Creating a named pipe

You can create a named pipe with one of the following two functions.

 #include <sys/types.h> #include <sys/stat.h> int Mkfifo (const char * filename, mode_t mode); int Mknod (const char * FileName, mode_t mode | S_ififo, (dev_t) 0);          

Filname refers to the file name, and mode is the read and write permission for the specified files. Mknod is a relatively old function, and using the Mkfifo function is simpler and more prescriptive, so it is recommended to use Mkfifo.

Open (char *path, o_rdonly);  1open (char *path, o_rdonly | O_nonblock); //2open (char *path, o_wronly);  3open (char *path, o_wronly | O_nonblock); //4           

(2) opening a named pipe

Like opening other files, you can open it with open . There are usually four ways to do this:

There are two points to note:

1, that is, the program cannot open the FIFO file in O_rdwr ( read-write ) mode to read and write operations, and its behavior is not clearly defined, because if a pipe is opened in read / write mode, the process will read back its own output, At the same time we usually use FIFO only for one-way data transfer.

2, is passed to The open call is the FIFO path name, but not the normal file. (such as: const char *fifo_name = "/tmp/my_fifo"; )

3, the second parameter in the option o_nonblock, the option O_nonblock represents non-blocking, plus this option, indicates that the open call is non-blocking, if there is no option, then the open call is blocked.

(3) blocking problems

o_rdonlyfifoopen The call is blocked (that is, the second parameter is o_rdonlyfifo otherwise it will not return; Span style= "Font-family:times New Roman;" >open call is non-blocking (that is, the second parameter is o_rdonly  | o_nonblock file, open The call succeeds and returns immediately.

For write-only mode (O_wronly) Open theFifoFile, ifOpenThe call is blocked (that is, the second argument is aO_wronlyopen The call will be blocked until a process opens the same fifo file, if call is non-blocking (that is, the second argument is a o_wronly | o_nonblock always returns immediately, but if no other process opens the same fifo< Span style= "font-family: the song Body;" > file, call will return -1fifo

(4) Use FIFO for interprocess communication

The write end of the pipeline reads the data from a file and writes to the write pipeline. The read end of the pipeline is read from the pipeline and written to the file.

Write-side code:fifowrite.c

#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <String.h>IntMain () {ConstChar *fifo_name ="/tmp/my_fifo";int PIPE_FD =-1;int DATA_FD =-1;int res =0;Constint Open_mode =O_wronly;int bytes_sent =0;Char Buffer[pipe_buf +1];int bytes_read =0;
if (Access (fifo_name, F_OK) = =-1) {printf ("Create the FIFO pipe.\n"); res = Mkfifo (Fifo_name,0777);
if (res! =0) {fprintf (stderr,"Could not create FIFO%s\n", fifo_name); Exit (Exit_failure); }} printf ("Process%d opening FIFO o_wronly\n", Getpid ()); PIPE_FD =Open (Fifo_name, open_mode); printf"Process%d result%d\n", Getpid (), PIPE_FD);if (pipe_fd! =-1) {Bytes_read =0; DATA_FD = open ("Data.txt", o_rdonly);if (data_fd = =-1) {Close (PIPE_FD); fprintf (stderr,"Open File[data.txt] failed\n");Return-1; } Bytes_read =Read (data_fd, buffer, pipe_buf); Buffer[bytes_read] =‘/‘;while (Bytes_read >0) {res =Write (pipe_fd, buffer, bytes_read);if (res = =-1 "write error on pipe\n "); Exit (Exit_failure); } bytes_sent += Res; Bytes_read = read (data_fd, buffer, PIPE_BUF); Buffer[bytes_read] = \0 ' else exit (exit_failure); printf ( "process%d Finished\n"

Pipe Read end: fiforead.c

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <limits.h>#include <String.h>IntMain () {ConstChar *fifo_name ="/tmp/my_fifo";int PIPE_FD =-1;int DATA_FD =-1;int res =0;int Open_mode =O_rdonly;Char Buffer[pipe_buf +1];int bytes_read =0;int bytes_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);

if (data_fd = =-1) {fprintf (stderr,"Open File[dataformfifo.txt] failed\n"); Close (PIPE_FD);Return-1; } printf ("Process%d result%d\n",getpid (), PIPE_FD); if (pipe_fd! =-1do {res = read (pipe_fd, buffer, pipe_buf); bytes_write = write (data_fd, buffer, res); Bytes_read += res;} while (Res > 0else exit (exit_failure); printf ( "process%d finished,%d bytes read\n" , Getpid (), bytes_read); Exit (exit_success);

(5) Security issues with Named pipes

One situation is that aFifoFile, there are multiple processes simultaneously to the sameFifo file write data, and only one read fifofifofifo (that is, blocking mode) Open fifopipe_buf< Span style= "font-family: the song Body;" >, either all bytes are written, or none of the bytes are written. If all of the write requests are destined for a blocked fifopipe_buf bytes, The system ensures that data will never be interleaved.

Linux interprocess communication-known pipeline (FIFO)

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.