P. 1
15734_pipe3_ FD

15734_pipe3_ FD

|Views: 0|Likes:
Published by Prabhat Pankaj
dfffffdfd
dfffffdfd

More info:

Categories:Types, Reviews
Published by: Prabhat Pankaj on Apr 04, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

04/04/2013

pdf

text

original

//Program for full duplex communication #include <iostream> using namespace std; #include<stdlib.h> #include <stdio.

h> #include <unistd.h> int main() { int status, pid, pipefds[2],pipefds2[2],status2; char instring[20],instring2[20]; /* Create the pipe and return 2 file descriptors in the pipefds array */ /* This operation is done before the fork so that both processes will */ /* know about the same pipe, which will allow them to communicate. */ status = pipe(pipefds); status2=pipe(pipefds2); if (status == -1) { perror("Trouble"); exit(1); } if (status2 == -1) { perror("Trouble"); exit(1); } /* create child process; both processes continue from here */ pid = fork(); if (pid == -1) { perror("Trouble"); exit(2); } else if (pid == 0) /* child : sends message to parent*/ { /* close unused end of pipe */ /* because this process only needs to write */ close(pipefds[0]); close(pipefds2[1]); /* send 7 characters in the string, including end-of-string */ cout << "About to send a message: " << endl; write(pipefds[1], "Hi Mom!", 7); read(pipefds2[0], instring2, 7); cout << "Just received a message that says: " << instring2 << endl; close(pipefds[1]); close(pipefds2[0]);

7). cout << "Just received a message that says: " << instring << endl. close(pipefds2[1]). close(pipefds[0]). } return 0. read(pipefds[0]. } /* parent : receives message from child */ . } else { /* close unused end of pipe */ /* because this process only needs to read */ close(pipefds[1]). "Hi pop!". instring. /* read from the pipe */ write(pipefds2[1]. 7). exit(0).exit(0).

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->