Department of Information Technology

Unix Shell

Description

This assignment is about using the POSIX Application Programming Interface (API) to do process management.
BashShell.png

You are to program a UNIX shell. A shell is an interface between a user and the operating system. It lets us give commands to the operating system and start new programs. A shell uses several POSIX system calls, for example fork, execve, and wait.

The final version of your shell should be able to fork at least two processes and connect them with pipes.

Preparation

Before you continue, you should have read the following sections in the textbook "Operating System Concepts", 8th edition, Silberschatz, Galvin, Gagne:

3.1 - 3.3
Read about processes, process creation (fork) and process termination (exit and wait).
3.6.3
Read about pipes.

Your task

Follow these instructions.

These slides gives some more details.

To make your task (and the teacher's) easier, the work is decomposed in four steps. Don't rush yourselves - follow the instructions and complete each step before continuing with the next step.

You may choose to use C or Java:

Language Download Get started
C shell.c To compile: g++ -Wall -Werror -o shell shell.c
TUTORIAL_pipe_execvp_dup2.tar.gz Unpack: tar xvfz TUTORIAL_pipe_execvp_dup2.tar.gz
Java lab1.solaris.tar.gz Unpack: tar xzvf lab1.solaris.tar.gz
upgrades.tar.gz Unpack: tar xvfz upgrades.tar.gz

Comments

Read the instructions carefully - there is a lot of helpful information there to guide you.

Nevertheless, somethings may not be clear and therefore you should read these comments along with the instructions. If you use Java you must also download some minor upgrades to the assignment package - see below.

Get started

  • Start by downloading and open up the instruction document.
  • Section 1 gives some background but is not necessary to complete the assignment.
  • Section 2 starts by describing the scope of the assignment.
  • Section 2.1: You may choose to do this assignment in C or Java (or any other programming language of your choice as long as you can do POSIX calls). If you know some C it should be the preferred choice.

TUTORIAL

Download and unpack the Tutorial about pipe(), execvp() and dup2().

Read the README file for further instructions. Even if you decide to use Java for the assignment you should look at the tutorial - you don't need to actually know much C to understand the tutorial.

Java

If you choose to use Java for this assignment you must make sure the jtux library is compiled and installed. The Java version of this assignment have been prepared to work on the department Unix system - but only for the Sparc architecture.

  • Open up a command shell.
  • From the shell promt, give the following command:

      $> uname -p

This command will tell you the current host's ISA or processor type. If you are logged in from one of the Sun Ray stations, the output from the uname command will most likely be sparc and you don't have to worry.
  • If the result from uname tells you i386 the prepared Java version will not work. The easies way to get around this problem is to use telnet to log on to a Sparc Unix server such as hamberg or triangeln:

      $> telnet hamberg
      Trying 130.238.18.44...
      Connected to hamberg.it.uu.se.
      Escape character is '^]'.


      SunOS 5.9

      login: 
      Password: 
      Last login: Tue Jan 27 12:44:11 from jeltz.it.uu.se
      Sun Microsystems Inc.   SunOS 5.9       Generic May 2002
      $> uname -p
      sparc
      $> 

Ok, you are running on a Sparc server, time to download the assignment package.

  • In your home folder, create a directory called OS_2009.
  • Download the tar ball to this directory.
  • From a command shell, unpack the assignment:


      $> tar xvfz lab1.solaris.tar.gz 

  • Once you unpacked the assignment all necessary files for the assignment are in the OS_2009/lab1 directory.

Java upgrades

  • Extract the upgrades: tar xvfz upgrades.tar.gz

Step 0

Before you continue, make sure you can compile and run the unmodified Shell.java skeleton program. We use the make utility to compile and run all our Java programs in this assignment.

  • To compile the Java sources give this command from the shell: make
  • You should now be able to run the skeleton program from the shell: make test0
    • You could also use make test0 directly. This will automatically recompile your code if you change it and then run the program.

Step 1: fork()

Java:

  • Make a copy of the Shell.java skeleton file named Shell_step_1.java
  • Inside Shell_step_1.java, change public class Shell to public class Shell_step_2
  • Use make step_1 to compile and make test1 to run your program.

Hand in:

  • You must hand in using Ping Pong
  • The file you hand in should be named Shell_step_1.java or step1.c
  • Answers to questions must be given in the comment field in Ping Pong.

Step 2: wait()

Java:

In the instructions you are instructed to "declare a variable of type UExitStatus...". To wait for a child process you should do something similar to:

UExitStatus status = new UExitStatus();
UProcess.wait(status);

  • Use make step_2 to compile and make test2 to run your program.

Hand in:

  • You must hand in using Ping Pong
  • Answers to questions must be given in the comment field in Ping Pong.

Step 3: execvp()

The exec family of functions replaces the current process image with a new process image.

Java:

  • Use make step_3 to compile and make test3 to run your program.

Hand in:

Step 4: pipe()

Before you continue you should now what is meant by STDIN and STDOUT, the standard input and output streams. You should also be familiar with the concept of I/O Redirection.

After you read about pipes in the instructions you should also download the tutorial to learn more about pipe(), execvp() and dup2().

More information about pipes:

The important thing to get right when working with pipes is to keep track of all the file descriptors connected to to the pipe. For example, what do you think should happen if you try to write to a pipe that no one is listening to (no reader attached)?

A summary of the results of reading or writing to a pipe in various situations is given in the following table:

Attempt Conditions Result
read Empty pipe, writer attached Read blocked
write Full pipe, reader attached Write blocked
read Empty pipe, no writer attached EOF returned
write No reader SIGPIPE

Java:

  • Use make step_4 to compile and make test4 to run your program.

Hand in:

Learn more: If you use a Unix shell such as Bash you can do I/O redirection directly at the command line. This can be very useful but is not something your shell implementation need to address.

Updated  2009-02-13 13:10:54 by Karl Marklund.