You are on page 1of 5

Operating Systems CS4348 Project #2: Threads Due Date: Wednesday, March 31, 2010 I.

Project Organization
This project will study the coordination of multiple threads. You should do the following pieces to complete your project. Each piece is explained below: Design Code Output Summary 30 points 30 points 20 points 20 points

Design
Design includes a table of semaphores and pseudocode for each function. The table should list every semaphore, its purpose, and its initial value. The pseudocode should be similar to the pseudocode shown in the textbook for the barbershop problem. Every wait and signal call must be included in the pseudocode.

Code
Your code should be nicely formatted with plenty of comments. The code should be easy to read, properly indented, employ good naming standards, good structure, and should correctly implement the design. Your code should match your pseudocode.

Output
Output will be graded by running your program.

Summary
The summary section should discuss your simulation, any difficulties encountered, what was learned, and results. It should be at least one page in length.

II. Project Description


Language/Platform This project must target a Unix platform and execute properly on our Unix apache server. The project must be written in C, C++, or Java. If using C or C++, you must use POSIX pthreads and semaphores. If using Java, you must only use Java Threads and Java Semaphores (java.util.concurrent.Semaphore). You may not use the synchronized keyword or thread-safe classes in java.util.concurrent.* that provide their own mutual exclusion for this project (other than the Semaphore class).

Post Office Simulation


A Post Office is simulated by using threads to model customer and employee behavior. The objective is to determine how the number of postal workers (between 1 and 4) affects the line length. This project is similar to the barbershop example in the textbook. The following rules apply: Customer: 1) Customers visit the Post Office (1 thread per customer up to 50) arriving every 30 seconds. 2) Only 10 customers can be inside the Post Office at a time. 3) Customers are in line until being served by a postal worker. 4) Each customer upon creation is randomly assigned one of the following tasks: a) buy stamps b) mail a letter c) mail a package d) buy a money order e) pickup a package 5) Times for each task are defined in the task table. Postal Worker: 1) Created initially, one thread each. 2) Serves next customer in line. 3) Service time varies depending on customer task. Measures: 1) Line length should be measured by a separate thread that gets the length every second. 2) Line length measurements are used to display the average and maximum line length. Other rules: 1) Number of postal workers and number of customers must be command line arguments. 2) A thread should sleep 1 second for each 60 seconds listed in the table. 3) All mutual exclusion and coordination must be achieved with semaphores. A thread may not use sleeping as a means of coordination. Busy waiting (polling) is not allowed. 4) Mutual exclusion should be kept to a minimum to allow the most concurrency. 5) Each thread should print when it is created and when it is joined. 6) Each thread should only print its own activities. The customer thread prints customer actions and the postal worker thread prints postal worker actions. 7) Your output must include the same information as the sample output.

Testing: Run your simulation with varying numbers of customers and postal workers. Output: 1) Each step of each task of each thread should be printed to the screen with identifying numbers so it is clear which threads are involved. 2) Thread activity output sample: Simulating post office with 6 customers and 2 postal workers Postal worker 0 created Postal worker 1 created Customer 0 created Postal worker 0 serving customer 0 Customer 0 asks postal worker 0 to buy a money order Customer 1 created Postal worker 1 serving customer 1 Customer 1 asks postal worker 1 to buy a money order Customer 2 created Customer 3 created Customer 4 created Postal worker 0 finished serving customer 0 Postal worker 0 serving customer 2 Customer 0 finished buying a money order Customer 2 asks postal worker 0 to buy a money order Joined customer 0 Customer 5 created Postal worker 1 finished serving customer 1 Postal worker 1 serving customer 3 Customer 1 finished buying a money order Customer 3 asks postal worker 1 to buy stamps Joined customer 1 Postal worker 1 finished serving customer 3 Postal worker 1 serving customer 4 Customer 3 finished buying stamps Customer 4 asks postal worker 1 to mail a letter Postal worker 0 finished serving customer 2 Postal worker 0 serving customer 5 Customer 2 finished buying a money order Customer 5 asks postal worker 0 to mail a package Joined customer 2 Joined customer 3 Postal worker 1 finished serving customer 4 Customer 4 finished mailing a letter Joined customer 4 Postal worker 0 finished serving customer 5 Customer 5 finished mailing a package Joined customer 5

3) At the end of the simulation, display a report as follows: Post Office Simulation Number of Postal Employees: 2 Number of Customers: 6 Lengths: Average line length: 0 Maximum line length: 2

Task Table Task Buy stamps Mail a letter Mail a package Buy a money order Pickup a package Time (seconds) 60 90 120 150 180

III. Project Guidelines


Compiling Your project should be compiled and tested on one of the following Unix machines: cs1, cs2, or the net01-net30 machines Your final project should work correctly on apache. Submitting Submit your project on eLearning. Include in your submission the following files: 1) A Word document for the written piece of the project. (The source code should not be part of this document.) 2) Source files a) Main file named proj2.c, proj2.cpp, or Proj2.java b) Other files as needed c) All debugging-related code removed 3) A readme file with: a) the complete command line used to compile your program b) the complete command line used to run your program c) any other details the TA should know Academic Honesty All work must be your own. Comparison software will be used to compare the work of all students. Similar work will be reported to the Office of Judicial Affairs for investigation. They will determine the penalty to be given. Resources The web has many articles on threads and there are books available on threads. The course website also contains example source code.

You might also like