Introduction to Monitor in Process Synchronization Operating System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and this video I am going to explain monitor concept in process synchronization so before discussing monitor let's see what is the problem with simultaneous access let's say we are having some data and these data are going to be shared among some processes so what will happen let's assume there are two processes so process p1 first a trying to access to data B and C so p1 is working on these two data now if process p2 also access this data C so their simultaneous access will leave data C in E inconsistent state and that condition is known as race condition so why we study these synchronization construct to avoid this race condition so in order to solve this problem we need to have a resource allocation algorithm so what this algorithm will do this algorithm will take the request from the process and it will see whether this axis will create any problem or not if not it will allow the access in case if it will create a problem like this one if p2 also try to access this data e in that case this algorithm will block it so monitor monitor is actually a modules and this module contains the data which are going to be shared among processes and it also having some procedures so if a process wants to access some data they cannot access this data directly they need to call procedures and those procedure in turn will allow access to data so with monitor only one process can enter in monitor so with monitor a lock is eh associated for it and let's say there are three process and all those three process trying to enter in monitor so only one will be succeed in entering in the monitor so every monitor will be having a lock so we can say only one process is allowed to enter in monitor at a time so monitor is a module that encapsulate shared data structures those seizures that operate on those data and synchronization between concurrent process procedures invocation so there is one more thing actually that is known as conditional variable which I will explain it later slight so with this part we have already seen only one thread or one process can enter in monitor at any time because whenever the process is trying to enter in the monitor that process need to have access to the lock of this monitor so let's say if this process is trying to enter in this monitor and right now this monitor is free so this process will enter in this monitor and this process will be having this lock now if after some time with this process p2 also wants to enter in monitor it cannot because log will associate associated with process p1 so only one thread or one process can enter in the monitor at a time so it will be blocked now what will happen with blocked threads or blocked process so as we know every monitor will be having a lock and all the process which are blocked they will be put in a queue so this Q is for this more so if there are five processes and all those are trying to enter in monitor one will we enter inside the monitor and four will be waiting so if thing this is the scenario like one process already monitor process ptooey is waiting for p1 to release this monitor and one more process trying to enter in the mountain so what will happen this p3 couldn't get the log and it will be put inside the cube so this Q is for monitor all the process which cannot enter in the monitor at that time they will be waiting in this cube so this is a simple example of monitor this is the sheer data and this is the procedure so whenever process or thread wants to access this data or want to manipulate this data they may use the procedure of this monitor so we have taken only one procedure withdrawn so this pause seizure will detect some amount from balance and it will return that balance and let's say there are three charts which want to execute this withdraw operation at the same time so what will happen let's say t1 called the withdraw operation or withdraw procedure it will execute balance equal to balance minus W so let's say it will update the balance this one is actually small B let me correct it so this is the small V this is not a capital P okay so after updating this one let us say a processor switch the charting so this time thread switch occurred and CPU is assigned to thread t2 or t2 start executing so t2 also call withdraw procedure but right now t1 is having that lock so t2 cannot continue its operation and let's say the switching also occurred after this one because this is blocked and the same thing will happen with thread t3 where it's calling withdraw procedure it's not having any lock so it will be blocked and finally switching will be for t1 and t1 will resume its operations so once t1 is done t2 will continue and once t2 is done p3 will continue so this is a basic idea how monitor works now let's see let's model this boundary buffer problem the actual solution I will discuss later I am just giving you a hint so for bounded buffer we are having a buffer and we are having some procedure we are having two processes one process is going to put some data in this buffer and another process is going to consume data from this buffer we are having procedure put item and get item now the question is that if this consume will call this process of get item and right now this buffer is empty because initially buffer is empty so what will happen if this consumer is start waiting inside the monitor how other process going to enter in the monitor because this consumer will be having the lock and we have seen we have discussed this that only one process or thread can enter inside the monitor so if consumer is already in the money how processor is oil producer is going to put it data in the profession so the thing is that if a process wants to wait inside the monitor here comes the role of conditional variable okay so now let's see how conditional variable is going to have to solve this situation so conditional variable are just regions for which a process is going to wait like in bounded buffer a consumer will wait if the buffer is empty and but for producer this condition will be different producer will wait if their buffer is full so if a process is waiting for some region that region will be considered as a conditional variable so conditional variable actually provides synchronization inside the monitor was outside the monitor that lock will be used to provide synchronization mutual exclusion but inside the monitor if a process wants to wait so for that thing conditional variable will be used so this part we have discussed and three operations can be performed on conditional variable waved signal and broadcast let's see how these operations works so wait operation so if a process enters in monitor and after entering if that process Paul wait operation so what it will do that process will release the lock for monitor and that process will be put in the waiting queue okay so whenever a wait operation is called the current process will release the lock for the monitor so that other process can enter inside the monitor and that current process will be put inside a queue signal operation signal operation it will wakes up one process which are sleeping as a result of or to wait so the process which actually put in queue as a result of called to wait operation that process will be wait so signal operation it will wakes up one of the processes from the queue and after this one the load is automatically passed to the waiter let us say a process p1 is calling signal and there is one more process p2 which is actually waiting in the queue and that p2 actually called wait operation in past so when p1 will call signal operation p1 will release its lock to the monitor and that block will be passed automatically to the process P 2 and P 2 will resume its execution and the third operation in broadcast so broadcast actually signal all the waiting processes so these operations we will discuss with a example then it will be more clear to you so let's see how this weight and signal operation what else be so conditional variable remember this one if a process is going to wait for a region so that region we will model it as a conditional variable condition variable and for every condition there will be a queue or sociated so the previous Q we have discussed that Q is for the monitor and this Q is for the conditional variable so how wait and signal operations are called on conditional variable so there may be a number of conditional variables so what we need to specify the name of condition variable and then we need need to specify the operation so if we is by the first statement like condition variable dot wait so this process P will be put inside the queue so this is the working of wait and in the case of signal condition variable signal it will signal one of the waiting process and those process will start their execution so now we are going to take a very simple example single resource allocation problem we are having a single resource and that there are two process which are trying to access that resource so we want to provide mutual exclusion so we will write in monitor and this monitor we will put the resource we are having a single resource we will provide two processor one procedure for acquiring the resource and the second one is for releasing the resource now the thing is that if a process is acquired this resource at the same time if another process is calling this procedure so how that process how this monitor is going to know whether this resource is free or not so for this purpose we are going to take a boolean variable which will show the status of this resource whether this one is in use on its field so we are taking a boolean variable busy so the initial value of this variable will be false it means the source is free now if this resource is not free what will have fun with the other process rather that process what will happen actually so we want that process to wait so we will take one conditional variable so conditional variable conditional variable actually in practice are implemented by cute so we will take one conditional variable so this Q we are representing as a conditional variable so this is the definition for our monitor monitor than name of monitor this is as data bullion busy for showing the status of this resource and condition non busy this is the conditional variable on which a process is going to wait or it is going to signal and we are having to procedure acquire and release so in case of acquire what we will do we will check whether this base is equivalent to false or true so if this is true it means that resource is already used so in that case the process which actually called acquire procedure that process will be put inside the queue and which queue queue of non busy conditional variable so non busy dot wait it means process will be put inside the non busy queue so it means this queue but if this is not true if this boolean variable is having false in that case we will come here and what it will do it will make this B equal to true and then that process will start using the resources at the time of realizing once the work of process is done that process will release the resource and it will send this busy variable as false to show that this resource is now available and next it will call non busy dot signal so what this will do it will signal a waiting process from this view now you need to understand this thing when this wait processor is called at this time the current processes process will be put inside this queue and monitor lock will be released so that another process can enter inside the might and here in case of non busy dot signal when this instruction is executed log for the monitor will be released and that log will be paused one of the process waiting in the queue and in case if this queue is already empty and if we are calling this instruction in that case signal will not be having any effect and two more things these are the constraint like it is possible that a process called release operation and it sent the value of busy to false and it called non busy dot signal after calling this statement it might be possible that a new process come and that process trying to enter in the monitor and what that process will find this that process will find that BZ equal to false so there is a constraint that no other program can intervene between the know V G dot signal and the continuation of a exactly one waiting program so after this statement waiting process will start execution this is the constraint one and the second one when this signal operation is called which process will be continued which process will be allowed to continue its execution so the process is actually waiting for long now let's see if this scenario with our monitor at say p1 and p2 both are doing some work with this resource but before that they will call acquire procedure and once their work is done they will call provision so here if p1 and p2 trying to access this monitor at the same time only one will be succeed in getting the lock and that process will enter inside the monitor so let's say this process p1 called acquire function or precision so this acquire what this acquire will do as you can see initially the value of B is false and this is the definition for at one if busy then non busy dot wait else B G dot is equal to true and the value of B is false so we will come in else part and it will set the value of B G to truth and the relook resource will be allocated to process p1 so now P 1 will be having the source R and it will continue its execution so let's say p1 is executing some code here some working it's performing some operation on this resource meanwhile process p2 also trying to access resource R now let us see what will happen so if p2 will try to access this resource if you can see the value of B change true and because of this this process p2 will be put inside the cube why because in acquire you can see if B Z so B Z is equal to true in that case our condition is true non V Z dot wait so process p2 will call a wait for a wait operation on this conditional variable and this conditional variable is represented with this Q so P 2 will be put inside the cute now when this b1 execution is done at the end it will call relief operation and this is the definition for relays operation it will send the VG value to false so this value will be changed to false and then it will call signal and what this signal will do it will wake this process of so now when this p2 is active p2 will continue its execution it will call acquire procedure then it will send BG to true this resource will be allocated to process p2 and finally it will release the resource so this is a very simple example of single resource allocation problem with mightor in the next tutorial I will explain how bounded buffer problem can be solved with monitor thank you very much for watching
Info
Channel: HowTo
Views: 77,790
Rating: 4.5555553 out of 5
Keywords: monitor, process, synchronization, mutual exclusion, concurrent, parallel, race condition, wait, signal, conditional, variable
Id: _5sbBARRpws
Channel Id: undefined
Length: 23min 21sec (1401 seconds)
Published: Sat Oct 22 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.