Anda di halaman 1dari 2

#include #include #include #include

<iostream> <sys/wait.h> <sys/types.h> <unistd.h>

using namespace std; int main(int argc, char* argv[]) { int pid; int status=0; int pfds1[2]; int pfds2[2]; char buf1[50]; char buf2[50]; pipe(pfds1); pipe(pfds2); pid=fork(); if(pid==-1) { cout<<"error n creating child process"<<endl; } else if(pid==0) { cout<<"Child"<<endl; cout<<"My id is = "<<getpid()<<endl; cout<<"My parents id is = "<<getppid()<<endl; int id; id=fork(); cout<<"Grandparent id = "<<getpid()<<endl; if(id!=0) cout<<"Parent id = "<<getpid()<<endl; else cout<<"New child id = "<<getpid()<<endl; close(pfds1[0]); write(pfds1[1], "CHILD: Sure!!what time???", 25); close(pfds2[1]); read(pfds2[0],buf2,34); cout<<buf2; exit(status); } else { cout<<"Parent"<<endl; cout<<"My id is = "<<getpid()<<endl; close(pfds2[0]); write(pfds2[1], "PARENT: Let s go and watch a,movie today", 42); close(pfds1[1]);

read(pfds1[0],buf1,25); cout<<buf1; wait(&status); } return 0; }

Anda mungkin juga menyukai