Question

Implement a simple program to illustrate one producer and one consumer problem. The producer will produce...

Implement a simple program to illustrate one producer and one consumer problem. The producer will
produce 100 messages for the consumer to consume. Use an array of 10 slots to hold the contents of
the buffer.
The producer should send a sequence of integers to the consumer. Specifically, it should send the
sequence 1, 2, 3, ..., 100, in that order. The consumer should receive them and check that it has indeed
received exactly that sequence, in that order.
Implement the program in two ways.
1. not thread-safe, not protecting the buffer;
2. using mechanism to protect the critical sections while they are updated.
Figure 2-32 (page 138) of the textbook gives a solution for a single buffer producer-consumer problem
using semaphores and conditional variable. Figure 2-35 gives a Java solution using monitors. You
may construct your program based on these examples.
Assume that it takes a random time between (0, 1) second to produce an item, and (0, 1) second to
consumer an item.
Notes:
1. You may use any programming language, as long as there is a way to to turn off protection on
synchronization;
2. You may increase the number of producers and consumers for additional bonuses;

i NEED THE BONUS AS WELL.tHANKYOU

Homework Answers

Answer #1

//1ST WAY IN C
#include <stdio.h>

int main () {

int n[ 100 ]; /* n is an array of 10 integers */
int i,j;

/* initialize elements of array n to 0 */   
for ( i = 0; i < 100; i++ ) {
n[ i ] = i + 1; /* set element at location i to i + 100 */
}

/* output each array element's value */
for (j = 0; j < 100; j++ ) {
printf("Producer to Consumer = %d\n", j, n[j] );
}

return 0;
}

//2ND WAY IN JAVA
import java.util.*;
import java.lang.*;

class Main
{
   public static void main (String[] args) throws java.lang.Exception
   //In the loop condtion it loops less than value less than 10
   {
       for (int i = 0; i < 10; i++) {
new Thread1().start();
}
   }

//static method to get the Integer
public static int getNum(int i) {
   return i + 1;
   }

  
static class Thread1 extends Thread {
static Integer value = 0;
@Override
public void run() {
while (value < 100) {
synchronized(Thread1.class) { //synchronized is the way to prevent deadlocks in threads
value = getNum(value);
System.out.println("Prouducer to Consumer" + "=" + + value);
}
try {Thread.sleep(100);} catch (Exception ignored) {}
}
}
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For this task, consider the producer/consumer problem. You have a buffer of 4096KB there are 4...
For this task, consider the producer/consumer problem. You have a buffer of 4096KB there are 4 consumer threads and 2 producer threads. 2.1 What problem may arise when simultaneously performing 4 consume from buffer and 1 produce to buffer operations? Assume that there are currently 3 elements of 1MB each in the buffer. 2.2 What problem may arise when performing 4 consume from buffer operations simultaneously if there are 4 elements of 1MB each in the buffer? 2.3 Given that...
For this task, consider the producer/consumer problem. You have a buffer of 4096KB there are 4...
For this task, consider the producer/consumer problem. You have a buffer of 4096KB there are 4 consumer threads and 2 producer threads. 2.1 What problem may arise when simultaneously performing 4 consume from buffer and 1 produce to buffer operations? Assume that there are currently 3 elements of 1MB each in the buffer. 2.2 What problem may arise when performing 4 consume from buffer operations simultaneously if there are 4 elements of 1MB each in the buffer? 2.3 Given that...
Consider the producer/consumer problem where the producer and consumer execute in separate processes synchronized by sempahores....
Consider the producer/consumer problem where the producer and consumer execute in separate processes synchronized by sempahores. Now, assume the producer can generate (data) items quickly and it is decided to update the consumer to a multi-threaded consumer. Also, assume the consumer has been changed to form three new consumer threads in addition to the thread running main(). You DO NOT have to write code for forming three new threads in the consumer. Now it is a requirement that all consumer...
Consider the producer/consumer problem where the producer and consumer execute in separate processes synchronized by sempahores....
Consider the producer/consumer problem where the producer and consumer execute in separate processes synchronized by sempahores. Now, assume the producer can generate (data) items quickly and it is decided to update the consumer to a multi-threaded consumer. Also, assume the consumer has been changed to form three new consumer threads in addition to the thread running main(). You DO NOT have to write code for forming three new threads in the consumer. Now it is a requirement that all consumer...
Your assignment is to implement a computer program in C++ to implement the below application. According...
Your assignment is to implement a computer program in C++ to implement the below application. According to Dummies.com the following algorithm determines the amount of paint you need to paint the walls of a four-sided room: 1. Add together the length of each wall. (For example, if each of the four walls are 14, 20, 14, 20 feet respectively then the total length is 14 + 20 + 14 + 20 = 68 feet) 2. Multiply the sum by the...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with...
Problem Statement: Write a Java program “HW6_lastname.java” that prints a program title and a menu with four items. The user should then be prompted to make a selection from the menu and based on their selection the program will perform the selected procedure. The title and menu should be as the following: Student name: <your name should be printed> CIS 232 Introduction to Programming Programming Project 6 Due Date: October 23, 2020 Instructor: Dr. Lomako ******************************** 1.     Compute Angles                               *...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
The purpose of this problem is to gain familiarity with stacks and queues. You have three...
The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end...