Create a consolebased application that spawns two threads a
Create a console-based application that spawns two threads: a producer and a consumer. Use any programming language.
The producer should open an input file and repeatedly copy values to a circular buffer.
The consumer should open an output file and repeatedly copy values from the same circular buffer to the file.
For both the producer and consumer, a random number of bytes between 1 and n, inclusive, should be copied each iteration, where n is specified by the user. The number of bytes should be randomized each iteration.
If the producer is unable to write to the buffer (because it does not contain enough empty elements), or if the consumer is unable to read from the buffer (because it does not contain enough unread items), then it should proceed to the next iteration, choosing a new random number of bytes to copy. (Exception: Once the producer has already read the entire file, then the consumer does not have to continue generating random numbers until it gets an exact hit; instead, it should write the rest of the buffer to complete the copy.)
When the program completes, the output file should be an exact copy of the input file.
The syntax of the program should be as follows:
copyfile input output n m
where
copyfile is the name of your executable
input is the name of the input file
output is the name of the output file
n is the maximum number of bytes to copy in any given iteration (described above)
m is the size of the circular buffer, in bytes
Solution
This is the java code demonstrated the producer consumer problem by using Thread. package ProducerConsumer; import java.lang.*; public class ProducerConsumerTest { public static void main(String[] args) { Buffer c = new Buffer(); Producer p1 = new Producer(c, 1); Consumer c1 = new Consumer(c, 1); p1.start(); c1.start(); } } class Buffer { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; } public synchronized void put(int value) { while (available == true) { try { wait(); } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); } } class Consumer extends Thread { private Buffer buffer; private int number; public Consumer(Buffer c, int number) { cubbyhole = c; this.number = number; } public void run() { int value = 0; for (int i = 0; i < 10; i++) { value = buffer.get(); System.out.println(\"Consumer #\" + this.number+ \" got: \" + value); } } } class Producer extends Thread { private Buffer buffer; private int number; public Producer(Buffer c, int number) { buffer = c; this.number = number; } public void run() { for (int i = 0; i < 10; i++) { buffer.put(i); System.out.println(\"Producer #\" + this.number + \" put: \" + i); try { sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } } } } OUTPUT Producer #1 put: 0 Consumer #1 got: 0 Producer #1 put: 1 Consumer #1 got: 1 Producer #1 put: 2 Consumer #1 got: 2 Producer #1 put: 3 Consumer #1 got: 3 Producer #1 put: 4 Consumer #1 got: 4 Producer #1 put: 5 Consumer #1 got: 5 Producer #1 put: 6 Consumer #1 got: 6 Producer #1 put: 7 Consumer #1 got: 7 Producer #1 put: 8 Consumer #1 got: 8 Producer #1 put: 9 Consumer #1 got: 9