$25
1 Motivation
As Computer Scientists/Engineers, you will be spending a lot of time learning new tools constantly. Google being our best friend, there is another great utility for UNIX systems, which is the man command. man stands for manual files. When you are interacting with UNIX commands, you will see a lot of options and arguments, which you are not familiar with or worst, you forgot, since it was 2 years ago when you had encountered them the last time. Manual pages are handy tools for a quick tour of learning/remembering.
However, man pages can be a bit overwhelming. There could be tens of options for a simple command with long and precise explanations. If you are interested in only one of these options, it can be difficult to locate that option in the man page. This is where the grep command comes in. By using the grep command, you could select the specific lines which contain the specific word you are interested in and thus you could minimize the length of text you are dealing with.
Your task requires combining the man and grep commands using UNIX pipes. In Unix, commands can be combined by directing the output of one command (which is normally printed to the console) to the input of the second command (which normally expects an input from the console). The user provides piped commands in the following form to the shell:
< command1 > | < command2 >
In this setting, command1 is executed first as a process. When it terminates, its output is directed to its child process as the input. Then, the child process executes command2 using this input.
For this assignment, you are expected to write a C program that simulates piped execution of the man and grep commands as though they were written in the shell. For this purpose you will use the fork, wait and exec system calls we have seen in the lectures. In addition, you will need to learn and use the pipe system call yourselves for piping the commands. Details about the program, command structure and system calls are provided in the following sections.
2 Problem Description
For this programming assignment, you are asked to implement execution of a shell command admitting the following pattern:
man < command > | grep < option > > output.txt (1)
Here, command and option are variables you will determine as arguments to the man and grep commands, respectively. Once they are fixed and the above command is executed on the shell, it prints lines from the manual page of the command that contain the string option, to a file named output.txt, which will be created through your code. You have to be careful about how to interpret the option variable. It is not an option for the grep command. It should be an option of the command that grep accepts as the search pattern argument. In other words, you want the grep command to find the lines which contain the string you have specified as your <option>. Also, you must determine the number of lines the grep command will pick when it finds a matching string. For most options, the explanation in the man page spans multiple lines. So, you must obtain all of them.
Rules and explanations on how to choose values for command and option variables are explained in Section 3. After you fix these values, the piped command you will simulate will be determined. You are expected to implement your simulation in a single C file called pipeSim.c. Guides and details about the implementation are presented in Section 4.
3 Command Formation & Argument Selection
Before implementing your simulation, you have to pick values for the command and option variables.
Once you fix these variables, your shell command will be determined. In order to inform us about the command you are simulating, you must write the exact command in a file called command.txt. This file will contain a single line which is the command. The command will be approximately in the form of (1).
Your implementation might require adding more options to the man or grep commands. For instance, if you enforce grep to take the next three lines after it finds a matching string, you must change the command in the command.txt file accordingly.
While deciding on variables of the piped command, you should first fix the command variable. You can select one item from the list below. They are some of the most useful command-line utilities for UNIX systems. You can look at their man page and use Google to determine which one is the most interesting one for yo. After fixing the command, you will select an option of the command. All the options of the command can be found under the man page of command.
• ls
• rm
• touch
• find
• grep
• df
• diff
• ping
• wget
• top
Example: We present an example of which command is not selected from
the above list to inspire originality.
selected command: kill selected flag (option) for kill: -L command.txt:
man k i l l −extra Options | grep ”−L” −extra Options > output . txt
Here, − extraOptions are again variables, you might want to use to make sure that your implementation exactly simulates the piped command.
In your submission, we expect to find a pdf file called report.pdf. In this file, you will explain what your command and option does, and why they attracted your attention. Here is an example:
k i l l command k i l l s ( terminates ) a p ro c e s s via sending va r io u s s i g n a l s to the Operating System . −L f l a g i s
f o r l i s t i n g the a v a i l a b l e s i g n a l s in a n i c e ta b l e .
I picked t h i s command and option because . . .
3.1 Niche Problems & Corner Cases
When you execute “man kill | grep “-L”” it will not successfully grep ”-L” from kill command’s man page. The reason is, the character ‘-’ is a special character, so the shell does not interpret this as a part of the search query. You have to solve this problem yourselves.
Again, let’s focus on the given example command man “man kill | grep “L”” for demonstration purposes. After you solve the special character problem, you will see that only the first line of the definition of flag -L will be displayed in the console as the output. The problem here is, actual definition was consisting of 2 lines.
actual definition:
-L, –table
List signal names in a nice table.
grep result:
-L, –table
The second line of the definition was ignored, since the second line was not including the string -L. We expect you to find a solution to this, and print all the necessary lines to output the full definition. You do not have to automatically infer the number of lines you need. Once you decide on the command and option, you can look at the man page of the command, decide on how many lines the option description occupies and could hardcodedly enter the number to your command.txt file and to your code.
4 C Program Implementation
In the pipeSim.c file, you will implement the simulation of the piped command. In Chapter 5 of the OSTEP book, p3.c and p4.c are example command execution simulations of the shell. You should fully understand these programs and the chapter to understand the nature of the shell. Your implementation must agree with the structure of these programs.
Difference of your implementation from the examples in the book is that your implementation will simulate the execution of two commands that are piped. Similar to examples, each command must execute in a separate process. These processes must be descending from the initial process which will simulate the shell. B y descending w e m e a n they are children or grandchildren or grand grandchildren and so on. The initial process should wait until the piped command finishes its execution as the shell would do. The shell process should not contain the bug we have seen in the lecture. It should not print to the console until all the processes descending from it terminate.
In order to implement your simulation as described, you must use fork, wait and exec system calls as in the examples. You should describe the process hierarchy generated by your program in your report (report.pdf).
In addition to these system calls, you have to utilize pipe system call for inter- process communication. Pipe takes two file descriptors as input and connects the parent and child processes using these descriptors. The parent process’ descriptor is in the writing mode and the child’s descriptor is in reading mode. Using these descriptors, the parent can send its output to the child as the input. You can get more information about pipes on this page.
We want to ensure that your program allows correct interleaving of the processes. By correct interleaving we mean that the initial process that represents the shell should wait until all of subprocesses that execute commands man and grep finish. Therefore, each process should print their process IDs to the console (not to any file). Only the shell process should print for the second time after the piped command execution finishes. Below is an example output in the console with correct print ordering:
I’m SHELL process, with PID: 38810 - Main command is: <insert piped command here>
I’m MAN process, with PID: 38811 - My command is: <insert man command here>
I’m GREP process, with PID: 38812 - My command is: <insert grep command here> I’m SHELL process, with PID:38810 - execution is completed, you can find the results in output.txt
1. Compilation (10 pts): Your program compiles, runs and terminates without an error.
2. Process Hierarchy (20 pts): Process hierarchy described in your report repre- sents correct shell simulation. Your description in the report must match with the reality (your C implementation).
3. Successful man (15 pts): Your command.txt contains just a man command without grep and the output of executing this command matches with the output of your implementation.
4. Successful piped command (40 pts): Your command.txt contains a piped com- mand (both man and grep) and the output of executing this command matches with the output of your implementation.
5. Correct interleaving (20 pts): When your implementation is executed, lines printed to the console have the same order with the example execution presented at the end of Section 4.
6. Correct grep option (5 pts): Your implementation and the command in command.txt solves the special character problem described in Section 3.1.
7. Correct number of lines (5 pts): Your implementation and the command prints all the description lines for the given command, option pair as described in Sec- tion 3.1.
The first criterion is a precondition for all the other criteria. Similarly, the fourth item is a precondition for items 5, 6 and 7. It means that you will not get any credits from these items if you cannot get full credits from item 4. Items 3 and 4 are mutually exclusive. You can get points from only one.