CSE 451
|
Autumn 1998 |
This is a group assignment.
Every shell prints out a prompt, reads a line of input, parses it into the program name and the parameters, fork's off an new process, exec's the program and wait's for the program to complete. It does this in a loop. Although most of the commands, you type on the prompt, are names of other UNIX programs (like ls, more etc.), shells recognize some commands (called internal commands) which are not program names. Examples are exit (which terminates the shell), cd (change directory) etc. The shells directly make the system calls to execute these commands.
For example, when /bin/ls mydir1 mydir2 is typed at the prompt of csh, the command line is seperated into the program name /bin/ls, and two arguments: mydir1 and mydir2. The shell then forks off a new process and the child execs /bin/ls and passes it the arguments mydir1 and mydir2. The parent process waits (by executing a wait() call) for the child to terminate. The /bin/ls program prints out the directories and exits. Then the parents process continues, prints out a new prompt and waits for the next command.
If an internal command, like cd mydir is typed, then the shell calls the system call chdir(mydir) to change the current directory of the shell, prints out a new prompt and waits for the next command. Therefore fork() and exec() are not used for internal commands.
The output produced by your shell should look like the following:
CSE451Shell% /bin/cat /etc/motd
DEC OSF/1 V3.2 (Rev. 214); Thu Feb 22 08:48:40 PST 1996
DEC OSF/1 V3.2 Worksystem Software (Rev. 214)
This is an AFS fileserver. Please run long running jobs (hours)
or memory
intensive jobs elsewhere.
CSE451Shell% /bin/date
Sun Apr 5 22:51:50 PDT 1998
Note: The words in bold are output by the shell and
the words underlined are typed in by the user.
Please take a look at the manual pages of execv, fork
and wait.
To allow users to pass arguments to programs you will have to parse the input line into words separated by whitespace (spaces and'\t' tab characters) and place these words into an array of strings. You might try using strtok() for this (man strtok for a very good example of how to solve exactly this problem with strtok). Then you'll need to pass the name of the command as well as the entire list of tokenized strings to one of the other variants of exec, such as execvp(). These tokenized strings will then end up as the argv[] argument to the main() function of the new program executed by the child process. Try man execv or man execvp for more details.
To turn in your solution, use the following:
To submit all the files in the current directory:
Remember to use only orcas or sanjuan to turnin files.