Unformatted text preview:

1 PurposeThe purp ose of this assignment is to learn about processes and using systemcalls on a UNIX system. This is a very good chance to understand how yourshell operates.2 ExercisesThis assignment will be broken down into 8 parts. This breakdown is just soyou can get a handle on the entire assignment. You only need to turn in thelast part since it will include all the functionality from the previous parts.2.1 Simple shellTo start you should create a program that serves as a simple interactive shell.This shell should read the command line and try to run the program containedthere. For example,% / usr / bin / l s% / usr / bin / date% / usr / bin / uptimeYour shell should not look for another command to run until the previouscommand has finished. Use the waitpid() system call to be able to know whenthe command is completed.2.2 Built-in commandsNow you are ready to build some commands that the shell needs to understand.Your shell needs to be able to do at least the following commands:• cd changes the working directory of the shell. Basically, this is a wrapperfor the chdir() system call.• exit stops the shell’s execution• pwd displays the current working directory. This should be a wrapper forthe getcwd() system call.• version displays the version of the shell. T his should include your nameat a minimum.2.3 Commands with argumentsYour shell should be augmented so that it can run programs with arguments.These arguments will be passed to the programs in the argv part of main()(i.e., int main(int argc , char ∗∗argv)). For example,1% / usr / bin / l s − l% / usr / bin / echo Testing A B C% / usr / bin / cat . b a shrc . b a s h p r o f i l eArguments are always separated by whitespace. You do not have to dealwith quotes to try to include spaces within the arguments.2.4 Commands with relative pathsNext, your shell should be able to find the location of an executable by searchingthe PATH environment variable. You can use the getenv() system call to findthe value an environment variable. The format of the PATH variable is a listing ofdirectories to search separated by colons. You should search the list in the orderthey appear in PATH. Thus, if PATH is /bin:/usr/bin:/usr/local/bin, andthe command that was entered was ls, you should try to run /bin/ls, followedby /usr/bin/ls, followed by /usr/local/bin/ls. Do not try to execute lswithout a leading path. Also, do not try to execute ./ls unless . is part of thePATH variable.The PATH variable should be searched for any command that uses a relativepath. Absolute paths should always ignore the PATH variable. Absolute pathsare indicated by a command that begins with /, ., or .. . These indicate theuser explicitly does not want to check PATH.% l s% ./ a . out% echo Testing A B C2.5 Background commandsIf the command line has a trailing &, you should immediately try to look foranother command to run. The shell should reap this command when it can, s oyou still need to use the waitpid() system call, but with different parameters.Your program should be able to handle at least 5 commands in the background.If your shell cannot handle another background command and the user asks forit, you must give an appropriate error message. For example:% ./ a . out &% ./b . out &% ./ c . out &% ./d . out &% ./ e . out &% ./ f . out &Too many background p r o c e s s e s%It should be very easy to handle more than 5 commands, and I encourageyou to develop a data structure that is more flexible. Five is just the absoluteminimum.22.6 Input redirectionIf the command line has a <, you should use the information preceding the <as the command, and the information following as a file name. For simplicity,the command line will have whitespace separating the parameters, the <, andthe file name. The file name should be used as standard input to the commandbeing run. For example,% cat < input% mail < t ex t% grep s e c r e t < c o n f i d e n t i a l2.7 Output redirectionIf the command line has a >, you should use it like the < except that the filename should be used to capture the standard output of the command. Notethat you will have to do a little more pro ce ss ing at this point because you maysee a command with both < and >. For example,% l s > f i l e s% echo < inp u t > output% grep s e c r e t > v i o l a t i o n < c o n f i d e n t i a lYou should use the open() system call to use the output file. If it does notexist, the permissions should be the same as the current working directory. Ifthe file does exist, its contents should be erased and replaced with the outputfrom the command.2.8 PipesIf the command line has a |, you should split the command into two parts. Thefirst part (before the |) should be command and optionally a set of arguments.Your program should interpret these as it normally does. The second part(after the |) is also a command with optional set of arguments that should beinterpreted normally. The difference is that the output of the first part shouldserve as the input to the second part. Remember to use the pipe() system calllike we discussed in class. For simplicity, a command with a | will not containa < or a >. For example,% l s − l | awk { p r i n t $5 }% ps − e f | grep r oot% f i n d . − name ∗ . cpp | x a r g s grep TODO3 Helpful hintsAgain, the man pages are going to be very helpful to you. You will get the exactformat of the commands along with the parameters they should be passed andthe behavior of the command.3Your textb ook gives a simple skeleton to start with. You may wish to use itto start off with. It is not necessary.4 ConstraintsI have listed a bunch of simplifications in the command line to try to make iteasier to parse. You may try to make it accept more sophisticated input if youwish.This assignment is probably the most common CS assignment for OperatingSystems, and you will be able to find lots of code on the web to do this stuff.In fact, the code for all the commonly used shells is open source. Don’t cheatby using those online resources that give you code.You may only use C or C++ to code this


View Full Document

TAYLOR COS 421 - Shell

Download Shell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Shell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?