Docsity

CS 440/540 Operating Systems Homework-3: Producer-Consumer Problem in Java, Assignments of Operating Systems

Toggle sidebar
bg1
Fall 2005 CS 440/540 Operating Systems
Homework-3
10/10/2005 1-1
200 points. Individual Work Only. Due Oct 24, 2005.
1. Implement the following producer-consumer problem in Java using threads. The
description of the Producer and Consumer are as follows:
Producer: The producer opens a specified file and reads the first 512 characters from a
file and writes the data to shared buffer. Before writing to the buffer the producer checks
to see if the buffer is empty and available for use. If the buffer is not available it waits
until it becomes available. Once the producer writes the data to the buffer, the producer
waits until the consumer notifies that the buffer is available, then reads the next 512
characters and writes to the shared buffer and continues until the end of the file is
reached. Once the end of file is reached, the producer exits the program.
Consumer: The consumer checks if the shared buffer is full, if it is full it reads the buffer,
converts all characters in the file to upper case and then writes the data to a new file,
otherwise it waits until the buffer is full. After the data is read the consumer notifies the
producer that the buffer is empty and then waits until the buffer is full and this process
continues until the buffer contains the end-of-file character. Once the end of file is
reached the consumer closes the file and exists.
The driver program must read the name of the input file and output file as command line
arguments, create an instance of the producer and consumer, and pass the input file name
to the producer and output file name to the consumer.
You can use the Producer/Consumer example provided in the Java Tutorial
(http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html) as a
starting point for this homework. Test your program with the two input files provided at
http://www.cis.uab.edu/cs440/homework3/. Do not use any sleep statements inside the
producer and consumer, all synchronization must be performed using wait and notify.
2. [Graduate Students Only, Bonus for Undergraduate Students]
In Problem 1, we created one producer and one consumer thread. If we were to create
multiple producer and consumer threads explain what changes would be required in
your implementation (there is no need to implement these changes, just write them in
words).
Submission Instructions:
Create a zip/tar file of all the source code, scripts (if any), output files, and README file
and email the zip/tar file as an attachment to [email protected] with the subject CS 440
Homework-3 or CS 540 Homework-3. Make sure to include instructions for compiling
and running the programs. Please do not include class files in the zip/tar file.

Partial preview of the text

Download CS 440/540 Operating Systems Homework-3: Producer-Consumer Problem in Java and more Assignments Operating Systems in PDF only on Docsity!

Fall 2005 CS 440/540 Operating Systems Homework-

200 points. Individual Work Only. Due Oct 24, 2005.

  1. Implement the following producer-consumer problem in Java using threads. The description of the Producer and Consumer are as follows:

Producer: The producer opens a specified file and reads the first 512 characters from a file and writes the data to shared buffer. Before writing to the buffer the producer checks to see if the buffer is empty and available for use. If the buffer is not available it waits until it becomes available. Once the producer writes the data to the buffer, the producer waits until the consumer notifies that the buffer is available, then reads the next 512 characters and writes to the shared buffer and continues until the end of the file is reached. Once the end of file is reached, the producer exits the program.

Consumer: The consumer checks if the shared buffer is full, if it is full it reads the buffer, converts all characters in the file to upper case and then writes the data to a new file, otherwise it waits until the buffer is full. After the data is read the consumer notifies the producer that the buffer is empty and then waits until the buffer is full and this process continues until the buffer contains the end-of-file character. Once the end of file is reached the consumer closes the file and exists.

The driver program must read the name of the input file and output file as command line arguments, create an instance of the producer and consumer, and pass the input file name to the producer and output file name to the consumer.

You can use the Producer/Consumer example provided in the Java Tutorial (http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html) as a starting point for this homework. Test your program with the two input files provided at http://www.cis.uab.edu/cs440/homework3/. Do not use any sleep statements inside the producer and consumer, all synchronization must be performed using wait and notify.

  1. [Graduate Students Only, Bonus for Undergraduate Students] In Problem 1, we created one producer and one consumer thread. If we were to create multiple producer and consumer threads explain what changes would be required in your implementation (there is no need to implement these changes, just write them in words).

Submission Instructions: Create a zip/tar file of all the source code, scripts (if any), output files, and README file and email the zip/tar file as an attachment to [email protected] with the subject CS 440 Homework-3 or CS 540 Homework-3. Make sure to include instructions for compiling and running the programs. Please do not include class files in the zip/tar file.