CSCI 152: Operating Systems Programming
Laboratory Assignment 2
Assignment Background:
Lab 2 is going to be
written in C and is going to make use of posix threads, semaphores and
mutexes. So when you find people to be in a group with you should
keep this in mind. I'm going to recommend doing this assignment
on tiglon (Solaris). I know it will also be possible to do it on
Linux, and it probably can be done on FreeBSD and MacOS X but I do not
know if MS Windows has the right libraries (maybe it could be done with
cygwin?). So I don't care what platform you do it on but if your
platform does not have pthreads you can not do it on that platform.
[Also, I haven't done any testing on steroid which is HP/UX].
Using the man pages:
The man pages will be your primary reference for pthreads, semaphores and mutexes.
man is a unix command (short for "manual").
To view a man page you type "man" followed by the thing you want to see the manual page for.
Commands inside man:
<enter> - move down a line
<space-bar> - move down a pageful of text
q - quit
b - back a pageful of text
Command to help you find man pages:
man -k <keyword>
You can view a man page from a particular section by using the -s option:
man -s3c sleep
Man pages are divided up into sections:
Section - Contents
--------------------------------------
1 - User Commands
2 - System Calls
3 - C library functions
4 - Devices and network interfaces
5 - File formats
6 - Games and Demos
7 - Environments, tables, and troff macros
8 - System maintenance
Try the following commands and read the documentation:
-------------------------------------
man pthread_create
man semaphore
man mutex
man pthread_join
man sem_init
man sem_wait
man sem_trywait
man sem_post
man rand
man -s3c sleep
man -s3c printf
Also check out the EXAMPLE CODE
C reference material:
Since many students in this class come from a background of mainly C++,
Java or Perl (myself included) you will probably also need a C
programming reference. A great reference book on C programming
is, "The C Programming Language" see http://cm.bell-labs.com/cm/cs/cbook/.
Unfortunately, this is not a free book. I think it is great
though and I really recommend purchasing it (other C reference books
might be just as good). Barnes and Noble here in town had a few
copies last time I was there. There is a slight discount at
bookpool, see http://www.bookpool.com/.x/ozdbhder1i/sm/0131103628.
Please note that this book is only a reference to the C
programming language and does not contain any info on posix threads,
semaphores, and mutexes. It documents stuff like input and output
structs, pointers, <stdlib.h> and so on... (This book could
possibly be useful in other courses like CSCI250, CSCI272, CSCI372 or
any other course involving the C language).
If you have never used C before don't freak out too much, it is
just like C++ without the classes and some other stuff. Instead
of cout's you use printf's and instead of methods you have functions
and instead of using g++ you use gcc. You will have to do some
reading though.
The reasons for restricting this assignment to C:
- I think the first assignment was a little too easy if done in Java.
- Operating Systems programming is usually done in C
- It will force most of you to learn a couple of new things
Lab Assignment 3:
Just so you know lab assignment 3 is likely going to involve modifying
lab 2 in some way to add some kind of functionality. Like turning
it into a network program or something.
Assignment Description:
Initially I was going
to assign the dining philosophers problem. A friend pointed out
there are only about a million solutions to the dining philosophers
problem posted on the web. So this is something more original.
This program solves a producer-consumer problem. The "producers"
are going to generate print jobs and the "consumers" are going to be
printers which print print jobs. A print job will have an ID,
page length, and a field holding the producer ID which created it.
Each producer thread will create print jobs randomly and then put
them into a queue of limited size. Consumers threads will remove
"print jobs" from the queue and "print" them by sending output to the
screen.
This program will have a main that creates producer threads and consumer threads. Each
producer thead can be thought of as a person that generates print
jobs and each consumer thread can be thought of as a printer.
Each person (producer) does the following:
- sleeps for a random amount of time (1 to 3 seconds) while it figures out what it wants to do
- chooses a random number of documents to print (2 to 4)
- loop for the number of documents to print
- see if there is room in the queue by doing a sem_wait()
- when there is room in the queue then lock the queue using a mutex
- add a print job to the queue
- remove the lock on the queue
- sleep for a second
Each printer (consumer) does the following:
- waits until there is something in the queue
- once there is the printer locks the queue
- removes a print job
- unlocks the queue
- prints a "print job" to the screen as follows: [sleeping 1 second for each page].
- documents are "printed" by sending output like following to the terminal:
- printer 2 printing document 3 from person 1 start
- printer 2 printing document 3 page 1 from person 1
- printer 2 printing document 3 page 2 from person 1
- printer 2 printing document 3 page 3 from person 1
- printer 2 printing document 3 from person 1 done
You should be able to turn off all
sleeping using a debug level feature like that used in program 1.
Problems to Avoid in Producer/Consumer:
- A consumer removes an item while a producer is in the process of putting it in the queue.
- A consumer removes items that are not there at all.
- A consumer removes items that have already been removed.
- A producer puts something in the queue when there is no free slot.
- A producer overwrites an item that has not been removed.
Goals:
Learn about operating system fundamentals, including history, process and thread
management, concurrency with semaphores and monitors, deadlocks,
storage management, file systems, I/O, and distributed systems.
Objectives:
- Learn about sychronization issues involved in solving a producer-consumer problem.
- Learn about the use of threads.
- Learn about the use of semaphores (waiting).
- Learn about the use of mutexes (locking).
Expected Outcome:
- This project must be completed in groups of 1 to 3 people.
- This project must be completed using one of the following languages
- The platform you do this project on must support posix threads, semaphores, and mutexes.
- Completing this assignment on tiglon is recommended for most students.
- Any platform is okay as long as that platform supports posix threads, semaphores, and mutexes.
- Read "Practical Software Engineering Guidelines" which were included with your syllabus
- Include DEBUG LEVEL feature included in these guidelines
- Required use of DEBUG LEVEL
- DEBUG LEVEL should be used to turn sleeping ON and OFF.
- Optional
- Debug level feature
is used for determining how verbose of messaging the server and client
should display about what is happening in them internally.
- Applications should take a DEBUG LEVEL as an optional command line parameter.
- When you demonstrate this program in class I want to see 2 or 3 different debug levels.
- Initially I recommend writing the program with a fixed
number of comsumer and producer threads and then modifying it so that
it can take arguments at the command line for any number of producer
and consumer threads.
- Documentation is required
- TURN-IN
- print out of all code used to create this project.
- Your code must have the following COVER SHEET
- you must demonstrate this project to your TA during lab.
- you must turn-in an evaluation of yourself and your group members.