Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

According to this instruction I need help to fix the code below. Thanks Make a C / C++, Java, Python program with two processes, a

According to this instruction I need help to fix the code below. Thanks

Make a C / C++, Java, Python program with two processes, a producer and a consumer. If you want to use another language, clear it with me first.

The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to three seconds (compute a new random wait value on each pass through the loop). The loop is to be executed five times.

The consumer process consists of a loop that reads the variable it shares with the producer five times and computes the sum of the values it has read. On each pass through the loop before it reads the shared variable, it does a random wait of from one to three seconds (compute a new random value on each pass through the loop). When the loop finishes, the program is to write the sum into a file.

You must run the program twice. Note that the output from the runs will almost certainly be different if you have written your program correctly. Place the code, a copy of the output from each of your runs for grading (the output file must be to a data file and not a screen shot) in the D2L Dropbox for the 2nd assignment. Also include a word or text file with your observations on the assignment: What did you notice about the runs? What if anything did you learn doing this assignment? Roughly how long (in hours) did the assignment take?

Please put comments at the top of your program with your name, the date, the assignment number, and a brief description of the program. I also want to see comments within the program. The first items in your data file and observations file must be your name and the assignment number (have your program output this information into your output file). Note, I do not want screen shots, your program must output to a data file which is to be sent to me. You may zip your files together, but do not send me a tar file or another type of compression file or place the files out on the Internet or the Cloud for me to pick up.

If you do not know how to create a process in Java, C/C++, or Python or do not know how to do a random wait, or how to output to a data file, it is your responsibility to learn how. This information is available on the Internet.

The contents of you output file should look something like this (where ### is your computed sum).

John Smith

ICS 462 Assignment #2

The sum is ###

========================================================================== import java.util.LinkedList; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.Scanner;

public class Processes {

public static void main(String[] args) {

// first initialize the variable to 100 SharedVariable sharedVariable = new SharedVariable(100);

// create a producer Producer producer = new Producer(sharedVariable);

// create a consumer Consumer consumer = new Consumer(sharedVariable);

// start the producer producer.start();

// start the consumer consumer.start();

} }

class SharedVariable { private int var;

SharedVariable(int var) { this.var = var; } public synchronized void write(int i) { this.var = i; }

public synchronized int read() { return this.var; } }

//consumer class extending the Thread class Consumer extends Thread {

private final SharedVariable sharedVar; private int sum = 0;

public Consumer( SharedVariable sharedVar) { this.sharedVar = sharedVar; }

@Override public void run() {

for(int i = 0; i <= 4; i++) {

// before reading the sharedVar, consumer does a random waiting // of 1 to 3 seconds try { int waitTime = (int)(Math.random() * 1000); Thread.sleep(1500); } catch (InterruptedException e) { System.out.println("Consumer got Interrupted"); }

// now reads the shared variable int newVal = sharedVar.read(); System.out.println("Consumer: "+newVal);

// computes the sum of the values it has read so far sum += newVal; }

// print the sum System.out.println("The Sum is : "+this.getSum()); }

public int getSum() { return sum; } }

//producer class extending the Thread class Producer extends Thread {

private final SharedVariable sharedVar;

public Producer(SharedVariable sharedVar) { this.sharedVar = sharedVar; }

@Override public void run() {

// producer processes the loop from 0 to 4 for (int i = 0; i <= 4; i++) {

// before writing into sharedVar, producer does a random waiting // of 1 to 3 seconds try { int waitTime = (int)(Math.random() * 1000); System.out.println("Producer: "+waitTime); Thread.sleep(1500); } catch (InterruptedException e) { System.out.println("Producer got Interrupted"); } PrintWriter writer = null; try { writer = new PrintWriter("Assignment.txt", "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.println("##############"); writer.println("##########"); writer.println ("The sum is: " +i); writer.close();

this.sharedVar.write(i); } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

1. In what ways has flexible working revolutionised employment?

Answered: 1 week ago