Fully implement addnumbers

This commit is contained in:
Isaac Shoebottom 2023-09-26 14:41:29 -03:00
parent 9f70a9a66c
commit 11d9b2e41e

View File

@ -40,17 +40,20 @@ int main() {
pipe(receive_from_child); pipe(receive_from_child);
if (fork() == 0) { if (fork() == 0) {
int subtotal = 0;
while (number != 0) { while (number != 0) {
// Read the numbers from the send_to_child pipe // Read the numbers from the send_to_child pipe
read(send_to_child[0], &number, sizeof(int)); read(send_to_child[0], &number, sizeof(int));
subtotal += number;
print_prompt(); print_prompt();
printf("The sum of the numbers is %d\n", number); printf("The subtotal is %d\n", subtotal);
// Write the sum to the receive_from_child pipe // Write the sum to the receive_from_child pipe
write(receive_from_child[1], &number, sizeof(int)); write(receive_from_child[1], &subtotal, sizeof(int));
} }
exit(0); exit(0);
} }
else { else {
int total = 0;
while (number != 0) { while (number != 0) {
print_prompt(); print_prompt();
@ -58,9 +61,13 @@ int main() {
char buffer[100]; char buffer[100];
fgets(buffer, 100, stdin); fgets(buffer, 100, stdin);
number = atoi(buffer); number = atoi(buffer);
print_prompt();
printf("You entered %d\n", number);
write(send_to_child[1], &number, sizeof(int)); write(send_to_child[1], &number, sizeof(int));
read(receive_from_child[0], &number, sizeof(int)); read(receive_from_child[0], &total, sizeof(int));
} }
print_prompt();
printf("The total is %d\n", total);
} }
} }
} }