Operating Systems - Projects and Exercises

Exercise 1 - The Operating System Shell

The first project will be for you to write your own shell to replace the standard Operating System shell.

The Shell or Command Line Interpreter (CLI) that is your environment when you logon to a UNIX system is usually the TENEX/TOPS/Thomas C Shell (tcsh), a completely compatible version of the Berkeley UNIX C shell, csh. It is a command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor, programmable word completion, spelling correction, a history mechanism, job control, i/o redirection, and a C-like syntax (see http://www.tcsh.org/Home for the full description of the ongoing development of tcsh).

Read Glass (UNIX for Programmers and Users) Chapters 3 and 6 and the tutorial on Shell Programming: shells.html with particular reference to the sections on i/o redirection and process control

Write a small program, sleepy, that gets a loop count from the command line:

sleepy n

where n is the number of seconds for which the program should run. Implement this timing by putting a loop n times of sleep(1) - this will put the program to sleep for one second n times before exitting.

#include <unistd.h>
...
unsigned int sleep(unsigned int seconds);

This function causes the calling process to be suspended until either

  1. the amount of wall clock time specified by seconds has elapsed, or
     
  2. a signal is caught by the process and the signal handler returns.

The return value from sleep is 0 or the number of unslept seconds if the sleep has been interrupted by a signal interrupt.

In each loop print out the process ID and the loop count so that that particular process can be identified.

The process ID can be obtained from the getpid function:

#include <sys/types.h>
#include <unistd.h>
...
pid_t getpid(void);

This function returns the process ID of the calling process.

The process ID is returned as a type pid_t which is actually an integer so it can be treated as such in a printf format statement (use an int cast to avoid a compile warning from gcc).

Use this program to investigate the process control of the shell provided on your UNIX system.

In particular learn how to:

  1. list all current active processes (see Process Control in the C-Shell )
  2. suspend an active process
  3. put an active process into background mode
  4. put an active process into foreground mode
  5. also remind yourself about i/o redirection and piping

Code should be in 'straight' C using the compiler of your choice (cc or gcc).

Always use nice to execute your test programs at lower priority to ensure they do not inconvenience other users if they go 'haywire'. e.g.:
 
   >nice a.out

The descriptions of the system functions above are drawn from sources that include manuals on the Sun Solaris system and the MAC OS X Darwin/BDS system, and also from 'Advanced Programming in the UNIX Environment', W. Richard Stevens, Addison-Wesley, 1993.

Go home top

For use only by students and instructors using the supplementary material available with the text book: "Operating Systems - Internals and Design Principles", William Stallings, Prentice Hall, 5th Edition, 2004. Not to be printed out or copied by any other persons or used for any other purpose without written permission of the author(s).

©