IPC interprocess communication for Linux environment Programming (iii): FIFO

Source: Internet
Author: User
Tags stdin

Pipeline is not a name, so it can only be used between the process of affinity, to pipe name, we call it a well- known pipeline FIFO, of course, there is no difference between FIFO and pipe name, there are other differences are mentioned below. similar to Pipelines, FIFO is a one-way (half-duplex) data stream. Unlike pipelines, each FIFO has a pathname associated with it, allowing non-affinity processes to access the same FIFO. FIFO is a file type. The code for the stat struct member St_mode Indicates whether the file is a FIFO type and can be tested with S_ISFIFO macros.

The FIFO is created by the MKFIFO function, which already implicitly specifies the o_creat| O_excl. That is, if a FIFO exists, it returns a eexist error and creates a new FIFO if it does not exist. FIFO is opened by open instead of Mkfifo. So to open an existing FIFO or create a new FIFO, call Mkfifo First, check if it returns a eexist error, and then call open if the error is returned.

<span style= "FONT-SIZE:14PX;" > #include <sys/types.h> #include <sys/stat.h>int mkfifo (const char *pathname, mode_t mode); </span >
after a FIFO is created, it must either be open to read, or open to write, which can be opened by an open or fopen function. The FIFO cannot be opened for both read and write because it is half-duplex.

FIFO has the following two kinds of uses:

1. FIFO is used by the shell command to transfer data from one pipe line to another, eliminating the need to create intermediate temporary files. FIFO can be used to replicate the output stream between serial pipe commands, so there is no need to write data to the intermediate disk file. such as:

<span style= "FONT-SIZE:14PX;" >mkfifo Fifo1prog3 < Fifo1 &prog1 < infile | Tee Fifof1 | Prog2</span>

Creates a FIFO and then starts Prog3 in the background, which reads data from the FIFO. The PROG1 is then started and the output is sent to the FIFO and prog2 with the tee. The illustrations are as follows:


Figure using FIFO and tee to send a data to two processes

2. FIFO is used in the customer process-server process application to pass data between the client process and the server process. for a client-server program, the code for replacing two pipelines with two FIFO instead is as follows:

Mainfifo.c

<span style= "FONT-SIZE:14PX;" > #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include < sys/stat.h> #include "fifo.h" #define FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2" #define File_mode (S_IRUSR | S_IWUSR | S_irgrp | S_iroth) extern void client (int, int); extern void server (int, int); Intmain (int argc, char **argv) {INTREADFD, writefd;pid_ Tpid;/*creat tow FIFOs; Ok if they already exist*/if ((Mkfifo (FIFO1, File_mode) < 0) && (errno = eexist)) printf ("Can ' t creat%s.\n", FIF O1); if ((Mkfifo (FIFO2, File_mode) < 0) && (errno = eexist)) {unlink (FIFO1);p rintf ("Can ' t creat%s.\n", FIFO2);} if (PID = fork ()) < 0) {printf ("Can ' t fork.\n"); return-1;} else if (PID = = 0) {/*CHILD*/READFD = open (FIFO1, o_rdonly, 0); writefd = open (FIFO2, o_wronly, 0); server (READFD, WRITEFD); E XIT (0);} /*PARENT*/WRITEFD = open (FIFO1, o_wronly, 0), READFD = open (FIFO2, o_rdonly, 0), client (READFD, WRITEFD); Waitpid (PID, NULL , 0);/*wait for child To Terminate*/close (READFD); close (WRITEFD); unlink (FIFO1); unlink (FIFO2); exit (0);} </span>
client.c

<span style= "FONT-SIZE:14PX;" > #include "fifo.h" intclient (int readfd, int writefd) {size_t Len;ssize_tn;char buff[maxline];/*read pathname*/fgets (Buff, MAXLINE, stdin); len = strlen (buff); if (buff[len-1] = = ' \ n ') len--;/*write pathname to IPC channel*/write (WRITEFD, BU FF, Len);/*read from IPC, write to Standard output*/while ((n = read (READFD, buff, MAXLINE)) > 0) write (Stdout_fileno, BU FF, n);} </span>
server.c

