From 11d9b2e41eb979db719041230dd2f3449fb500bf Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Tue, 26 Sep 2023 14:41:29 -0300 Subject: [PATCH] Fully implement addnumbers --- Assignment2/main.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Assignment2/main.c b/Assignment2/main.c index 8c29c29..5e969eb 100644 --- a/Assignment2/main.c +++ b/Assignment2/main.c @@ -40,17 +40,20 @@ int main() { pipe(receive_from_child); if (fork() == 0) { + int subtotal = 0; while (number != 0) { // Read the numbers from the send_to_child pipe read(send_to_child[0], &number, sizeof(int)); + subtotal += number; 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(receive_from_child[1], &number, sizeof(int)); + write(receive_from_child[1], &subtotal, sizeof(int)); } exit(0); } else { + int total = 0; while (number != 0) { print_prompt(); @@ -58,9 +61,13 @@ int main() { char buffer[100]; fgets(buffer, 100, stdin); number = atoi(buffer); + print_prompt(); + printf("You entered %d\n", number); 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); } } }