You are on page 1of 2

Assignment 1 (Open book)

Operating Systems
CS C372
BITS Pilani - Hyderabad Campus
Total: 20 Marks

Weightage 10%

Note: The assignment contains 3 problems. Each of you must write programs individually. There would
be a demo run of the programs followed by viva-voce to award marks. You may get help from the
Internet, but be prepared to answer any question that may be asked. Copied material/code without
knowledge of the logic/code would result in 0 marks.
The due date for demo is 24/06/2015. Demo would be on the same day.
Note that you dont have to submit any written material.
1. a) Write a simple shell in C. Your shell should read a line from standard input, parse the line to get
the command and its arguments, fork, and exec the command. Your shell should wait for commands to
finish, and should exit when it receives the command exit.
The following is a simple flow of control when your shell is run:
Print a command prompt.
Allow the user to enter commands and execute these commands.
Find the program to be launched in a hard-coded array of standard locations.
Display an appropriate error if a requested command cannot be found or is not executable.
Launch the program and pass the provided arguments to this program.
When the program has finished, print a command prompt again.
The command exit should exit the shell.
Note that using the system() function is not allowed, as it just invokes the system's /bin/sh shell to do all
the work.
You may assume command line arguments are separated by whitespace. Don't do anything special for
backslashes, quotes, ampersands or other characters that are ``special'' in other shells. Note that this
means commands in your shell will not be able to operate on filenames with spaces in them!
You may set a reasonable maximum on the number of command line arguments, but your shell should
handle input lines of any length.
Your shell prompt text must always be "$".
Check the return values of all functions utilizing system resources. Do not blithely assume all requests
for memory will succeed and all writes to a file will occur correctly. Your code should handle errors
properly. Many failed function calls should not be fatal to a program. Typically, a system call will return
-1 in the case of an error (malloc will return NULL). If a function call sets the errno variable (see the
function's man page to find out if it does), you should use the result of strerror(errno) as part of your
error message. As far as system calls are concerned, you will want to use one of the mechanisms
described in Reporting Errors or Error Reporting.
All error messages must be of the form "error: %s", strerror(errno). If an error occurs for which errno is
not set, ensure the message begins with "error:". The remaining text is up to you.
The executable file for your shell should be named cs372sh. When testing, make sure you execute your
cs372sh and not /bin/sh.

A few shell commands are not separate programs at all, but are handled directly by the shell program.
For example, cd and exit are both shell built-ins. Implement these two as part of shell itself. You will
need to use chdir() to implement cd. If the call to chdir fails, you should handle the error appropriately.
You can use the default $PATH to look for the programs to run in your shell.

[5 marks]

1 b) Augment your shell to be capable of executing a sequence of programs that communicate through a
pipe as discussed in . Your shell should support pipes between upto three processes. You do not need to
allow built-in commands such as cd to work with pipes.
[5 marks]
2. Using Pthreads, write a program that takes as input two matrices of size m*n and n*r, calculates the
product of the matrices and prints the result in proper matrix format. Each entry in the result is
calculated by a separate pthread. The proper matrix format prints result row by row and left to right
column. If a thread T1 that calculates result at row 3 column 4 before another thread T2 yet to calculate
result at row 3 column 3, T1 should wait for T2. The input matrices is stored in a single file in the
following format. Remember that the first line tells the number of rows and columns, followed by that
many rows, followed by the rows and columns of second matrix and then the rows themselves:
mn
row 1
row 2
row 3
.
.
.
row m
nr
row 1
row 2
row 3
.
.
.
row n
For example:
34
12 2 3 4
3528
6 34 2 3
42
56
84
93
23 4
Note that if the input matrices can't be multiplied (column of first matrix not equal to rows of second
matrix), the main function of your program should immediately print error and exit. This is the only
error condition that need to be checked in the input file.
[5 marks]
3. POSIX Semaphore problem: to be uploaded soon

[5 marks]

You might also like