Hello friends welcome back to our channel so in today's session we'll discuss about One More Concept in our operating system that is a Sim of force so what is this semaphore so the semaphore is mainly used as an integer variable to prevent the race condition right so SEMA 4 it is an integer variable integer variable which can be used as a shared variable so which can be used as a shared variable to prevent race condition to prevent average condition so actually in our previous session we have discussed about the uh threat concept A process concept and we have seen the process synchronization right so there we have seen the critical section problem so only one process should be entered into the critical section so that other process need to wait for its execution until the critical section is available right so that we have seen in our previous session and that in order to solve the critical section so the three conditions should be satisfied that one is a mutual exclusion so mutual exclusion means anytime only one process should complete its execution right so for that purpose we'll use a semaphore as a solution to prevent the race condition so race condition means if two processors are executing concurrently there might be a chance of getting the inconsistency on data so that we call it as a race condition both the process will be having a different results and both the process will be treating that their own result will be the correct one right so such type of thing we call it as a race condition so in order to prevent that race condition we'll use a semaphore as a integer variable which can be used as a shared variable so actually if you are using the process synchronization if you are using some multiple process which are being executed continuously some shared resources will be uh I mean some sort of resources will be shared between both the process right so those resources may be the variables or functions or any other resources right so this integer variable will be acting as a shared variable among a different processes in order to prevent the risk condition and see here this semaphore is implemented with the help of two procedures so see semaphore is implemented implemented using two procedures two procedures so first one is a weight procedure and the second one is signal procedure signal procedure so this will tell the process whether it is able to enter into the critical section or it has to wait for some time so for that purpose we are going to use this weight and signal in order to implement the symbol for and see both our Atomic procedures both are Atomic procedures so here the term Atomic procedures means until it completes its execution it should not be interrupted by any other process right so if the weight process if the weight procedure is being executed by the process so the until the process completes its weight procedure no other process should interrupt this one so after successful completion of all the instructions available in the weight then only the next process will be being executed so that's why we call it as an atomic procedures so no process will interrupt right so let us take with a weight procedure weight procedure so in this weight procedure it mainly tell that whether the process is able to use the critical section or not so it gives the status for the process to enter into critical section critical section right and if the symbol for value value is greater than or equal to 1. 1 then it indicates it indicates process can avail the critical section so the process can enter into the critical section that means so as we said that the SEMA 4 is an integer variable right so integer variable if that integer variable is a greater than or equal to 1 that implies the critical section is available for the processor so a process can enter into the critical section a process can enter into the critical section so whenever the process enters into the critical section the symbol for Value will be decremented the SEMA 4 value will be decremented so immediately what happens so Sigma 4 value SEMA 4 value will be decremented so just for understanding uh let us assume that Sigma 4 value is nothing but the number of resources okay so just for understanding semaphore value is a number of resources so if there are any resources available that means if there are If the symbol value is 1 that that implies one resource is available so automatically the process can uh utilize that particular resource right so such a way if the pro if the semaphore value is greater than or equal to 1 that implies some sort of resources are available and that can be utilized by by some other process so a process can Avail the resources which are available by entering into that critical section you see after after a section immediately the semiform value should be decremented so if if the semaphore value is not decremented that implies there are still the resources available right it indicates that still the resource is available but the resource is allocated to the process so we have to decrement the value right so the thing and the next point if it is zero if the semaphore value is zero if the semaphore value is 0 then it indicates it indicates some process is in critical section critical section so no other process should be allowed no other process should be alone into critical section right so if the SEMA 4 value is 0 that implies so just one of we have seen the sum of four value is equal to number of resources if the number of resources are 0 that implies the all the resources have been availed by other processes so the current process should wait until it gets the resources right so if you write this same procedure with the code so the code will be weight of semaphore and right on the loop while s less than or equal to zero C while s less than or equal to 0 semicolon that means it will it will be inside this particular Loop until s is greater than 1 I mean greater than 0 right so if it is s is greater less than zero it will wait in this particular Loop itself the S value will be decremented the S value will be decremented so that that means if s is greater than 0 that means if s is equal to 1 so automatically it will be decremented it will be allocated by other process allocated to other process and that will be decremented right so this is the weight this is a weight procedure then signal the second procedure is a signal procedure second procedure is a signal procedure so it's a very simple so whenever a process completes its execution in the critical section wherever it comes out of the critical section immediately the semaphore values will be incremented right so after completion of the process after completion of the process in critical section in critical section immediately the semaphore value will be incremented SEMA 4 value will be incremented value will be incremented so immediately signal of yes that is a sigma 4 yes plus plus immediately after completion of the process execution immediately the a symbol for Value will be incremented so it becomes greater than 0 that means some sort of resources are deallocated from the process and they are available for the another process so immediately yes is shared among different process okay so in the weight signal we are using the S and in the signal we are using the semaphore here we are using the decrement and here we are using the increment so whenever it is a less than or equal to 0 it will wait until it until it until the resource is available and here automatically yes to express once the process have been completed its execution immediately s will be incremented automatically FS is implemented automatically s will be greater than zero if s is greater than 0 the resource is available so some of the process can Avail the resource immediately it will be allocated for another process and yes it becomes and then s will be decremented so this is how the critical section problem will be solved with the help of weight and null procedures and both the procedures are the atomic procedures that means after one after completion successful accomplished of one procedure then only the second procedure should be executed so no process should interrupt this one right now we will see the types of procedures I mean types of semaphores what are the different types of semaphores types of SEMA Force so there are mainly two types of SEMA Force One is binary semaphore binary semaphore so where the value will be where the value is either 0 or 1. and if value is 1 value is 1 implies implies process can avail critical section critical section and if value is zero the value is 0 implies process have to wait foreign executed so it has to wait and the second category of semaphore is a count in Singapore counting semaphore so here it will be a non-negative value non-negative integer and will have more numbers more numbers and this type of semaphores are mainly used for using the pool of resources if there are multiple resources right so it is used for efficient use of multiple resources if there are more than one resource then we can use this Counting semaphode so these two are a different types of semaphores binary semaphore as well as the counting semaphore so hope you understood this one so let's see the advantages of semaphore advantages of semaphore so First Advantage we have discussed yes now we are using the semaphore to prevent the race condition in order to prevent the risk condition so so to prevent race condition and the second one to prevent the deadlocks to prevent deadlocks that means if one resource is allocated to one process and another process is also waiting for the same resource so that's a never ending process that we call it's a deadlock so in our further sessions we will discuss about the dendrons right so in order to prevent the Deadlocks also we use this semaphore and to implement Mutual exclusion which is a one condition needs to be satisfied for critical section problem so to implement Mutual exclusion Mutual exclusion and another one efficient use of resources just now we have discussed efficient use of resources for all these reasons we are going with the SEMA Force which is a integer value it is an integer value now what are the disadvantages so obviously some sort of disadvantages will be there so the first main disadvantage is we need to use this semaphore perfectly so if you are unable to use this Sima 4 perfectly automatically there might be a chance of getting a date of situation so improper usage improper usage of semaphore of semaphore may lead to may lead to deadlock situation deadlock situation in our further sessions we will see about the data what is what exactly the data and also more number of semaphores if you are using more number of semaphores may lead to high maintenance and confusion high maintenance and confusion right more number of sum of force high that may lead to high maintenance and difficult to debug difficult to debunk if any synchronization occurs so mainly we are using this simmer Force for a critical section problem and why we are using the critical section to overcome the process synchronization problem so which we have seen for the concurrent processes so in order difficult to debug if any synchronization problem occurs so these are a few advantages and disadvantages of using semaphrose right so in our session we have seen what is the semaphore where it can be used and how it can be implemented and what are the different types of semaphores and the advantages and disadvantages of using SEMA Force right let's stop here and if you are having any doubts regarding this concept feel free to post your doubts in the comment section definitely I will try to clarify all your doubts if you really enjoyed my session like my session share my session with your friends and don't forget to subscribe to our Channel thanks for watching thank you very much