Question: Round 2 : pipeline.c ( 5 0 % ) This program takes the same input as sequence.c , but executes the commands as a pipeline,
Round : pipeline.c
This program takes the same input as sequence.c but executes the commands as a pipeline, where the output of
each command is piped to the input of the next command in line. The input of the first command, and the output
of the final command, should not be altered. For example, if the file "cmdpipe" contains the lines
then running:
pipeline cmdpipe
should output the largest files in the current directory, in order of size.
Suggested approach: set it up so that the parent process forks a child to execute each command in turn, and
keeps track of the pipes that the current child should read from and write to The child redirects its input and
output to these pipes, and then executes the current command. Here is a pseudocode version of the setup:
This pseudocode will also be covered in lectures.
Remember that once you call fork, both parent and child have their own copy of all variables, including pipes!
Have a look at the pattern for setting up processes to read and write from pipes,from lectures.
Make sure you close all pipes when they are no longer needed as not doing so can cause behaviour that is hard
to debug.
Note your code should work with any command. For example:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
