
Operating Systems (COP 4600) [Fall 2006]
Programming Assignment 2
Objectives:
1. To gain experience writing multithreaded programs.
2. To use semaphores and mutexes to synchronize threads.
3. To solve a problem in the classic producer-consumer paradigm.
Due Date:
Sunday, October 8, 2006 (at 11:59pm)
Assignment Description:
0) Complete this assignment by yourself on the following CSEE network computers:
Obelix, Epidemix, Geriatrix, Asterix, Panoramix, or Dogmatix. These machines are
physically located in the Center 4 lab (Room 220). Do not use any server machines like
grad, babbage, sunblast, etc. You can connect to these machines from home using SSH.
(Example: Host name: obelix.csee.usf.edu Login ID and Password: <your undergrad
login id and password>)
1) Download the market.c file from http://www.cse.usf.edu/~ligatti/4600-06/as2.
2) Study the code in market.c and compile and execute it. This code generates random
stock market trades.
3) Modify market.c into a multithreaded C program that simulates the production and
consumption of stock trades using the Pthreads API (discussed in class and in Chapters 4
and 6 of the textbook).
The program takes two command-line arguments, the first indicating the number of stock
brokers (i.e., stock trade producers) and the second the number of market makers (i.e.,
stock trade consumers). Please enforce that no more than 10 stock brokers (SBs) and no
more than 10 market makers (MMs) are used. Each SB and MM runs in a separate
thread, reading and writing a single, shared buffer of at most 5 stock trades. The trades in
the shared buffer are conceptually incomplete, in the sense that they have been produced
by an SB but have not yet been consumed by an MM. We have provided a C structure
for representing stock trades; it contains 4 fields: an integer number of shares to trade, an
integer indicating whether the trade is a buy or sell, an uppercase English character stock
symbol, and an integer identifying the SB that produced the trade.
SBs sit in an infinite loop doing the following. They wait a random amount of time
between 1 and 4 seconds and then, when the buffer has an empty position, write a random
trade into the next free buffer position. Generate trades by calling the get_new_trade
method we have already provided, and print a message like the following after inserting a
trade into the buffer: “trade BUY 93 shares S, from SB 1, produced into position 0 ”.
1