Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose we have two threads, in a producer/consumer arrangement that are communicating via a first-in-first-out buffer. The producer thread deposits data into the buffer, and

image text in transcribed

Suppose we have two threads, in a producer/consumer arrangement that are communicating via a first-in-first-out buffer. The producer thread deposits data into the buffer, and the consumer thread removes data from the buffer. The following variables define a simple circular buffer that can store double values: #define BUFFERS IZE 50 double buffer [BUFFERSIZE];//The data buffer int inIndex = 0;//Next item goes in at this index int outIndex = 0;//Next item comes out from this index int count= 0;//Number of items inthe buffer Here is the essence of the code for the bufferPut function: void bufferPut (double data) { if(count==BUFFERS!ZE){//buffer is full, wait} buffer [inIndex] = data: inIndex++: if(inIndx==BUFFERSIZE){ inIndex= 0: } count++: } When the buffer becomes full, the producer will need to wait. Similarly when the buffer becomes empty, the consumer will need to wait. The pthread library provides condition variables to help solve this problem. (a) Explain how to create and initialise a pthread condition variable (You may assume default attributes will be used.) (b) What additional variables will be needed for the buffer to allow it to operate correctly in this producer/consumer application. (c) Show the additional code that must be added to the bufferPut function to make it work correctly (d) Condition variables are subject to spurious wakeup. How is this problem handled

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions