Lab 1 - A Simple UNIX Shell
COP4610/CGS5765, Introduction to Operating Systems, Fall 2003
Department of Computer Science, Florida State University
Points: 100 points.
Due: Week 6, Tuesday, September 30, 2003.
Purpose:
To know how to use Unix system calls to create new processes,
deal with signals, and have working knowledge of developing a
command-interpreter interface, i.e., a shell program.
Assignment:
Write a Unix shell program which acts as a command-interpreter
interface between the UNIX operating system and the user using system calls.
Some commands will be built into the shell itself, which means that those
built-in commands only change internal status of your shell program.
Commands that are
built into the shell are:
- mycd - Change current working directory.
The new directory would be given
by an optional command-line argument. When no argument is given,
it should change to the user's home directory.
- mypwd - Return the current working directory name.
- myecho - Print out the input directly. However, when prefixed by '$'
sign, the following word should be echoed as the corresponding environmental variable.
For example, "echo $HOME" should output the current home directory.
- myhistory - Shows the last number of commands specified by an optional
command-line argument. When no argument is given, show all the saved previous commands. You can assume
a circular buffer of 100 commands.
- mysetpath - Set the environment variable MYPATH.
The new path will be given by an optional command-line argument. When
no argument is given, MYPATH is set to an empty string.
It then
shows the current MYPATH.
In this lab, MYPATH will be a string of directories separated by
':' such as "/bin:/usr/bin". Initially MYPATH is set to an empty
string.
- myaddpath - Add the given directory at the end of
the environment variable MYPATH. It accepts one required command-line
argument. If the given argument is a directory, add it to MYPATH;
otherwise, report an error and
do not add it to MYPATH. In either case, your shell then shows the
current MYPATH.
- myexit - quit from the program.
Any additional input is assumed to be an executable program with the
following
format:
command argument_1 argument_2 ....
A command may not require any arguments. Here an empty line is also
a legal input and your shell should be simply ready to
accept another command.
If the input command contains a slash character, the corresponding file
will be used directly.
Otherwise,
your shell program will first try to find the program by
searching through the directories specified in MYPATH first.
Then your shell will
attempt to run the program (using fork and execve system calls
to create a child process).
For external commands, your shell program should allow the
following features.
- Both foreground and background executions are allowed.
'&' at the end of a command
is used to indicate background execution (in other words, the parent
will not wait for the child process to finish). Commands without '&'
are assumed to run in foreground mode.
- Your shell program should allow I/O redirection. '<' is used to
redirect the standard input from a file and '>' is used to redirect
the standard output to a file.
- Your shell program should allow
a pipe between two commands indicated by '|', .i.e, the output from
the first program
is used as the input to the second program.
- Your shell should allow a user to run two commands
separated by ";"
sequentially in one command line.
For example, if a user types in "ls ; ls -l", your program should first
execute "ls" and then "ls -l".
- A previous command can be repeated by using '!' as the first character
in the current command followed by the
corresponding number in the command history or part of the command.
For example, suppose a command "sleep 100" appeared previously,
one can repeat this command using "!sl". In case there are more than one
match, repeat the most recent matched one. In case there is no match, report
an error "Command was not found in the history."
For the basic requirement, no more than one special control
character ('&', '<', '>', '|', ';') (except '!') is allowed in any command and
these special control characters only apply to external commands.
You can also assume that there will be at least one white space before and after
each special control character unless it is the very last one in the command
line.
Additionally, your shell should be able to handle CTRL-C
in the following way.
If a program is running in the foreground mode,
your shell should ignore signal CTRL-C.
However when no program is running in the foreground mode,
your shell should abort the user's
current input and start to accept a new command.
Your shell should use the same syntax as csh, a popular Unix shell,
which means
that your shell should behave similarly
to csh given the samilar command.
As in UNIX shells, these commands are case-sensitive. For example,
your shell program shall NOT accept "MYCD" as a built-in command
but as an external command.
Submission:
- Report - You should turn in a report explaining how you have
implemented your shell and how you have handled some of the problems
you had.
You should state clearly in your report where your programs
are located and the names of your programs.
Your report must start with the following information:
- User name:
- Source code file with the path starting with your home directory:
- Executable program with the path starting with your home directory:
- Extra credit options:
As an example, my report will start with the following information:
- User name: liux
- Source code:
~liux/public_html/courses/cop4610/examples/simple-shell.cc
- Executable: ~liux/public_html/courses/cop4610/examples/simple-shell
- Extra credit options: Multiple Special Control Characters.
- Source code - You should
attach all the source programs you developed
for this lab. You need to make sure that the copy you turned in is
exactly the same as the one you compiled and generated your executable
program.
ANY INCONSISTENCY IN THESE PROGRAMS WILL BE TREATED AS
CHEATING.
- Test cases - The results your shell generated for given
commands.
Extra Credit:
Please state clearly in your report if you have implemented one or both
of the
following options for extra credit.
- Multiple Special Control Characters(10 %) -
Your shell program should handle all legal combinations of
special control
characters ('&', '<', '>', '|', ';') in one command line.
As one example, your program should allow "ls | cat > tmp.out", or
"ls -l > tmp.out ; cat < tmp.out > tmp.out2 ", or "ls | cat | wc".
- Command Hashing (10 %) - Your shell will
create a hash table for all the executable programs in the directories
specified in MYPATH. When an external command that does not contain
a slash ('/') is given, instead of searching through the directories,
your shell should find the location directly from the hash table.
The hash table needs to be updated when myaddpath or mysetpath is used.
For this option, you need to implement
the following internal command.
- myhash - Show the hash location. It accepts
one optional argument. If there is no command-line argument,
it displays the entire hash table. Otherwise, it shows the hash location
of the given command.
Grading:
- Report, Source Code, and Test Cases -- 30 points.
- Fail to include Source Code results in a penalty of 10 points.
- Fail to include test cases results in a penalty of 10 points.
- Correct Execution -- 70 points. The following
is the penalty for each failure.
- Fail to implement an internal command correctly -- 5 points.
- Fail to run an external command with absolute path -- 10 points.
- Fail to use execve (for example use execvp and do not search
the directories in MYPATH) -- 20 points.
- Fail to implement a special control character -- 5 points.
- As a shell, your program must be robust, meaning that your program
can handle all situations gracefully.
Penalty for failure to finish for legal inputs (such as core dump) is 20
points.
- Multiple Special Control Characters -- 10 points
- Command Hashing -- 10 points
Demonstration: For grading purpose, you will be required
to schedule a time slot to demonstrate your program for the TA.
One week's recitation sessions will be used for this demonstration purpose.
If you fail to demonstrate your program before the deadline (given later),
your program will be graded based on your source code and worst cases
will be assumed.
Test Cases:
In general, for this lab,
you should compare the behaviors of your program with
a csh program for similar commands. A detailed test plan will be
made available so that you can test your program and the same test plan will
be used for grading.
Additional Information:
- The textbook provides a good coverage of how to implement a shell
program. It is on pages 76-82 and again on pages 371-373.
- The following system calls/functions are helpful.
- fork
- execve
- getenv
- setenv
- getcwd
- chdir
- exit
- wait
- dup
- pipe
- close
- open
- man page for hash command