Question

I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I...

I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I need a source comment next to the line that gives the delay so i can compare please.

(Concurrency – Semaphores)

1.- In this assignment you will implement a deadlock free variant of the bounded-buffer

producer/consumer using jBACI (C - -). C- - is a subset of C + + that allows you to declare

semaphores and apply the operations P and V.

In the standard solution with one producer and one consumer, you will need at least three

semaphores named:

a) Full->to stop the producer from producing items when the buffer is full. (initial value n= buffer size)

b) Empty->to stop the consumer from consuming items from the buffer when the buffer is empty (initial value zero)

c) Mutex->to allow only one process in the critical section (initial value one)

In this homework you will implement the bounded buffer producer/consumer, there will be one producer (P), one Consumer (C) and one buffer; the size of the buffer is 5.

The producer will produce 20 items and stop running.

The consumer will consume items from the buffer until the 20 items have been consumed

Project Direction:

You will write the program based on the

BACI interpreter.

Program example in BACI:

line pc

1 0 // example of C-- semaphore usage

2 0

3 0 semaphore count; // a "general" semaphore

4 0 binarysem output; // a binary (0 or 1) semaphore for unscrambling

output

5 0

6 0 void increment()

7 0 {

8 0 p(output); // obtain exclusive access to standard output

9 2 cout << "before v(count) value of count is " << count << endl;

10 6 v(output);

11 8 v(count); // increment the semaphore

12 10 } // increment

13 11

14 11 void decrement()

15 11 {

16 11 p(output); // obtain exclusive access to standard output

17 13 cout << "before p(count) value of count is " << count << endl;

18 17 v(output);

19 19 p(count); // decrement the semaphore (or stop -- see manual text)

20 21 } // decrement

21 22

22 22 main()

23 23 {

24 23 initialsem(count,0);

25 26 initialsem(output,1);

26 29 cobegin {

27 30 decrement(); increment();

28 36 }

29 37 } // main

Test your solution

You must run and test your solution for two cases as explain below and

compare and comment on the results emitted by each test:

a) Test your first solution

b) Introduce a time delay in the consumer and test your solution.

Time delay can be implemented using a dummy loop that iterates, for example, 1000 times

Project Submission:

What to submit?

For each test you must submit the source code and the output file

showing the results

I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I need a source comment next to the line that gives the delay so i can compare please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

semaphore full;
semaphore empty;
binarysem mutex;

const int size = 5;
int buffer[size];

int in = 0;
int out = 0;
int itemProduced = 0;
int itemConsumed = 0;
int producerFlag = 0;
int consumerFlag = 0;
int itemCounter = 1;
int i = 0;


void Produce(){
int item;

while (producerFlag == 0){
if (itemProduced == 20){
producerFlag = 1;
break;
}
else{
item = itemCounter;

itemCounter = itemCounter + 1;


p(full);
p(mutex);

if(itemProduced < 20){

cout << "Item " << item << " produced" << endl;

buffer[in]= item;
in = (in +1) % size;
itemProduced = itemProduced + 1;


if (itemProduced >= 20){
producerFlag = 1;
v(mutex);
v(empty);
break;
}

}

v(mutex);
v(empty);

}
}

}



void Consume(){
int item;
while (consumerFlag == 0){

if (itemConsumed == 20){
consumerFlag = 1;
v(empty);
break;
}

else{

p(empty);
p(mutex);

if (itemConsumed != 20){

item = buffer[out];
itemConsumed = itemConsumed + 1;

cout << "Item " << item << " consumed." << endl;

out = (out+1) % size;

if (itemConsumed== 20){

consumerFlag = 1;
v(mutex);
v(empty);
break;
}
}

v(mutex);
v(full);

}
}
}


void main(){
initialsem(full,size);
initialsem(empty,0);
initialsem(mutex,1);

cobegin{
Produce();Consume();

}


}

Add a comment
Know the answer?
Add Answer to:
I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...

    Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining space procedure producer() {    While (true) {    item = produceItem(); down(emptyCount); // emptyCount is decremented putItemIntoBuffer(item); up(fillCount); // fillcount is incremented } } procedure consumer() { While (true) {    down(fillCount); item = removeItem(); up(emptyCount); // emptyCount is incremented consumeItem(item); } } Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of...

  • I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so...

    I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so use int buffer[10]; // buffer is global variable and buffersize is 10 and there is only one producer and one consumer. 1) Producer() - At every 200msec, generates random positive integer and add to the buffer. - Then print current state of buffer (numbers and in/out indices) -Also print current state of two semaphore which are full...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    operating system engineering , please read the question and solve on the given skeleton code . Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...

    Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...

  • I just need the answer of #4 (Area, power, and delay analysis) P1 left shift 2...

    I just need the answer of #4 (Area, power, and delay analysis) P1 left shift 2 adder/subtractor Og - Yine code - Yu3 V2 mux sel x 2x stage 1+1 left shift 2 adder/subtractor 71+2 code mux sel ох 2x stage i Figure 1. Booth multiplier architecture 1. 6x6 Booth multiplier In this lab, you will first design a VHDL project for an 6x6 Booth multiplier in Xilinx Vivado. You optimize the multiplier for speed because Booth's multiplication algorithm is...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • This question has already been answered however I do not understand the particulars of the answer....

    This question has already been answered however I do not understand the particulars of the answer. for an example why are we importing an average class I thought that we could solve the problem just inputting the scanner and doing the import command at the top. Also at the bottom of the problem they increment the counter I thought you increment the counter at the top. Is there any way someone could do a detailed step-by-step explanation of the actual...

  • Person got it wrong the first time I posted this. Iconic memory is a type of...

    Person got it wrong the first time I posted this. Iconic memory is a type of memory that holds visual information for about half a second (0.5 seconds). To demonstrate this type of memory, participants were shown three rows of four letters for 50 milliseconds. They were then asked to recall as many letters as possible, with a 0-, 0.5-, or 1.0-second delay before responding. Researchers hypothesized that longer delays would result in poorer recall. The number of letters correctly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT