Software Lab(CO–393


April 25, 2012



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.



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 finished. The shell you will build is similar to Linux shell but simpler.


Program Specifications

Your basic shell is basically an interactive loop: it repeatedly prints a prompt mysh> , parses the input, executes the command specified on that line of input, and waits for the command to finish. This is repeated until the user types exit. The name of your final 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.


echo . your shell should change the working directory to the path stored in the $HOME environment variable. and then run pwd.g. it should check whether the command is a built-in command or not.Note that the shell itself does not ”implement” ls or really many other commands at all. 4 Built-in Commands Whenever your shell accepts a command.g. So far. Most Unix shells have many others such as cd . it should look like this: % . Instead. you should implement cd and pwd . when a user types pwd. your shell will invoke your implementation of the built-in command. For example. it should not be executed like other programs. you simply call chdir(). Formally this is named as redirection of standard output and standard input./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. The shell provides this nice feature with the ”>” and ”<” characters . etc. you should simple overwrite the file (after truncating it). All it does is find those executables and create a new process to run them. Hence. you simply call exit(0). pwd . you have added your own exit built-in command. More on this below. If it is. your shell should also include this feature. In this project. nothing should be printed on the screen. 2 . ”ls >” ). For example. When you run cd (without arguments). Use getenv("HOME") to obtain this. When a user changes the current working directory (e. Instead. 5 Redirection Many times. to implement the exit built-in command. you simply call getcwd(). Basically. if you run your shell. ”cd somepath”). in your C program. the output of the ls program should be rerouted to the output file. If the output file is not specified (e. To make your shell users happy. if a user types ls -la /tmp >output . If the ”output” file already exists before you run your program. a shell user prefers to send the output or receive the input of his/her program to or from a file rather than to the screen or from the keyboard . you should also print an error message. A good tutorial for beginners in Linux/ Resources Various short notes are available on this site related to Linux programming environment. 7 References http://pages. html 3 .surrey.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.