代做CSE 3100留学生作业、代写C/C++程序作业、代做Systems Programming作业、代写C/C++编程设计作业
CSE 3100 Systems ProgrammingHomework #5 Due: 10/23/2018Complete your work in the hw5 folder. Remember to pull, add, commit, and push. You need to work onthree files only: execmd.c, workon.c, and runpipeline.c. Do NOT add other files.Exercise 1. (100 points) runpipelineA pipeline is a sequence of external programs chained together to perform a task. The standard output ofstage i of the pipeline is fed to the standard input of stage i + 1. In shells like bash, stages are separatedby "|". For example, the following pipeline that contains 7 stages counts the number of occurrences of eachword in a text file.1 The output of the last stage is redirected into file counts.txt. whitman.txt is in thesolutions repo under sol-hw5. You can also replace it with other text files, for example, your C source code.cat whitman.txt | tr -s [:space:] '\n' | tr -d [:punct:] | tr A-Z a-z | sort | uniq -c | \sort -nr > counts.txtThe seven stages do the following. The command in each stage prints its result to stdout and the outputof the last command is redirected to counts.txt.1. Send the contents of the file whitman.txt to stdout2. Replace every sequence of consecutive spaces in stdin with a single line-feed3. Delete all punctuation characters from stdin and send remaining characters to stdout4. Replace uppercase letters in stdin with lowercase letters5. Sort the lines from stdin alphabetically6. Collapse adjacent matching lines to a single copy preceded by the number of copies7. Sort the lines from stdin in reverse numerical orderIn this problem, you will compete the functions in runpipeline.c so the program can start a pipelinewith the programs specified at the command line. To avoid interference with the shell, pipeline stages areseparated with "--", instead of "|". To run the above bash pipeline with runpipeline, you would run thefollowing command in bash and the resulting counts.txt should be the same. Join two lines when you trythe command../runpipeline cat whitman.txt -- tr -s [:space:] '\n' -- tr -d [:punct:] -- tr A-Z a-z -- \sort -- uniq -c -- sort -nr > counts.txtIn runpipeline.c, the commands for all stages are already stored in an array of Program structures,which are defined as follows.1 typedef struct program_tag { 2 char ** args ; // array of pointers to arguments3 int num_args ; // number of arguments4 int pid ; // process ID of this program5 int fd_in ; // pipe fd for stdin6 int fd_out ; // pipe fd for stdout7 } Program ;1The “\” at the end of first line allows the pipeline commands to continue on the next line; you can also just join the twolines when you try the pipeline in bash.1args[0] is the command and args is the array of arguments to be passed to an execv* function. num argsis the number of arguments in args. pid is the process ID of the child process for this command. If fd inis non-negative, the file descriptor will be used for stdin for the command. If fd out is non-negative, it willbe used for stdout for the command.Note that runpipeline does not redirect the input or output for the pipeline itself. If needed, theredirection can be set on runpipeline by the shell. Then the first command in the pipeline can haveredirected stdin and the last one can have redirected stdout.Your program will start commands in start program(), one command at a time. You can create pipesin the function for the command to be started, if any pipe needed has not been created yet, or you can createall pipes for all stages in function prepare pipes().Your code should close unused pipe FDs for/in each process, should not leave zombies behind, and shouldnot have memory leaks.You can use lsof command to check the open files for processes. For example,# list open files by PIDs$lsof -p 3753,3754,3755,3756,3757$lsof -p 3753# list open files for processes whose name starts with runp, cat, wc, or trlsof -c runp -c cat -c wc -c tr# use -u option to specify a user name. -a indicates combine conditions with 'and'lsof -c runp -u netid -aThe rows that you should pay attention to are the ones that have a number in the FD column. Most ofthe commands have only three open files 0, 1, and 2. Some commands, for example, tee, may have additionalopen files.Dealing with many pipeline stages may look scary at the beginning. However, if you start with two stages,and go on to three stages, four stages, and more, you will find out that seven stages are about the same asthree stages. You can test pipelines with various numbers of stages, as shown in the examples below. Notethat you can use tee to examine the data stream at the middle stages. To check if your code is producingcorrect result or behaves correctly, run the same pipeline in bash (and use "|", instead of "--", to connectstages)../runpipeline echo 'Hello, world!'./runpipeline echo 'Hello, world!' -- wc./runpipeline echo 'Hello, world!' -- cat -- wc./runpipeline echo 'Hello, world!' -- cat -- cat -- wc./runpipeline ls -- cat -- tee t.out -- wc./runpipeline cat -- cat -- tee t.out -- cat -- wc http://www.6daixie.com/contents/13/2035.html
因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:
微信:codinghelp