Programming Assignment 2
CS 370: Systems Architecture & Software
Due date - 10:00PM on Sunday, March 9th, 2014 - Part A (100 points)
Due date - 10:00PM on Sunday, March 16th, 2014 - Part B (100 points)
Due date - 10:00PM on Sunday, March 30th, 2014 - Part C (100 points)
Due date - 10:00PM on Friday, April 4th, 2014 - Lab Report 2 (100 points) - No Late period
Programming Assignment 2 - Threads, Producer/Consumer, & Synchronization in Java & C
Version 1.0 - Initial Version
Version 1.1 - Added no late penalty on Lab Report 2
Objective of Assignment
The objective of this assignment is to introduce the student
to the concept of producer/consumers, multithreading, and synchronized
data structure in Java and C. This will be done by first creating a
multi-threaded consumer/producer using a concurrent data structure
(ArrayBlockingQueue) for
the work queue, for part B modifying that program to use a
non-concurrent data structure and a synchronized block instead, and
for part C implement the same requirements in C using Pthreads.
Requirements for Part A - Multi-threading & Producer/Consumer in Java
-
The program will take as input on the command line 2 numbers, each
number between 1 and 20 inclusive. The first of these numbers indicate the
number of producer threads to create, the second of these numbers indicates
the number of consumer threads to create.
-
The class for Part A should be named ProdConsABQ.
-
The constructor for the class will spawn the appropriate number of
each of the producer and consumer threads.
-
The class will have a global work queue of type ArrayBlockingQueue
of size 100 and a global boolean variable as a flag to finish.
-
The main program will parse the command line, call the constructor with the
appropriate parameters indicating the number of producers and consumers,
sleep for 30 seconds (note this is seconds, not milliseconds), then
set the global finish flag telling the consumers and producers
to quit. It will then join with the threads, print a finishing message,
and exit.
-
The producer threads will generate a random number between 1 and 1000,
sleep for that number of milliseconds, generate a second random number
between 1 and 1000 inclusive. They will then attempt to put this number
on the work queue. If the queue is full, they will write a message
indicating that the queue is full, otherwise they will write a message
indicating what the number was they put on the queue and the size of the
queue when they did so. This will be done in a continuous loop until
the main program sets the finish flag, at which point they will exit.
-
The consumer threads will - in a continuous loop - sleep for a
random number of milliseconds between
1 and 1000, and then perform a blocking receive off the work queue.
Once they pull the number, they will compute the square of that number,
print a message indicating the original number, the square, and the
size of the queue. They will continue this until the main program sets
the finish flag, at which point they will exit.
- This program should be written in Java and should be able to be run
on the state capital machines in 120.
-
An example of spawning work pools of Java threads can be found
here.
-
An example of joining threads in java can be found
here.
-
An example of waking up threads in java can be found
here.
Requirements for Part B - adding synchronization in Java
The requirements for Part B are identical to the ones above with the
following exceptions.
-
The class for Part B should be named ProdConsLL.
-
Instead of using a work queue of type ArrayBlockingQueue which hides the
need for synchronization from you, you should implement the work queue
as type LinkedList. This will require you to surround access of the
work queue with a synchronized block.
-
The maximum number of entries on the linked list should be 100, you
will have to implement your own blocking with wait() and notify().
-
The time spent inside the synchronized block should be minimized.
-
This program should be written in Jave and be able to be run on
the state capital machines in 120.
-
An example of using synchronized to protect non-concurrent data structures
can be found
here.
Requirements for Part C - Using Pthreads in C to accomplish the same
The requirements for Part C are to implement the program from Part 2
in C using the Pthreads library instead of Java.
- The work queue and pointers should be implemented as a global struct
- There is no requirement for the type of data structure in the struct.
My suggestion would be to use an array, an in pointer, and an out pointer
to keep it as simple as possible.
- Critical sections should be protected using mutexes
- Blocking calls should be implemented using pthread_cond_wait()
and pthread_cond_broadcast()
- This program should be written in C and be able to be run on the
state capital machines in 120.
-
An example of using threads in C can be found
here.
-
An example of using passing structs to a thread in C can be found
here.
-
An example of using conditional waits and broadcasts in C can be found
here.
-
An example of using conditional waits, signals, and broadcasts in C can be
found here .
-
An example of using a global struct can be found
here.
Sample output
carrot> javac waker_lab2_1.java
carrot> java ProdConsABQ 2 3
Producer started
Producer started
Consumer started
Consumer started
Consumer started
Producer added 4 to queue, size = 1
Consumer removed 4, computed the square of 4 = 16, queue size = 0
.
.
.
Consumer removed 3, computed the square of 3 = 9, queue size=4
Main program exiting after joining threads
Writeup and checkin
Writeup
In your writeup, you should include issues such as objective, methodology
(fairly straight forward for this lab), issues encountered while implementing,
and an analysis of the assignment. For this assignment, what did you learn
about multi-threading? Producer/Consumer? What did the difference between
parts A,B, and C show you? What was different between Java and C? What were
the strengths and weaknesses of each? What problems did you encounter?
Imagine you are writing up your research to be reviewed by a team
of professors or peers - that is the flavor of a lab writeup.
Part A
You should tar up 2 files and submit them electronically into RamCT.
Please name the files
using the convention described in each of the bullets below.
- The first file is your Java programs for Part A.
This program should be
well-documented and should fulfill the requirements laid out above. This file should
be named ProdConsABQ.java .
- The second file is the README file contain a list of the files submitted,
and anything the grader should know to grade your program.
Part B
You should tar up 2 files and submit them electronically into RamCT.
Please name the files
using the convention described in each of the bullets below.
- The first file is your Java programs for Part B.
This program should be
well-documented and should fulfill the requirements laid out above. This file should
be named ProdConsLL.java .
- The second file is the README file contain a list of the files submitted,
and anything the grader should know to grade your program.
Part C
You should tar 3 files up and submit them electronically into RamCT.
Please name the files
using the convention described in each of the bullets below.
- The first file (or files) is/are your C program file(s) for Part C.
This program should be well-documented and should fulfill the
requirements laid out above.
-
The second file is your Makefile that builds your code to an object named PA2C.
- The third file is the README file contain a list of the files submitted,
and anything the grader should know to grade your program.
Lab Writeup
You should turn your lab writeup electronically into RamCT. This file
should be professional in appearance, should be created using some kind of
text processor, and should be submitted in .pdf format. The writeup should
contain your name at the top of the first page and should have your name in
the header of each page. An example of the lab report can be found
here. Points will be awarded for completeness, grammar, and clarity.
Note: there is no late period for Lab Report 2
Please see
here for details on the Late Policy for this class.
Copyright © 2014: Colorado State University for CS 370.
All rights reserved.