#include #include #include #include #include #include #include void print_prompt() { printf("[%d]> ", getpid()); } int main() { bool is_looping = true; // Listen loop while (is_looping) { char input[100]; print_prompt(); fgets(input, 100, stdin); if (strcmp(input, "stop\n") == 0) { is_looping = false; } if (strcmp(input, "onechild\n") == 0) { if(fork() == 0) { print_prompt(); printf("Hello, I am a child process\n"); exit(0); } else { print_prompt(); printf("Hello, I am a parent process\n"); wait(NULL); } } if (strcmp(input, "addnumbers\n") == 0) { int number = 1; int send_to_child[2]; int receive_from_child[2]; pipe(send_to_child); 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 subtotal is %d\n", subtotal); // Write the sum to the receive_from_child pipe write(receive_from_child[1], &subtotal, sizeof(int)); } exit(0); } else { int total = 0; while (number != 0) { print_prompt(); printf("Please input a number, 0 to terminate: "); 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], &total, sizeof(int)); } print_prompt(); printf("The total is %d\n", total); } } } } void temp() { char input[5][100]; char buffer[500]; //Print prompt print_prompt(); if (fgets(buffer, 500, stdin) == NULL) { exit(0); } //split buffer on spaces into input array sscanf(buffer, "%s %s %s %s %s", input[0], input[1], input[2], input[3], input[4]); };