MPs queue, FIFO, and socketpair

Source: Internet
Author: User

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

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.