How reader and writer problem was implemented and come up
with effective solution for reader and writer problem in
case we have n readers and 1 writer.
Answer Posted / raj kumar
Readers: read data, never modify it
Writers: read data and modify it
criteria:
– Each read or write of the shared data must happen within
a critical section.
– Guarantee mutual exclusion for writers.
– Allow multiple readers to execute in the critical section
at once.
In this example, semaphores are used to prevent any writing
processes from changing information in the database while
other processes are reading from the database.
semaphore mutex = 1; // Controls access to
the reader count
semaphore db = 1; // Controls access to
the database
int reader_count; // The number of
reading processes accessing the data
Reader()
{
while (TRUE) { // loop forever
down(&mutex); // gain access
to reader_count
reader_count = reader_count + 1; // increment the
reader_count
if (reader_count == 1)
down(&db); // if this is
the first process to read the database,
// a down on db
is executed to prevent access to the
// database by a
writing process
up(&mutex); // allow other
processes to access reader_count
read_db(); // read the database
down(&mutex); // gain access
to reader_count
reader_count = reader_count - 1; // decrement
reader_count
if (reader_count == 0)
up(&db); // if there are
no more processes reading from the
// database,
allow writing process to access the data
up(&mutex); // allow other
processes to access reader_countuse_data();
// use the data
read from the database (non-critical)
}
Writer()
{
while (TRUE) { // loop forever
create_data(); // create data
to enter into database (non-critical)
down(&db); // gain access
to the database
write_db(); // write
information to the database
up(&db); // release
exclusive access to the database
}
| Is This Answer Correct ? | 3 Yes | 3 No |
Post New Answer View All Answers
develop a program to calculate and print body mass index for 200 employees
can you please write a program for deadlock that can detect deadlock and to prevent deadlock.
how to write a program that opens a file and display in reverse order?
Write a (n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)
write a program that can LOCATE and INSERT elements in array using c++ programming languages.
how to diplay a external image of output on winxp by using c & c++,
write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n
i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.
Code for Two Classes for Doing Gzip in Memory?
solve the problem in the programming language C++"if a five digit number is input through the keyboard.Write a program to calculate the sum of its digits(hint: use the modulus operator)
Code for Easily Using Hash Table?
Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?
write a program that reverses the input number of n.Formulate an equation to come up with the answer.
Code for Method of Handling Factorials of Any Size?
Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?