Socketpair Implementing Process Communication

Source: Internet
Author: User

pipes are used to create pipelines, but a single pipe can only communicate One direction at a time, while the other end is for reading and the other for writing. If you want to process two-way communication, you must create a pair of pipelines. Specific implementations are ignored. The Socketpair can then be used to create a two-way channel. depending on the underlying implementation, the Open is a file, fd[0],fd[1], in the pipeline f[0] read end, f[1] write end.

#include <sys/types.h>

#include <sys/socket.h>

int socketpair (int domain, int type, int protocol, int sv[2]);

Domain: Choose af_local;

Type:sock_stream

Protocol: Default 0

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/65/wKioL1dAJWiha3BLAABcQg5Afhs261.png "title=" 110. PNG "alt=" Wkiol1dajwiha3blaabcqg5afhs261.png "/>

#include <stdio.h>                                                                           #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include < unistd.h> #include <errno.h> #include <string.h>int main () {&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;FD [2];    if (Socketpair (AF_LOCAL,SOCK_STREAM,0,FD) <0) {         perror ("Sockpair");        return 1;     }    pid_t id=fork ();     if (id<0) {        &nbsP;perror ("fork");        return 2;    }     else if (id==0) {        close (fd[0]);         char buf[1024];         while (1)                 {             ssize_t _s;             strcpy (buf, "Hello bit");             write (Fd[1],buf,strlen (BUF));             _s=read (fd[1],buf,sizeof (BUF)-1);             buf[_s]= ';             printf ("Father-> child%s\n ", buf);        }         close (fd[1]);    }    else{         close (fd[1]);        char buf[1024];         while (1)         {             ssize_t _s=read (fd[0],buf,sizeof (BUF)-1);             if (_s>0) {                 buf[_s]= ';                 printf ("Child -> father  %s\n ", buf);            }             strcpy (buf, "Hello world");             write (Fd[0],buf,strlen (BUF));        }                        close (fd[0]);        //wait child     }    return 0;

Run:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/65/wKioL1dAI8iBV9ZkAAAub3W1O4c497.png "title=" 28.png "alt=" Wkiol1dai8ibv9zkaaaub3w1o4c497.png "/>

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1775706

Socketpair Implementing Process 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.