Вы находитесь на странице: 1из 3

Software Lab(CO393)

April 25, 2012

Objective

There are three objectives to this assignment: (a)To familiarize yourself with the Linux programming environment.(b) To learn how processes are created, destroyed, and managed i.e use of fork(), exec() system calls .(c)To gain exposure to the necessary functionality in shells.

Overview

In this assignment, you will implement a command line interpreter or, as it is more commonly known, a shell in Linux or command prompt in Windows. The shell functions in this basic way, when you type in a command (in response to its prompt), the shell creates a child process that executes the command you entered and then prompts for more user input when it has nished. The shell you will build is similar to Linux shell but simpler.

Program Specications

Your basic shell is basically an interactive loop: it repeatedly prints a prompt mysh> , parses the input, executes the command specied on that line of input, and waits for the command to nish. This is repeated until the user types exit. The name of your nal executable should be mysh : prompt>./mysh mysh> You should structure your shell such that it creates a new process for each new command (there are a few exceptions to this, which we will discuss below). The advantage of creating a new process is that, it protects the main shell process from any errors that occur in the new command. Your basic shell should be able to parse a command, and run the program corresponding to the command. For example, if the user types ls -la /tmp , your shell should run the program ls with all the given arguments and print the output on the screen.

Note that the shell itself does not implement ls or really many other commands at all. All it does is nd those executables and create a new process to run them. More on this below.

Built-in Commands

Whenever your shell accepts a command, it should check whether the command is a built-in command or not. If it is, it should not be executed like other programs. Instead, your shell will invoke your implementation of the built-in command. For example, to implement the exit built-in command, you simply call exit(0); in your C program. So far, you have added your own exit built-in command. Most Unix shells have many others such as cd , echo , pwd , etc. In this project, you should implement cd and pwd . When you run cd (without arguments), your shell should change the working directory to the path stored in the $HOME environment variable. Use getenv("HOME") to obtain this. Basically, when a user types pwd, you simply call getcwd(). When a user changes the current working directory (e.g. cd somepath), you simply call chdir(). Hence, if you run your shell, and then run pwd, it should look like this: % ./mysh mysh>pwd mysh>/home/quamar/Desktop/OperatingSystem/assignment mysh>cd mysh>pwd mysh>/home/quamar For the other commands you have to use execvp() system call.

Redirection

Many times, a shell user prefers to send the output or receive the input of his/her program to or from a le rather than to the screen or from the keyboard . The shell provides this nice feature with the > and < characters . Formally this is named as redirection of standard output and standard input. To make your shell users happy, your shell should also include this feature. For example, if a user types ls -la /tmp >output , nothing should be printed on the screen. Instead, the output of the ls program should be rerouted to the output le. If the output le already exists before you run your program, you should simple overwrite the le (after truncating it). If the output le is not specied (e.g. ls > ), you should also print an error message.

Resources

http://www.ee.surrey.ac.uk/Teaching/Unix/, A good tutorial for beginners in Linux/Unix. http://cslibrary.stanford.edu/, Various short notes are available on this site related to Linux programming environment.

References

http://pages.cs.wisc.edu/~remzi/Classes/537/Spring2012/Projects/p2a. html

Вам также может понравиться