Purpose

Thepurposeofthisassignmentistomakesurethatthestudentscanworkeffectivelywiththe fork , pipe , and dup , system calls. It should also grant some insight into how the shell enables input and output redirection behind the scenes.

Description

For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text entered for either of these is quit , then the program should immediately exit. If quit is not found, then each of these lines of input will be treated as a command line to be executed. These two commands should be executed so they behave as if the user had typed command1 | command2 at the shell prompt. This means that the standard output of the first command will be redirected into the standard output of the second command.

It should be noted that using cin >> will not grab a whole line; you will need another function to get the whole line.

You will need to be able to split the text entered by the user into the individual command line arguments that comprise it. I do not care how you accomplish this, as long as it's either part of the standard library or code that you wrote entirely on your own. Possibilities include the C function strtok or the C++ istringstream object. You do not need to worry about escaping special characters while splitting the string for the purposes of this assignment; the split can be performed based on spaces alone.

After these commands have both finished executing, your program should prompt for another pair of input commands to run, repeating the procedure over and over again until the program receives quit as one of its inputs.

Example

% ls
a b c def example typescript
% ls | wc
6 6 29
% ls -a
. .. a b c def example typescript
% ls -a | wc -w
8
% ./assign5
command1? ls
command2? wc
6 6 29
command1? ls -a
command2? wc -w
8
command1? quit
%

Requirements

  • You may not use the system function to do this. You must make your own child processes with fork , do the appropriate changes to the file descriptors in each of them, and have each of them use one of the exec calls to run the commands given by the user.
  • Usethe pipe systemcalltocreatethepipeandsupplythefiledescriptorsusedtocommunicatebetween the two programs that are run.
  • If an error occurs at any point, there should be an appropriate error message printed to the standard error stream.
  • Your program must be able to handle user-specified commands that contain up to five command-line arguments past the command.
  • Your program must be able to handle user-specified commands that are up to 255 characters long.
  • Make sure to close unused pipe file descriptors in all processes. If you fail to do this, your program will likely stall. (deadlock)
  • Do not forget that this program involves a loop. After both programs have finished running (but not until), the program should go back to the beginning and prompt for two more commands to run.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.