1. Standard Flow PipingPipeline Operations Support file stream mode, which is used to create a pipeline that links another process, through functions Popen and PcloseThe specific introduction of Popen in this blog:linux multi-process learning is described in detail2. Nameless Pipes (pipe)Features:1) Communication between kinship processes only (parent or sibling)2) Half duplex (fixed read end and fixed write end)3) is a special file, can be used read,write, etc., in memoryPipeline function Prototypes:#include <unistd.h>int pipe (int fds[2]);pipelines are represented by a pair of file descriptors in the program, one is a readable attribute, one is a writable property: Fds[0] read, fds[1] Write
Two processes must have an inheritance relationship in order to inherit this open file descriptor
int main () {int fds[2];if (pipe (FDS) ==-1) {perror ("pipe:"); return 0;} Char buf[1024]= "", if (fork () ==0) {close (fds[1]), while (Memset (buf,0,sizeof (BUF))) {if (read (fds[0],buf,1024) ==0) {// Exit break when there is no data in the pipeline;} printf ("Child:read:");p UTS (BUF);} Exit (1);} Else{close (Fds[0]);//char p[1024];//char *p= "Hello world!"; while (memset (buf,0,1024), fgets (Buf,1024,stdin)!=null) write (fds[1],buf,1024); close (fds[1]);p rintf ("parents, Finish\n "), Wait (NULL),//wait must be placed behind close, because only the parent process's fds[1] is turned off, when no data is read by the child process, it will exit, otherwise it will cause deadlock}}
3 Named pipes (FIFO)nameless pipes can only be used in the process of inter-relationship interprocess communication, which is greatly limited by the use of pipelines, and a well-known pipeline breaks through this limitation by developing a path name paradigm to implement irrelevant process communication3.1 Create, delete FIFO fileCreating a FIFO file is similar to creating a normal file, just after the file FIFO is createdCreating a function prototype for a FIFO file#include <sys/types.h>#include <sys/stat.h>int Mkfifo (const char *pathname,mode_t mode);The parameter pathname is the full path name of the FIFO file to be created;parameter mode is a file access rightcreate successfully returned 0, creation failed to-1the function prototype for deleting the FIFO file isint unlink (const char *pathname);Example:
#include <iostream>using namespace Std;int main (int Argc,char *argv[]) {if (Mkfifo (argv[1],0666) ==-1) {perror ("ew "); return 0;} Unlink (argv[1]);}
3.2 Open, close FIFO fileThe open and close functions are the same as opening/closing more common files for FIFO types of files. Using O_wronly to open the write end of the FIFO, with the o_rdonly option, the read-in end of the FIFO is opened, and the write read-in can be opened simultaneously by several processes. Fd_recv=open (argv[1],o_rdonly);
Fd_send=open (argv[2],o_wronly);
if (fd_send==-1) {
Perror ("Fd_send");
Exit (1);
}
3.3 Read/write FIFOread:Fd_recv=open (argv[1],o_rdonly);
char buf_recv[1024]= "";
if (read (fd_recv,buf_recv,1024)!=0) write (1,buf_recv,strlen (BUF_RECV));Close (FD_RECV)Write:Fd_send=open (argv[2],o_wronly);
char buf_send[1024]= "";
if (fgets (Buf_send,1024,stdin)!=null) write (Fp_send,buf_send,strlen (buf_send));Close (fd_send);Program Demonstration:pork_send.c and pork_recv.c chatuse fork to open up a process, two programs can be chat function
/*************************************************************************> File name:pork_send.c> Author: Yang> mail:[email protected] > Created time:2014 August 22 Friday 19:54:08 ************************************* /#include <stdio.h> #include <string.h> #include <stdlib.h># include<sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <memory.h>int main (int Argc,char *argv[]) {if (Mkfifo (argv[1],0666) ==-1) {perror ("Mkfifo1"); exit (1);} int Fd_send,fd_recv;fd_send=open (argv[1],o_wronly); Fd_recv=open (argv[2],o_rdonly); char buf_recv[1024]= ""; Char buf _send[1024]= "";p rintf ("opening!\n"); if (fork () ==0) {//Peel a process to accept information close (fd_send); while (Memset (buf_recv,0,sizeof (BUF_RECV)), read (fd_recv,buf_recv,1024) >0) {Write (1,buf_recv,strlen (BUF_RECV));} Close (FD_RECV); exit (1);} Close (FD_RECV), while (Memset (buf_send,0,sizeof (buf_send)), fgets (Buf_send,1024,stdin)!=null) {Write (fd_send,buf_ Send,strlen (Buf_send));//used to send information}cloSE (fd_send); wait (NULL);}
/*************************************************************************> File name:pork_recv.c> Author: Yang> mail:[email protected] > Created time:2014 August 22 Friday 20:12:32 ************************************* /#include <string.h> #include <stdlib.h> #include <sys/types.h > #include <sys/stat.h> #include <fcntl.h> #include <memory.h> #include <stdio.h>int main ( int Argc,char *argv[]) {if (Mkfifo (argv[2],0666) ==-1) {perror ("Mkfifo2"); exit (1);} int Fd_send,fd_recv;fd_recv=open (argv[1],o_rdonly);//Note there is something different about this position in the program above Fd_send=open (argv[2],o_wronly); Char buf_ Recv[1024]= "", Char buf_send[1024]= "" ";p rintf (" opening!\n "), if (fork () ==0) {//Accept information Close (fd_send); while (Memset (buf_ Recv,0,sizeof (BUF_RECV)), read (fd_recv,buf_recv,1024) >0) {Write (1,buf_recv,strlen (BUF_RECV));} Close (FD_RECV); exit (1);} Close (FD_RECV);//Send Message while (memset (buf_send,0,sizeof (buf_send)), fgets (Buf_send,1024,stdin)!=null) {Write (fd_send , Buf_send,strlen (buf_send));} Close (fd_send); wait (NULL);}
Pipeline Learning for Linux IPC (inter-process communication, interprocess communication)