Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program example in BACI: line pc 1 0 // example of C-- semaphore usage 2 0 3 0 s

ID: 3904454 • Letter: P

Question

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: Example for the print out of results: Item 1 produced Item 2 produced Item 3 produced Item 1 consumed Item 4 produced Item 2 consumed Item 3 consumed ….. Post source code for part A and part B 1.- In this assignment you wl implement a deadlock free variant of the bounded-buffer producer/consumer using jBACI (C- -). C-- is a subset of Cthat allows you to declare semaphores and apply the operat ions 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 Download jBACI executable files, example files, and documentation from the folder JBACI on Webcourses. The folder JBACI in Webcourses tells you how to download and install jBACI on your computer The BACI support C-programming, which is a subset of C++ extended with primitives for process synchronization (Semaphores and Monitors) Program example in BACI

Explanation / Answer

While testing the solution we find that the first 3 items are produced and then a consumer is in action.
to introduce a time delay in the code use a dummy loop which will run for example 1000 times.

consumer code

# Producer-Consumer
+Implementation of a deadlock free variant of the bounded-buffer producer/consumer using jBACI
+
+Programmed in C--

Producer code


@@ -0,0 +1,65 @@
+int buffer[5];
+int i, j, w;
+
+// init semaphores
+semaphore full;
+semaphore mutex;
+semaphore empty;
+
+// producer function
+void Producer(){
+int y = 0;
+int k = 0;
+// loop to run 20
+for(i=1;i<21;i++){
+y++;
+// output
+cout << "Item" << i << " produced " << endl;
+p(full);
+p(mutex);
+buffer[k] = y;
+k++;
+if(k == 5) k = 0;
+v(mutex);
+v(empty);
+}
+}
+
+// consumer function
+void Consumer(){
+int a = 0;
+int x = 0;
+// loop to run 20
+for(j=1;j<21;j++){
+// consumer delay loop
+x++;
+p(empty);
+p(mutex);
+buffer[a] = x;
+a++;
+if(a == 5) a = 0;
+v(mutex);
+v(full);
+// output
+cout << "Item" << j << " consumed " << endl;
+
+}
+}
+
+main(){
+
+// init sem
+initialsem(full, 5);
+initialsem(mutex, 1);
+initialsem(empty, 0);
+
+// synchron process
+cobegin {
+
+Producer();
+Consumer();
+
+}
+
+}
+

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote