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