Shared Memory: Full-duplex anonymous pipe

Source: Internet
Author: User

Main.cpp#include <iostream> #include <string.h> #include <stdlib.h> #include <signal.h> Using namespace std;//two processes share information residing in the kernel, and each access to shared information involves a system call.  void EXIT (int arg) {cout<< "SIGINT" << "is Quit" <<endl;exit (0);} int write (int fd,const char *buff,int n) {ssize_t count = 0;if ((Count=write (fd,buff,n)) ==-1) {cout<< "Write error!!!" <<endl;return-1;} return count;} int read (int fd,char *buff,int N) {ssize_t count = 0;if ((Count=read (fd,buff,n)) ==-1) {cout<< "Read error!!!" <<endl;return-1;} return count;} int main () {int fd[2],pd[2];p ipe (FD);p ipe (PD); signal (sigint,exit);p id_t id;if ((Id=fork ()) ==0) {close (fd[0]); Close ( PD[1]); Char buff[255];memset (buff,0,sizeof (Buff)); while (1) {Write (fd[1], "I am Child process!", 20); Read (pd[0],buff,sizeof (Buff)); Cout<<buff<<endl;sleep (1);}} else if (id>0) {close (pd[0]), close (Fd[1]), Char buff[255];memset (buff,0,sizeof (Buff)), while (1) {Read (Fd[0],buff,    sizeof (buff));cout<<buff<<endl; WritE (pd[1], "Hello child", 11);}} else{cout<< "fork () error!!" <<endl;} return 0;}

Makefile:

out=a.outmaincpp=main.cppmaino=main.occ=g++$ (out): $ (Maino) $ (cc)-o [email protected] $^$ (Maino): $ (maincpp) $ (CC)-C $ <. PHONY:CLEANCLEAN:RM-RF $ (out) $ (Maino)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shared Memory: Full-duplex anonymous pipe

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.