-
Notifications
You must be signed in to change notification settings - Fork 0
/
happyfamily.c
80 lines (68 loc) · 1.93 KB
/
happyfamily.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main(void)
{
int fd1[2], fd2[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
int a, b, resultado = 0;
int state;
// Creacion de tuberias e hijo
if ((pipe(fd1)) == -1)
{
printf("Error with pipe 1 \n");
perror("fork");
_exit(-1);
}
if ((pipe(fd2)) == -1)
{
printf("Error with pipe 2 \n");
perror("fork");
_exit(-1);
}
if((childpid = fork()) == -1)
{
printf("Error with son\n");
perror("fork");
_exit(-1);
}
// Padre e hijo
if(childpid == 0)
{
/* PADRE */
close(fd1[0]);
close(fd2[1]);
/* Send "string" through the output side of pipe */
a = 5;
b = 6;
write(fd1[1], &a, sizeof(int));
write(fd1[1], &b, sizeof(int));
printf("PADRE | Se van a sumar los números %d y %d\n", a, b);
wait(state);
nbytes = read(fd2[0], &resultado, sizeof(int));
printf("PADRE | Resultado = %d\n", resultado);
//exit
close(fd1[1]);
close(fd2[0]);
}
else
{
/* HIJO */
close(fd1[1]);
close(fd2[0]);
/* Read in a string from the pipe */
printf("HIJO | Comienzo a leer\n");
nbytes = read(fd1[0], &a, sizeof(int));
nbytes = read(fd1[0], &b, sizeof(int));
resultado = a + b;
printf("HIJO | Suma: %d + %d = %d\n", a, b, resultado);
write(fd2[1], &resultado, sizeof(int));
//exit
close(fd1[0]);
close(fd2[1]);
exit(1);
}
return(0);
}