Sie sind auf Seite 1von 3

NATIONAL UNIVERSITY OF COMPUTER & EMERGING SCIENCES

OPERATING SYSTEMS (CS 206)


SPRING 2011
ASSIGNMENT #5
SUBMISSION DEADLINE: FRIDAY, 17TH MARCH 2011 10:00 PM ON KHI PORTAL

Note: Make sure you submit your own work. Any form of collaboration will not be tolerated and case will be
forwarded directly to the Disciplinary Committee or in the best case, -100 policy (Remember?).

Introduction

In this assignment you'll write a simple interactive shell similar to bash and tcsh. The program will print a
command prompt to indicate that the user can enter command. When the user enters the command, the
shell will attempt to execute it with the provided arguments and then print another command prompt. If
the user enters the exit command, the shell will quit.

Example Run

iShell$ ls
somefile anotherfile yetanotherfile

iShell$ echo foo bar baz foo bar


baz

iShell$ exit

Reading the user input (10 marks)

There are several ways to read a line of user input. Keep in mind that a command may have a several
arguments. You may use the function gets even though it can be unsafe in different circumstances. A better
solution would be to use fgets with stdin as the input stream.

Parsing the user input (10 marks)

When you've read a command you need to split the actual command and the arguments. You can do this
using the strsep function. The function returns a string containing the first word in the supplied string, while
the supplied string itself will have the first word removed. For this assignment you'll assume space(“ “) as the
argument delimiters. Moreover, newline(“\n”) can be used as line delimiter.

You may also combine this step with the first step in any way you like. Just beware that reading the input
character by character takes quite some time to program. Learning to use standard functions can save you a lot
of time and is an important skill to develop. Refer to man-pages or websites for information on functions you
don't know or understand.
Running the command (30 marks)

Once you've parsed the command, it's time to execute it. This can be done using the function execv. This
function takes two arguments. The first is the path to an executable, while the second is an array containing all
the arguments. This function turns the current process into another process. Doing so allows us to execute a
command, but in return we lose our shell process. The solution is to use the function fork, which makes a copy
of the current process. When fork is called we have two nearly- identical processes, both at the same point in
the code. The only difference between the two is the return value of the fork function. The original process will
get the process id of the new process, while the new process gets a return value of 0 (zero). We can use an if-
statement to have the new ('child') process call execv to run the command, while the original ('parent') process
can call the wait function to wait for the child to finish.

Running the programs that aren't in the current directory (15 marks)

Using execv we can run any executable, but unless it's located in the current directory we have to supply a full
path. For example, ls can only be executed by entering /bin/ls. You'll now add the ability to run commands
from several standard locations. Add an array of paths to your code ({“.”, “/bin”, “/usr/bin”}). Now, when the
user enters a command, try to execv “./command”, “/bin/command”, “/usr/bin/command”, until execv is
successful.

You can concatenate a path string and a command string using the strcat function or any other method
you like.

Command History (25 marks)

A history of previous commands executed should be maintained. The user can access this list through the
direction keys on the keyboard. The user can also access the history through "!" key. "!!" meaning that the last
command should be executed. "!1" meaning the second last command, !2 meaning the third last command and
so on. If "gcc input.cpp" is a command that has been previously executed then doing "!g" should execute it.

Report (10 marks)

Write a short report detailing how you went about writing the shell and what problems you
encountered. In your report, also answer the following questions:

1. Look at the man page for execv (man execv). Can you explain why there are several exec
functions?
2. You should be using the wait function. What does this function do and how does the
shell function without it?
3. What are the advantages of a shell designed using the fork and exec functions?
4. In a regular shell (not the one you wrote) run the following command:
echo $PATH
What is this path variable? How do shells like bash use this variable?
Deliverables

1. The source code of your shell. Make sure it’s properly commented and indented.
2. Your report, including the answers to the questions 1-4.

Make sure each deliverable contains the names of the student ID. Put all files you want to deliver into
a separate directory (e.g., 09-2222), free of object files and/or binaries, and create a zipped file of this
directory.

Submit your zipped file at khi.nu.edu.pk portal.

Good luck!

Das könnte Ihnen auch gefallen