Created
April 12, 2013 22:32
-
-
Save montogeek/5375720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <string.h> | |
using namespace std; | |
#define tam 100 | |
int main(int argc, char *argv[]) | |
{ | |
pid_t pid; | |
int tuba[2], tubb[2], bytesleidos; | |
char memoria[tam]; | |
pipe(tuba); | |
pipe(tubb); | |
if((pid=fork()) == 0) | |
{ | |
//hijjo | |
close(tuba[1]); | |
close(tubb[0]); | |
string msjh = ""; | |
cout<<"Proceso hijo corriendo "<<endl; | |
//bloqueo hasta que llegue mensaje | |
while((bytesleidos=read(tuba[0],memoria,tam)) > 0) | |
{ | |
memoria[bytesleidos] = '\0'; | |
string msjp = (string)memoria; | |
cout<<"Mensaje del padre: "<<msjp<<endl; | |
} | |
close(tuba[0]); | |
memoria[0] = '\0'; | |
strcpy(memoria,"Recibido \n"); | |
write(tubb[1],memoria,strlen(memoria)); | |
close(tubb[1]); | |
} | |
else{ | |
//padres | |
close(tuba[0]); | |
close(tubb[1]); | |
string msjp = ""; | |
cout<<"Digita mensaje: "; | |
cin>>msjp; | |
strcpy(memoria,msjp.c_str()); | |
write(tuba[1],memoria,msjp.size()); | |
close(tuba[1]); | |
while((bytesleidos=read(tubb[0],memoria,tam)) > 0) | |
{ | |
memoria[bytesleidos] = '\0'; | |
string msjh = (string)memoria; | |
cout<<"Mensaje del hijo: "<<msjh<<endl; | |
} | |
close(tubb[0]); | |
} | |
waitpid(pid,NULL,0); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment