Docsity

Operating Systems Programming Assignment 2: Multithreaded Stock Market Simulation - Prof. , Assignments of Operating Systems

Toggle sidebar
bg1
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
pf3

Partial preview of the text

Download Operating Systems Programming Assignment 2: Multithreaded Stock Market Simulation - Prof. and more Assignments Operating Systems in PDF only on Docsity!

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 ”.

This message indicates that the first SB generated the trade “BUY 93 S” and wrote the

trade into the buffer at index 0.

MMs sit in an infinite loop doing the following. They wait a random amount of time

between 1 and 4 seconds and then consume the next trade from the shared buffer when

one is present. After an MM consumes a trade, it prints a message like “trade BUY 93

shares S, from SB 1, consumed from position 0 by MM 1 ”. This message indicates

that the first MM has consumed the trade “BUY 93 S”, which was generated by the first

SB, from the buffer at position 0.

Implement the shared buffer as a circular queue. That is, the first trade gets produced

into index 0 of the buffer, the second into index 1, third into index 2, fourth into 3, fifth

into 4, and then the sixth trade to be produced gets stored into index 0 again. Similarly,

the first trade will be consumed from position 0 in the buffer, the second from 1, etc.

Synchronize SBs and MMs using two semaphores and one mutex lock. One semaphore

counts the number of empty buffer positions; the other counts the number of full buffer

positions. The mutex lock simply controls access to critical sections. These global

synchronization variables have already been declared for you in market.c , but they still

need to be initialized prior to use.

The simulation continues until exactly 10 trades have been consumed. At that point, print

the contents of the shared buffer (i.e., the incomplete trades) and exit the simulation

entirely.

Important Pointers:

 We have already provided you many “helper methods” in market.c. Use them all

and do not modify them.

 Your textbook contains helpful material. Sections 4.3.1, 6.1, 6.5, and 6.6.1 are

particularly helpful for this assignment. The Chapter 6 project description (page

236) will also be very helpful, though of course the textbook project differs in

many ways from this assignment.

 This problem can be solved within 180 lines of clean, well-documented code.

Submission Notes:

 Type the following pledge as an initial comment in your C file: “I pledge my

honor that I have not cheated on this assignment.” Type your name after the

pledge. Not including this pledge will lower your grade 50%.

 Upload market.c onto Blackboard using the digital drop-box. You can make any

number of submissions; we will grade the latest.

 It is your responsibility to ensure that your program compiles on Unix machines.

 For every day that your assignment is late, your grade reduces 10%.

 Your program must be commented appropriately.