lunduniversity.lu.se

Computer Science

Faculty of Engineering, LTH

Denna sida på svenska This page in English

Lab1: Implement a Simple Shell

Introduction

The shell is the interface provided by an operating system to its services. In this assignment, you will implement a simple shell for an UNIX-based operating system. Your shell will provide basic functionalities such as the ability to run multiple programs, directory tree navigation, I/O redirection and inter-process communication.

In contrast with the other assignments for this course, in this one you will develop a shell that runs on a UNIX system, not on PintOS.

Learning outcomes

You will get an understanding of how a simple UNIX shell can be implemented. You will become familiar with pipes, I/O redirection, environment variables, etc.

Assignment

Task Read the man pages for the following system calls (Linux: man -S 2 fork etc, Solaris: man -s 2 fork etc).

  • access
  • close
  • dup2
  • execv
  • exit
  • fork
  • open
  • pipe
  • wait and waitpid (it might be more convenient to use use waitpid instead of wait in your implementation).
  • chdir and getcwd (to implement cd)

Task Fetch the incomplete UNIX shell source code and ensure that it builds:

  1. First download the source code of the incomplete UNIX shell.
  2. Decompress and untar the file and go to the directory lab1.
  3. When you build your shell after having edited a source file, just type make (not make sh).
  4. During the lab you need to change two functions in the file sh.c: parse_line and run_program.

Task Implement the functionality required to run a single program. Modify the run_program function such that you will be able to execute:

  • % /usr/pwd

Use the fork and wait system calls. Your implementation must handle both programs that exist in the $PATH environment variable and programs that are given explicitly via their path. Check that the current user can access and execute the program file (use the access function). The global variable path_dir_list is a circular list containing the directories in the $PATH variable.

Task Extend the shell such that it can run a sequence of programs. Modify the gettoken function to handle ; between commands. You should be able to execute:

  • % ls ; ls

Task Add directory tree navigation by implementing the cd command. The following commands should work:

  • % cd DIR - change current directory to DIR
  • % cd - - change to the previous working directory

Observe that cd is a shell built-in, not a program in $PATH.

Task Implement I/O redirection Allow for the standard output of a program to be redirected to a file and for the contents of a file to be redirected to the standard input.

  • % ls > filelist
  • % cat < filelist

The dup2 system call might be useful.

Task Implement inter-process communication using pipes Redirect the output from one process to the input of another process.

  • % ls | wc
  • % ls | cat | cat | cat | wc

Use the pipe and close system calls. Small changes in the parse_line(void) function might be needed.

Hand-in

  • The modified shell source code implementing all the functionality described above.

Hints

  • This assignment does not require writing any assembly code.
  • The approximate size of the code changes is:
lab1/sh.c     | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------