<span style= "FONT-SIZE:14PX;" > #include "fifo.h" void server (int readfd, int writefd) {Intfd;ssize_tn;char Buff[maxline+1];/*read pathname frome IPC Channel*/if ((n = read (READFD, buff, MAXLINE)) = = 0) {printf ("end-of-file while reading pathname.\n"); return;} Buff[n] = '/*null '; terminate pathname*/if (fd = open (buff, o_rdonly)) < 0) {/*error:must tell client*/snprintf (buff+ N, sizeof (buff)-N, ": Can ' t open,%s\n", Strerror (errno)); n = strlen (buff); write (writefd, buff, n);} Else{/*open succeeded:copy file to IPC channel*/while ((n = read (fd, buff, MAXLINE)) > 0) write (writefd, buff, n); Close (f d);}} </span>
fifo.h

<span style= "FONT-SIZE:14PX;" > #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h> #include <errno.h></span>
Makefile

<span style= "FONT-SIZE:14PX;" >all:mainfifo mainfifo:mainfifo.c server.c client.cgcc  server.c client.c mainfifo.c-o mainfifo</span>
The program above illustrates the following:

figure using two FIFO customer-server examplesThe above example sends a FIFO, but is still related to inter-process communication, which gives examples of unrelated clients and servers. The client program client_main.c as follows:
<span style= "FONT-SIZE:14PX;" > #include "fifo.h" extern client (int, int); Intmain (int argc, char **argv) {int READFD, WRITEFD;WRITEFD = open (FIFO1, o_w ronly, 0); READFD  = open (FIFO2, o_rdonly, 0); Client (READFD, WRITEFD); close (READFD); close (WRITEFD); unlink (FIFO1); Unlink (FIFO2); exit (0);} </span>
client.c
<span style= "FONT-SIZE:14PX;" > #include "fifo.h" intclient (int readfd, int writefd) {size_t Len;ssize_tn;char buff[maxline];/*read pathname*/fgets (Buff, MAXLINE, stdin); len = strlen (buff); if (buff[len-1] = = ' \ n ') len--;/*write pathname to IPC channel*/write (WRITEFD, BU FF, Len);/*read from IPC, write to Standard output*/while ((n = read (READFD, buff, MAXLINE)) > 0) write (Stdout_fileno, BU FF, n);} </span>
The server program server_main.c as follows:
<span style= "FONT-SIZE:14PX;" > #include "fifo.h" #include <sys/stat.h> #define File_mode (s_irusr | S_IWUSR | S_irgrp | S_iroth) extern void server (int, int); Intmain (int argc, char **argv) {int READFD, writefd;/*creat, Fifos:ok if they alrea Dy exist*/if ((Mkfifo (FIFO1, File_mode) < 0) && (errno! = eexist)) {printf ("can ' t create%s.\n", FIFO1);} if ((Mkfifo (FIFO1, File_mode) < 0) && (errno! = eexist)) {unlink (FIFO1); printf ("Can ' t create%s.\n", FIFO1);} READFD  = open (FIFO1, o_rdonly, 0), WRITEFD = open (FIFO2, o_wronly, 0), server (READFD, WRITEFD); exit (0);} </span>
server.c
<span style= "FONT-SIZE:14PX;" > #include "fifo.h" void server (int readfd, int writefd) {Intfd;ssize_tn;char Buff[maxline+1];/*read pathname frome IPC Channel*/if ((n = read (READFD, buff, MAXLINE)) = = 0) {printf ("end-of-file while reading pathname.\n"); return;} Buff[n] = '/*null '; terminate pathname*/if (fd = open (buff, o_rdonly)) < 0) {/*error:must tell client*/snprintf (buff+ N, sizeof (buff)-N, ": Can ' t open,%s\n", Strerror (errno)); n = strlen (buff); write (writefd, buff, n);} Else{/*open succeeded:copy file to IPC channel*/while ((n = read (fd, buff, MAXLINE)) > 0) write (writefd, buff, n); Close (f d);}} </span>
fifo.h
<span style= "FONT-SIZE:14PX;" > #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h> #include <errno.h> #define FIFO1 "/tmp/ Fifo.1 "#define FIFO2"/tmp/fifo.2 "#define MAXLINE 4096</span>

Reference: Advanced Programming for UNIX environments, UNIX network programming Volume 2: interprocess communication










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.