/* pipe1.c++ creates a pipe and parent-child communicates through it. */ #include using namespace std; #include #include #include #include #include int main() { // pipe1.c++ int chpid, fd[2], err, status ; err = pipe(fd) ; if(err == -1) { cerr << "pipe open error\n" ; return 0; } chpid = fork(); if(chpid == -1){ cerr << "fork() error\n"; return 0; } if(chpid > 0){ // write in parent char buffP[100] = "IIIT Kalyani"; close(fd[0]); write(fd[1], buffP, strlen(buffP)); cout << "Parent has written in pipe\n"; close(fd[1]); waitpid(chpid, &status,0); } else { // child char buffC[100]={0}; close(fd[1]); sleep(5); read(fd[0], buffC, 100); cout << "Child: " << buffC << endl; close(fd[0]); } return 0; }