Demo
Demo
CA2 ASSIGNMENT
roll No :- 11200220025
Because the buffer pool has a maximum size, this problem isoften
called the Bounded buffer problem.
Here's a Solution
// acquire lock
wait(mutex);
// release lock
signal(mutex);
// increment 'full'
signal(full);
while(TRUE)
Copy
do
signal(mutex);
// increment 'empty'
signal(empty);
while(TRUE);
Copy
• The consumer waits until there is atleast one full slotin the
buffer.
while(TRUE)
wait(stick[i]);
/*
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
The Solution
wait(w);
signal(w);
Copy
And, the code for the reader process looks like this:
while(TRUE)
//acquire lock
wait(m); read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
• As seen above in the code for the writer, the writer just
waits on the w semaphore until it gets a chanceto write
to the resource.
• On the other hand, in the code for the reader, the lockis
acquired whenever the read_count is updated by a process.
• The reason for this is, when the first readers enters the
critical section, the writer is blocked from the resource.
Only new readers can access the resource now.