in the previous lecture we have discussed about the bounded buffer problem or the producer consumer problem which was a classic problem of synchronization and we have seen how it was solved using semaphores so in this lecture as we continue with this classic problems of synchronization we'll be discussing the next problem which is the reader's writer's problem so we'll be understanding what is this reader's status problem and we will be seeing how it can be solved using the semaphores that we have been discussing so in this reader's writer's problem what we consider is a database is
to be shared among several concurrent processes so let's say that there is a database which consists of different kind of data that has to be shared between different processes now some of these processes may want only to read the data whereas others may want to update that is to read and write the database so when we have a database that is shared among different processes so different processors concurrently may want to read the data or read and write to the data so there may be some processes that only wants to read so by only wants
to read means they just want to read the data and they don't want to update or modify anything in the data but there may be also other processes that wants to update the data so by updating the data that means they want to read as well as write that means they want to read and modify the data in the database so we distinguish between these type of processes by referring to the former as readers and to the later as writers so the processes that only wants to read from the database are known as readers and
then the processes that want to read and write they are known as the writers so this is what we mean by readers and writers in this reader's writer's problem all right so now what is the actual problem in this obviously if two readers access the shared data simultaneously no adverse effect will result so if the shared data is being read by two readers at the same time simultaneously there will be no problem we know that there is no problem going to occur if two processors are reading the same data because they are not making any
changes they are just reading it so there will be no problem however if a writer and some other thread either a reader or a writer access the database simultaneously chaos may ensue so what we mean by this is that if a writer and another process which could be either a reader or writer both try to access the file simultaneously then we could have a big problem and why is that that is because when a writer is going to access the database it means it is going to write something it is going to make some modification
so at the same time let's say that if a reader is trying to read then it is not going to be able to read the exact data because while it is reading just at that time the writer is also updating or changing the data so that could be a problem and also let's say that a writer is trying to make a modification and at the same time another writer is also trying to access the database that means even that writer is trying to write something so both these writers are simultaneously trying to make some changes
in the database to the same data which will result in chaos so this should not be allowed to happen so that is a problem that we have when one writer and either a reader or writer are simultaneously trying to access the data then we are going to have problems so to ensure that these difficulties do not arise we require that the writers have exclusive access to the shared database so in order to avoid this problem we need to make sure that writers will have exclusive access that means when writers are writing something in the database
no reader should be allowed to read and also no other writer should be allowed to write when one writer is writing so this synchronization problem is referred to as the reader's writer's problem so that is what we mean by the readers writers problem so this is a problem of synchronization because here we see that concurrent processes are trying to either read or write data simultaneously so if both of them are just reading the data there is no problem but when one is writing if some other process tries to either read or write we are going
to have problems so now what we have to see is using semaphores can we solve this problem so let's see if we can solve this problem using semaphores so now we are going to see the solution to the radial striatus problem using semaphores so here we will make use of two semaphores and an integer variable so with the help of two semaphores and one normal integer variable we'll be seeing how we can solve this readers writer's problem so the first semaphore that we are going to use is a mutex so mutex is a binary semaphore
which is initialized to one and it is used to ensure mutual exclusion when the read count is updated that is when any reader enters or exits from the critical section so we'll be understanding this when we also discuss about the read count variable so rate count is a variable that is used to maintain a count of how many readers are trying to read that same data so whenever the read count variable has to be updated we will be making use of a mutex semaphore which will ensure mutual exclusion when the read count is updated so
when rate count is updated that means we should preserve mutual exclusion so no two processors try to modify the read count at the same time that is why we are going to make use of this mutex semaphore and secondly we are going to use a semaphore called wrt let's just call it right which is a semaphore that is also initialized to one and it is common to both the reader and the writer processes so this wrt or write is a sema 4 which is initialized to 1 and it is going to be shared between the
reader and the writer processes and it will take care of the writing so when we see the code we'll be understanding how it works so just remember that the two semaphores that we are going to use are mutex and the wrt which will be calling it right for easy understanding and finally we have an integer variable called readcount so keep in mind that this read count is not a semaphore it is just a normal integer variable and it is initialized to 0 and it keeps track of how many processors are currently reading the object so
read count is a variable that will keep a count of how many readers are reading the same data so if there is only one reader the read count variable will be equal to 1 if there are two read count equal to 2 and if there are no readers then read count is equal to 0 and remember that read count is also initialized to 0. mutex and wrt are initialized to 1 and read count is initialized to zero so keep in mind about these two sum of fours and the integer variable so using this let's see
how we design the code of the writer and the reader processes so first of all we'll be seeing the code of the writer process so the writer process are the process that writes or updates the data so here we have a do while loop and inside this do while loop here the writer is going to request for the critical section so here it performs a weight operation on the right semaphore so what will happen when it performs a weight operation on the right semaphore so what will happen is if this write semaphore is not being
held or used by any other processes then the writer will get access to it and it will perform the write operation on the database and while it is writing since this write semaphore is being held by the writer process the readers or other writers will not be able to write or read anything from the database because this is going to be shared between the readers and writer as i already told you so when it is held by the writer process the reader cannot read or even other writers cannot write to the database so the writer
process will take hold of this it will perform the write and after it performs the right it will leave the critical section by signaling the right variable so when it signals a right variable it means that it is releasing the right variable now so when it releases the right variable readers could take it and use it for reading or other writers could take it and use it for writing so this is the simple code of the writer process so we see that the main important thing here is it gets hold of the right semaphore which
ensures that when the writer is writing something the database will not be disturbed by either readers or other writers all right now let's see the code of the reader process so here is the core of the reader process it is a bit long compared to the writer process but let's try to understand this it's not difficult let's see here also we have a do while loop so inside this do while loop when a reader wants to read something what will it do it will perform a weight operation on the mutex semaphore so mutex is also
another semaphore and i told you that mutex is used in order to acquire a lock when the read count variable is updated so this is the read count variable which i was talking about so whenever this read count variable has to be updated the reader will acquire this mutex so that no two processes will try to modify the read count variable at the same time all right so the reader process will acquire the mutex and when it is going to read it will increment the value of the read count variable why because this particular reader
is now going to read and it has to say that yes i am coming to read so increment the count of read count so initially if it was 0 it will be incremented and it will be equal to 1 so the number of readers has now increased by 1. so then here we have an if condition which says if read count equal to 1 so what does this if condition imply if read count equal to 1 means there is at least one reader that is trying to read from the database there is at least one
reader present so when there is a reader process that is reading we should not allow the writer to come and modify it so in this readers writer's problem there are different variations where they may be differently defined based on the priority that is given either to the reader or the writer so in this example that we are discussing now let's say that we are giving priority to the reader so here if at least one reader is present we should not allow the writer to come and modify anything until and unless the reader finishes its reading
so if read count equal to 1 wait wrt that means it will acquire the write semaphore so when it acquires a write semaphore what will happen the write semaphore is now with the reader process and when it is with the reader process even if a writer tries to write it will not be able to get this right semaphore as i told you right is shared between reader and writer so when we see that when a writer tries to write something it is also going to try to get hold of the write semaphore here but if
it is already held by the reader process here the writer will not be able to get it and hence it will not be able to write so this ensures that no writer can enter if there is even one reader present okay and after this it signals the mutex semaphore so where is the mutex semaphore here we saw that the weight operation was performed on the mutex semaphore before the read count was updated now this reader is signaling the mutex that means it is releasing that mutex semaphore which implies that other readers can enter while this
current reader is inside the critical section so i already told you that if there are two or more readers there are no problem we can allow any number of readers to read the data at the same time so this reader is now saying that if there are any other readers who wants to read they are free to come inside and read from the database so that is why it does this signal of the mutex because it will release the mutex and by releasing the mutex if there is other reader wants to read it will be
able to come and increment the read count and then enter the database and start reading so that is why this signaling of mutex is used here all right now after that the current reader performs a reading here the reading operation is performed here and let's say that it finished reading now when it finishes reading what happens it has to exit and it has to go out but before it goes out it has to decrement the value of the read count variable because a reader is now leaving so the read count variable has to now decrease
which implies that there is one reader less now so it has to modify this read count variable and decrease it so when it has to modify this i told you it always has to acquire this mutex in order to modify the recon variable so again it does a weight operation on the mutex and once it gets that it will decrement the value of the rate count which implies that the reader wants to leave and it will leave the critical section and now here it says that if read count equal to equal to 0 so what
does it imply it implies that no reader is left in the critical section so when all readers completed reading what will they do whenever they exit they will be decrementing this read count variable and there will be a time when there are no more readers left so at that time the rate count variable will be equal to zero so when read count variable is equal to zero we are going to signal the right semaphore which implies that a writer can enter so when we signal this right sigma for what happens the right semaphore will be
released by this reader processes and hence it will be available for a writer if they wants to acquire it so that is what we mean by signaling of write so it means writer can enter if they want to enter so if writer wants to enter it will now acquire it here and enter and also it will signal the mutex because here the mutex was held when it was trying to decrement this rate count so it has to also free the mutex so the signaling of the mutex is done and hence the reader leaves so this
is the code for the reader process it may look long but if you just analyze it carefully we can know that it is not difficult it is easy to understand so here again what were the problems we had the problems were that when a writer is writing no other reader or writer must be able to access the database so we see that that problem is solved here by using this write semaphore to write semaphore when it is held by one of the writers no reader can read no other writers can write because this is a
shared semaphore and only the process who is having the hold of the write semaphore can access the database so that problem is solved and also we see that when the reader is reading we allow other readers to read but we don't allow the writer to come and write at the same time when a reader is currently reading something from the database so this is an example where the priority is given to the reader because when the reader is reading the writer cannot come and preempt it and the writer cannot come and disturb it so this
is an example of how the readers writers problem is solved using semaphores so we made use of two sum of fours one is the right sum of four and another one is a mutex m of four and this read count was a normal variable that we used so this is how the radius riders problem is solved using semaphores so i hope this was clear to you thank you for watching and see you in the next one you