Question

C homework help pipes

Round 2: 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

ls -s1

sort -n

tail -n 5

then running

./pipeline < cmdpipe

should output the 5 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 pseudo-code version of the setup:

// For each command except the final one:// new_pipe = the output of the current command// For each command except the first one:// prev_pipe = the output of the previous command, input of current commandfor cmd in cmds:
    if this is not the final command:
        pipe (new_pipe)    // Create a new pipe

    fork and handle errors    if (child)
        if this is not the first command:
            Redirect input to prev_pipe        if this is not the final command:
            Redirect output to new_pipe
        execute command    else // parent
        if this is not the first command:
            close prev_pipe        if this is not the final command:
            prev_pipe = new_pipe

close any remaining pipes, clean up

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:

cal

cat pipeline.c

grep .

wc -l

should print out the number of non-blank lines in your source file (the first line has no effect - why?).


2 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 7 more requests to produce the answer.

3 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
C homework help pipes
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • Can someone please help me with the piping of the following code. The Myshell is a command langua...

    Can someone please help me with the piping of the following code. The Myshell is a command language interpreter which can execute several internal commands as well as some external command using a default Linux shell. The command can be read from either standard input or a batch file. This program currently executes commands such as ls>1, sort myshell.c >2. What I need it the to pipe the commands so i can execute commands like ls | 1 or sort...

  • Round 1: sequence.c This program should read and execute a list of commands from stdin. Each comm...

    Round 1: sequence.c This program should read and execute a list of commands from stdin. Each command and its arguments (if any) will appear on a separate line. For example, if the file "cmdfile" contains the lines: whoami cal 4 2019 echo The time is: date then running 1 /sequence< cmdfile should output your username, a calendar of the month of April, the string "The time is:", and the current date/time, to standard output. Suggested approach: first, make sure you...

  • 7. Write a command that redirects all chrome processes to a file name process.txt. Write a...

    7. Write a command that redirects all chrome processes to a file name process.txt. Write a command to output the content of process.txt file. 8.Write a command that redirect (add) all processes containing the word “host” to process.txt file above. 9.Write a command that will ask users about their location (“Where do you live?”). The command should read the input from a user and store it in a variable “$location”. Use another command to output the user’s response. 10.Write a...

  • Write a shell script called GetMyStuff.sh that will provide the above information (the script must be...

    Write a shell script called GetMyStuff.sh that will provide the above information (the script must be a executable, and must work regardless of which directory it is in). Also, just saying cat MyStuff.txt is not sufficient. (1.0%) A pipeline is the combination of at least 2 commands, where the output of command1 is the input for command2. The syntax is command1 | command2 where | is the pipeline operator. Read the man pages about the ls and wc commands. Devise...

  • Allow the main process to generate a text file containing the text of this assignment. The main p...

    Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • Write Linux command for each question. Please use one single commend to do following process. Q1:...

    Write Linux command for each question. Please use one single commend to do following process. Q1: Display all lines of list1.txt and list2.txt containing the letter ‘p', and sort the result. Q2: Append “file1.txt” and “file2.txt”, and redirect to “file3.txt”. Q3: Sort “file3.txt”, and redirect the result to file “file4.txt” Q4: Use shorthand way to change the permission of “file3.txt” to “user - write read execute, group - read execute, other - execute”. Q5: Extract first column from “file3.txt”, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT