Last active
August 23, 2017 12:17
-
-
Save morsoinferno/92ac7526769b84198d47cd9cd38938f9 to your computer and use it in GitHub Desktop.
04_pipe2
This file contains 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 <stdlib.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
void main() | |
{ | |
pid_t pid; | |
int fd[2]; | |
int msg_length; | |
char msg[14]; | |
char p[100]; | |
if (pipe(fd) == -1) | |
{ | |
perror("No se pudo crear el pipe"); | |
exit(-1); | |
} | |
if ((pid = fork()) == -1) | |
{ | |
printf("No se pudo crear hijo\n"); | |
exit(-1); | |
} | |
if (pid == 0) // soy el hijo | |
{ | |
printf("Descriptores del hijo: fd[0] = %d, fd[1] = %d\n", fd[0], fd[1]); | |
close(fd[0]); | |
write(fd[1], "Hola pos papi\n", 15); | |
exit(0); | |
} | |
else // soy el padre | |
{ | |
printf("Descriptores del padre: fd[0] = %d, fd[1] = %d\n", fd[0], fd[1]); | |
close(fd[1]); | |
msg_length = read(fd[0], msg, sizeof(msg)); | |
printf("Recibi del hijo = %s", msg); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment