Fork, Processes, and Pipes

Description of pipes.

Description of fork.

This program is going to implement a calculator. One process will be the user interface; it will prompt the user for an operator and two operands. The second process will perform the operation and pass the result back to the first process. Pipes will be used to transfer information between the two processes.

To Do

The operating system buffers output. To see the print statements as they occur in the execution of your program, you must force the output to be displayed, not buffered. To force the output to be displayed, follow each printf with the statement fflush(stdout);

Write a program that:
  1. Declares two pipes: parentpipe and childpipe.
  2. Fork so there is a parent process and a child process.
  3. The parent and the child must each close the end of each pipe that it is not using.

    The parent and child are then going to work as a team, using the appropriate pipe for communication, to provide a simple integer calculator for the user.

    The parent is going to prompt the user for an operation (+, -, *, /) and then 2 operands. The parent is going pass the operation and the operands to the child via the parentpipe. The child is going to perform the the operation and pass the results back to the parent via childpipe. More details are below.

After closing the portion of the pipe not being used by the parent/child:

Parent

The parent is going to present a menu to the user to enter: You must properly handle invalid input by displaying an error message and then presenting the menu again. Invalid input would be any other input besides the listed operations.

The parent then must then prompt the user for 2 integer operands. You can assume that the operands entered will be valid, integer operands. You do not have to validate the operand input.

The parent must then pass the operation followed by the 2 operands to the child by writing them in that order into parentpipe.

The parent must then read from childpipe to receive the results. After receiving the result of the operation from the child, the parent must display the results (remember to also display the operation and the operands) and present the menu again.

The parent must perform the above sequence until the user enters X to terminate the program. When X is entered, the parent must pass X on to the child via parentpipe, wait for the child to terminate, and then close parentpipe (the order is important here).

Child

The child must go into a loop until X is passed. The child must read the operation from parentpipe (Remember read is blocking, what does this mean?). If the operation is X, it must close childpipe and terminate. If the operation is not X, the child must read the two operands from parentpipe, perform the specified operation, write the result into childpipe, then read the next operation from parentpipe.

More specifics

The output of your program must be properly labeled and easy to read. You must echo all input.

You will need the following include files:

sys/types.h

sys/ipc.h

unistd.h

stdio.h

stdlib.h

wait.h

Submit (see main project page for due date)

You will use turnin to submit your code. On the command line, type turnin, select cs326 then pipe . Your program name must be pipecalc.c.

Note: this assignment must be done in C

Your grade will be made up of the following:


Maintained by  Barbara Bracken
Last Modified 10/7/2026
This page is copyright © by Barbara Bracken