Pipelines and FIFO are initially in the form of unix ipc and are rarely used. SocketPair can be used as a full-duplex MPs queue.
MPs queue
* Only used for inter-process communication with kinship
* Unidirectional, that is, half duplex (bidirectional method: 1 uses two pipelines 2 uses SocketPair)
* Pipe () => write ()/read ()
FIFO (famous Pipeline)
* It Can Be Used for unrelated inter-process communication.
* Unidirectional
* Mkfifo () => open () => write ()/read ()
SocketPair
* A socket (generally used for network communication) is used for inter-process communication between local machines.
* Bidirectional, that is, full duplex
* Socketpair () => write ()/read ()
* Example [SocketPair. cpp]:
#include
#include
#include
#include
#include
using namespace std;const int MAXSIZE = 100;int main(){ int fd[2]; int rLen; char wBuf[MAXSIZE] = "Hello World"; char rBuf[MAXSIZE]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) { cout << "SocketPair Err" << endl; return -1; } pid_t pid = fork(); if (pid < 0) { cout << "Fork Err" << endl; return -1; } if (pid == 0) { // Chlid cout << "Child, Pid=" << getpid() << endl; write(fd[0], wBuf, strlen(wBuf)); write(fd[1], wBuf, strlen(wBuf)); exit(-1); } // Parent sleep(1); cout << "Parent, Pid=" << getpid() << endl; rLen = read(fd[1], rBuf, MAXSIZE); rBuf[rLen] = 0; cout << "Read Fd[1], rBuf : " << rBuf << endl; rLen = read(fd[0], rBuf, MAXSIZE); rBuf[rLen] = 0; cout << "Read Fd[0], rBuf : " << rBuf << endl; return 0;}
# g++ SocketPair.cpp # ./a.out Child, Pid=9569Parent, Pid=9568Read Fd[1], rBuf : Hello WorldRead Fd[0], rBuf : Hello World