You are on page 1of 4

CPSC 503 Operating Systems Fall 2015

Project 1: UNIX Shell and Program Selection Feature


Objectives:
1. Understand one commonly used process model to execute program instructions.
2. Understand the effect of practical factors on implementing OS utilities.
3. Commonly used means to communicate with both the OS and the user.
4. Reasonable ability to implement OS functionality using C.
5. Low-level programming development and debugging skills.
6. Gain experience with an industry standard OS such Unix and Linux

Due: Sunday, October 18, 2015. 11:59:59 PM (EST)

This will be individual projects. The main purpose of this project assignment, i.e. Project 1, is to
design and implement a process model and OS utility, i.e., Shell. In this project, you can use only
C programming language with gcc compiler under Debian Linux VM used in exercise lab.
Therefore, there is no requirement of GUI.

This project consists of designing a C program to serve as a shell interface that accepts user
commands and then executes each command in a separate process. The shell provides the user a
prompt after which the next command is entered. The example below illustrates the prompt
ubos> and the users next command: i.e., cat prog.c (This command displays the file
prog.c on the terminal using the UNIX cat command)

ubos> cat prog.c

#include <stdio.h>

One technique for implementing a shell interface is to have the parent process first read what the
user enters on the command line (in this case, cat prog.c), and then create a separate child process
that performs the command. Unless otherwise specified, the parent process waits for the child to
exit before continuing. This is similar in funcationality to the new process creation illustrated in
Figure 3.10 (text book) and Exercise Lab.

The separate child process is created using the fork() system call and the users command is
executed by using one of the system calls in the exec() family (see Section 3.3.1 in the
textbook).

A C program that provides the basic operation of a command line shell is given below. The
main() function presents the prompt ubos> and outlines the steps to be taken after input from
the user has been read. The main() function continuously loops as long as should_run=1;
when the user enters exit at the prompt, your program will set should_run=0 and terminate.

#include <stdio.h>
#include <unistd.h>
#define MAX_LINE 80 /* 80 chars per line, per command */

int main(void)
{
char *args[MAX_LINE/2 + 1];
/* command line (of 80) has max of 40 arguments */
int should_run = 1;

int i, upper;

while (should_run){
printf("ubos>");
fflush(stdout);

/**
* After reading user input, the steps are:
* (1) fork a child process
* (2) the child process will invoke execvp()
* (3) if command included &, parent will invoke wait()
*/
}

return 0;
}

This project is organized into two parts: (1) creating the child process and executing the
command in the child, and (2) modifying the shell to allow selection of program from current
directory.

Part 1: Creating a Child Process

The first part of the project is to modify the main() function in the code above so that a child
process is forked and execute the command specified by the user. This will require parsing what
the user has entered into separate tokens and storing the tokens in an array of character strings
(args in the code). For example, if the user enters the command ps ael at the prompt,

ubos> ps ael

the values stored in the args array are:

args[0] = ps
args[1] = -ael
args[2] = NULL

This args array will be passed to the execvp() function, which has the following interface:

execvp(char *command, char * params[]);

where command represents the command to be performed and params stores the parameters to
this command. For this project, the execvp() function should be invoked as
execvp(args[0], args).
Part 2: Creating Program Selection Feature

The next task is to modify the shell interface program so that it provides a program selection
feature that allows the user to access the available executable programs from the current
directory. The user will be able to access all executable programs, i.e., all files that a user has
executable permission, from a current directory by using the feature.

The commands will be consecutively numbered by alphabetical order (i.e., A-Z and 0-9) starting
at 1, and the numbering will continue the last executable program.

The user will be able to list the executable programs by entering the command:

ubos> selection

As an example, assume that there are 5 executable files (or programs) in the current directory:

a.out ex1 ex2 test b.out

The output of selection command will be:

1 a.out
2 b.out
3 ex1
4 ex2
5 test

Your program should support the following execution feature, i.e., when the user enter an integer
number N, the Nth command in the list is executed.

Continuing out example from above, if the user enters 1, the program ex1 will be executed. Any
command executed in this fashion should be echoed on the users screen.

The program should also manage basic error handling. If there are no executable file, No
executable file in directory.

Restrictions

The code must be written in C and compile with the version of gcc installed in the lab.
system() library call may not be used
execvp() should be used among the exec*() family

Write-up

You should submit a write-up as well as your program. Your write-up should include analysis of
performance measure with various parameter values, any known bugs, limitations, and
assumptions in your program. This write-up should be in text-format and titled as README. It
should be submitted along with your code. GA will use the README file to compile (or install)
and run your program. If the GA has trouble with your program then he will contact you to
makeup it.
Demonstration

After the project is submitted, each student demonstrates it in front of instructor and GA. The
sign-up sheet will be provided before due date, and the student can choose 10 minutes time slot
for the demonstration.

Submission

You will submit your program using Canvas Assignment


(https://bridgeport.instructure.com/login ). If you have any trouble to use Canvas, you can contact
GA or instructor.

You should make tar file your source files and write-up (README) into a single tar file and
submit it. Be sure that you include everything necessary to unzip this file on another machine and
compile and run it. This might includes forms, modules, classes, configuration file, etc. DO NOT
include the executable file itself, we will recompile it. The name of your submitted zip file should
be your UB account_ID_Project1. For example, jelee_000000_Project1.tar. To create tar
file you can use tar command with cvf option.

Make sure your name and UB ID are listed in both your write-up and source code. You may
resubmit your program at any time. However, please do not ask the GA to grade your program
and then resubmit it. The submission time of the latest submission will be used for determining
whether the assignment is on time or not. Late submissions will be accepted at a penalty of 10
points per day. In other words, it may pay you to do this project early on the off chance that
something prohibits your submitting it in a timely way. If your program is not working by the
deadline, submit it anyway and review it together with GA for partial credit. Do not take a zero
on any lab just because the program isnt working yet. If you have trouble getting started, ask the
professor or the GA.

Grading

20 Process Creation
25 Task 1
25 Task 2
20 Error Handling
05 Write-up
05 Comments in code + Write-up
Bonus +10 for background process

You might also like