Spring 2007

Clay Shields


front | classes | research | personal | contact

Project 1 - Part 2: Writing a User Shell

Project 1: The Shell - Part 2


Starting from the command table produced in Part 1, in this part you will execute the simple commands, do the file redirection, piping and if necessary wait for the commands to end.
  1. For every simple command create a new process using fork() and call execvp() to execute the corresponding executable. If the _background flag in the Command struct is not set then your shell has to wait for the last simple command to finish using waitpid(). Check the manual pages of fork(), execvp(), and waitpid(). Also there is an example file that executes processes and does redirection in cat_grep.cc. After this part is done you have to be able to execute commands like:
  2.              ls -al

                 ls -al /etc &

  3. Now do the file redirection. If any of the input/output/error is different than 0 in the Command struct, then create the files, and use dup2() to redirect file descriptors 0, 1, or 2 to the new files. See the example cat_grep.cc to see how to do redirection. After this part you have to be able to execute commands like:
  4.              ls -al > out

                 cat out

                 ls /tttt >& err

                 cat err

                 cat < out

                 cat < out > out2

                 cat out2
                 ls /tt >>& out2
    ">& file" redirects both stdout and stderr to file.
    ">>& file" append both stdout and stderr to file.
    ">> file" appends stdout to file.
  5. Now do the pipes. Use the call pipe() to create pipes that will interconnect the output of one simple command to the input of the next simple command. use dup2() to do the redirection. See the example cat_grep.cc to see how to construct pipes and do redirection. After this part you have to be able to execute commands like:
  6.              ls -al | grep command

                 ls -al | grep command | grep command.o

                 ls -al | grep command

                 ls -al | grep command | grep command.o > out

                 cat out
  7. Your shell has to ignore ctrl-c. When ctrl-c is typed, a signal SIGINT is generated that kills the program. There is an example program in ctrl-c.cc that tells you how to ignore SIGINT. Check the man pages for sigset.

  8. You will also have to implement also an internal command called exit that will exit the shell when you type it. Remember that the exit command has to be executed by the shell itself without forking another process.
                 myshell> exit

                   Good bye!!

                 csh>
  9. Implement the cd [ dir ] command. This command changes the current directory to dir. When dir is not specified, the current directory is changed to the home directory. Check "man 2 chdir".

  10. When your shell uses a file as standard input your shell should not print a prompt. This is important because your shell will be graded by redirecting small scripts into your shell and comparing the output. Use the function isatty() to find out if the input comes from a file or from a terminal.

Submission Instructions

The deadline of this part of the project is March 29, 2007, before class. Follow these instructions to submit.

1. Login to trogdor.cs.georgetown.edu.

2. cd to shell-src and type make clean

3. Type make to make sure that your shell is build correctly.

4. Type make clean again.

5. cd one directory above shell-src by typing "cd .."

6. Create a tar file named <user_name>.tar, where <user_name> is your trogdor.cs.georgetown.edu login, by typing

tar -cf <user_name>.tar shell-src

7. Gzip the tar file by typing

gzip <user_name>.tar

8. Since this timestamp will be used to verify whether the work was completed on time or not, you should set the permissions on the file you submitted to make sure that the file timestamp is not changed. So this by typing:

chmod a-w <user_name>.tar.gz

9. Mail the gzipped tar file to clay at cs dot georgetown dot edu as an attachment.