Docsity

Operating Systems Programming Assignment: Producers and Consumers using Threads, Assignments of Operating Systems

Toggle sidebar
bg1
COMPSCI 453/552: Operating Systems
Programming Assignment 5 (20 points)
Due Date (on class home page)
Producers and Consumers using Threads
We will simulate a size-bounded queue (implemented as a doubly-linked list with a limit
on maximum size) being shared by multiple producers and multiple consumers running
simultaneously. Suppose we have mproducers and nconsumers (where mand nare supplied
as command line arguments). The initial main process creates a queue data structure. Make
this queue global so all threads can access it. Then the main process will create mproducer
threads and nconsumer threads.
We will fix the number of items each producer produces (as a command line argument).
Then we will keep track of how many items each consumer actually consumed. At the end
of the program, we print out the total number of items produced versus the total number
of items consumed. These two numbers should be the same unless the program has race
conditions.
The items are a structure that contain an unique id (basically number the items 1,2,3, . . .
as they are entered in the queue) and also contains the index of the producer that created
it.
Approach
A template program has been provided for you to use as a starting point. The program is
in the directory ∼amit/cs453/lab/producers-consumers and the main file is called pc.c.
The directory also contains a relevant Makefile that you should use.
First, you will need to create a monitor version of your doubly linked list to use in this
project. An additional feature we will add to it is the ability to set the maximum size of the
list in the constructor. You will be using the POSIX Pthreads mutexes and and Pthreads
condition variables to implement the monitor version of your doubly linked list. Rele-
vant man pages are pthread mutex init, pthread mutex lock, pthread mutex unlock,
pthread cond init, pthread cond wait, pthread cond signal. Please create a separate
version that is a monitor. For example, this could be on a separate branch of your subversion
tree.
Then the producer/consumer threads will use the monitor version of the doubly linked list
to run the simulation. To test your monitor version, we will have the producers randomly
pf2

Partial preview of the text

Download Operating Systems Programming Assignment: Producers and Consumers using Threads and more Assignments Operating Systems in PDF only on Docsity!

COMPSCI 453/552: Operating Systems

Programming Assignment 5 (20 points)

Due Date (on class home page)

Producers and Consumers using Threads

We will simulate a size-bounded queue (implemented as a doubly-linked list with a limit on maximum size) being shared by multiple producers and multiple consumers running simultaneously. Suppose we have m producers and n consumers (where m and n are supplied as command line arguments). The initial main process creates a queue data structure. Make this queue global so all threads can access it. Then the main process will create m producer threads and n consumer threads.

We will fix the number of items each producer produces (as a command line argument). Then we will keep track of how many items each consumer actually consumed. At the end of the program, we print out the total number of items produced versus the total number of items consumed. These two numbers should be the same unless the program has race conditions.

The items are a structure that contain an unique id (basically number the items 1, 2 , 3 ,... as they are entered in the queue) and also contains the index of the producer that created it.

Approach

A template program has been provided for you to use as a starting point. The program is in the directory ∼amit/cs453/lab/producers-consumers and the main file is called pc.c. The directory also contains a relevant Makefile that you should use.

First, you will need to create a monitor version of your doubly linked list to use in this project. An additional feature we will add to it is the ability to set the maximum size of the list in the constructor. You will be using the POSIX Pthreads mutexes and and Pthreads condition variables to implement the monitor version of your doubly linked list. Rele- vant man pages are pthread mutex init, pthread mutex lock, pthread mutex unlock, pthread cond init, pthread cond wait, pthread cond signal. Please create a separate version that is a monitor. For example, this could be on a separate branch of your subversion tree.

Then the producer/consumer threads will use the monitor version of the doubly linked list to run the simulation. To test your monitor version, we will have the producers randomly

insert new items at the front or back of the queue. Similarly the consumers randomly remove^2 items from the front or back of the queue.

Notes on testing

  • When testing your program limit the number of threads to less than ten and number of items produced per producer to less than ten thousand to avoid overloading onyx. At home you can do whatever you want.
  • Run the program several times for the same input arguments. See if the results vary.
  • Comment out the output statements. See if that changes the results for the number of items consumed versus produced.
  • Use the testing script provided in the assignment folder.

Submitting the Assignment

You should also have a Makefile that compiles and links your program. The name of the executable must be pc, which should be produced by typing in make. Also include a README file that contains your name, date, assignment number and observations about the assignment.

Put all these files in a directory (say ∼/cs453/p6). Change directory to this directory and execute the following command (on onyx) to submit the assignment.

submit amit cs453 5

or

submit amit cs552 5

This command will pick up all files in the current directory (as well as any sub-directories recursively) and time-stamp them before transferring the combined files to the instructor’s account.