-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog1.c
98 lines (72 loc) · 2.74 KB
/
prog1.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#include <unistd.h>
#include <string.h>
#define MSG_SIZE 1024
int main(void)
{
int status, id, j;
int pid = getpid();
//pipe to send messages from parent to child
int size_message_to_child;
int pipe_parent_to_child[2];
pipe(pipe_parent_to_child);
//pipe to send messages from child to parent
int size_message_to_parent;
int pipe_child_to_parent[2];
pipe(pipe_child_to_parent);
char buffer[1025];
printf("PID do Processo: %d \n", pid);
int pid2 = fork();
if (pid2 != 0) {
//PARENT
int pid_filho = pid2;
printf("PARENT: pid: %d \n", getpid());
char *message = "Luke, I am your father.";
printf("PARENT: Sending Message: %s\n",message);
//Using pipe to send the message
write(pipe_parent_to_child[1], message, strlen(message));
close(pipe_parent_to_child[1]);
printf("PARENT: waiting message from child...\n");
//wait for message from parent
if ((size_message_to_parent = read ( pipe_child_to_parent[0], buffer, MSG_SIZE ) ) >= 0) {
buffer[size_message_to_parent] = 0; //terminate the string
printf("PARENT: Received Message: %s \n", buffer);
}else{
printf("PARENT: Error reading from pipe\n");
}
printf("PARENT: waiting child to end...\n");
waitpid(pid_filho, &status, 0);
printf("PARENT: Child ended. Ending parent too. \n");
exit(0);
} else {
//CHILD
int pid_filho = getpid();
printf("CHILD: pid: %d \n",pid_filho);
printf("CHILD: parent's pid: %d\n", pid);
printf("CHILD: waiting message from parent...\n");
//wait for message from parent
if ((size_message_to_child = read ( pipe_parent_to_child[0], buffer, MSG_SIZE ) ) >= 0) {
buffer[size_message_to_child] = 0; //terminate the string
printf("CHILD: Received Message: %s \n", buffer);
}else{
printf("CHILD: Error reading from pipe\n");
}
char *message = "Noooooooooooooooooooooo";
printf("CHILD: Sending Message: %s\n",message);
//Using pipe to send the message
write(pipe_child_to_parent[1], message, strlen(message));
close(pipe_child_to_parent[1]);
int j;
for (j = 0; j <= 10000; j++);
char str_j[20];
sprintf(str_j, "%d", j);
printf("CHILD: Sending new message: %s\n",str_j);
//Using pipe to send the message
write(pipe_child_to_parent[1], str_j, strlen(message));
close(pipe_child_to_parent[1]);
execl("/bin/ls", "ls", (char*)0);
}
exit(0);
}