650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/77/C7/wKioL1ZuVnLBMqY9AADMXNaTmIA629.png "title=" 4.png " alt= "Wkiol1zuvnlbmqy9aadmxnatmia629.png"/>
The pipeline is one of the first UNIX IPC forms supported by Linux.
The pipe is half-duplex, the data can only flow in one direction;
A pipeline can only be responsible for data transfer in one Direction.
Two pipelines need to be established when communication between two parties is required;
Can only be used between parent-child processes or sibling processes (affinity processes);
If process a communicates with process B, you need to create two pipelines:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/77/C7/wKioL1ZuU43DiTnSAABtEy-RtXk920.png "title=" 1.png " alt= "Wkiol1zuu43ditnsaabtey-rtxk920.png"/>
A pipe can only be used for communication in one Direction, and its other direction needs to be closed.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/77/C7/wKioL1ZuVCThFv6YAABlqIm8eWA392.png "title=" 2.png " alt= "Wkiol1zuvcthfv6yaablqim8ewa392.png"/>
encapsulated pipe class, header file:
#ifndef CPIPE_H#define CPIPE_H#include <cstddef> #define socket int#define INVALID_SOCKET (SOCKET) (~0) class tpipe{public: tpipe (void); ~tpipe (void) { close (); } boolopen (bool Bocanbeinherit = false) voidclose (void) { closereadhandle (); Closewritehandle (); } intread (void * lpbuff,size_t nlength); intwrite (const void * lpbuff,size_t nlength); Socketgetreadhandle (void) const { return m_nPipe[READHANDLE_INDEX]; } socketgetwritehandle (void) const { return m_npipe[writehandle_ Index]; } voidclosereadhandle (void) { closeimp (READHANDLE_INDEX); } voidcloSewritehandle (void) { closeimp (writehandle_index); }protected: enum { READHANDLE_INDEX=0, WRITEHANDLE_INDEX=1, }; Voidcloseimp (Int nindex); socketm_npipe[2];}; Class tpipepair{public: tpipepair (void); ~tpipepair (void); boolinitex (void); voidclose (void) { m_comm[0].close (); m_comm[1].close (); } voidcloseina (void) { m_comm[0].closereadhandle (); m_comm[1].closewritehandle (); &nbSP;} &NBSP;&NBSP;&NBSP;&NBSP;VOIDBCLOSELNB (void) { m_comm[0].closewritehandle (); m_comm[1].closereadhandle () ; } socketgetaproreadhandle (void) { return m_comm [1].getreadhandle (); } socketgetaprowritehandle (void) { return m_ Comm[0].getwritehandle (); } socketgetbproreadhandle (void) { return M_comm[0].getreadhandle (); } socketgetbprowritehandle (void) { return m_comm[1].getwritehandle (); }protected: enum { MASTER_2_SUBMGR = 0x00, submgr_2_master = 0x01, }; tpipem_ COMM[2];}; #endif &nbsP;// cpipe_h
Source file:
#include "cpipe.h" #include <unistd.h>tpipe::tpipe (void) { m_npipe[0] = invalid_socket; m_npipe[1] = invalid_socket;} Void tpipe::closeimp (Int nindex) { if (m_npipe[nindex] != Invalid_socket) ::close (M_npipe[nindex]); m_npipe[nindex] = invalid_socket;} Bool tpipe::open (Bool bocanbeinherit) { if (pipe (m_nPipe) == -1 ) return false; return true;} Inttpipe::read (void * lpbuff,size_t nlength) { if (m_nPipe[ Readhandle_index] == invalid_socket) return -1; return ::read (m_npipe[readhandle_index],lpbuff,nlength);} Inttpipe::write (const Void * lpbuff,size_t nlength) { if (m_nPipe[WRITEHANDLE_INDEX] == invalid_socket) return -1; Return ::write (m_npipe[writehandle_index],lpbuff,nlength);} Tpipepair::tpipepair (void) {}tpipepair::~tpipepair (void) {}bool tpipepair::initex (void) { tpipepair::close (); if (!m_comm[submgr_2_master].open ()) return false; else if (!m_comm[master_2_ Submgr].open ()) return false;}
LINUX-IPC Process Communication-pipeline