Today we will read about semaphore in operating system. And to know about its types in easy language too, then let's start.
Semaphore in Operating System
Semaphores are what they are variables. Semaphores are used to prevent threads or processes from accessing a single resource.
In other words, "It is used to solve the problem of critical section and to solve process synchronization."
In multi-programming, if many processes are using a common resource, then they need to access that resource in such a way that those processes do not disturb or interfere with each other.
So semaphore is used to prevent this disturbance and interfere.
The semaphore is mostly used for two purposes.
First, to share the same memory space.
And second, to share access to files.
The concept of semaphore was proposed by Edsgar Diskrstra.
There are two types of semaphores
binary semaphores
Counting Semaphores
The binary semaphore can only take 0 & 1 values.
And what is the counting semaphore can take a non negative integer value.
There are two operations defined in semaphores:- wait and signal.
The entry to the critical section is handled by the wait operation and the exit from the critical section is handled by the signal operation.
The wait and signal operations are also called P and V operations.
The mechanism of action of semaphores (S) is as follows:-
1:- wait command P(S) which is it decreases the value of semaphore by 1. If then its value comes negative then the P operation has to wait till the condition is satisfied.
2:- What is the V(S) command that adds 1 to the value of the semaphore.
The mutual exclusion in semaphores is applicable only within P(S) and V(S). If multiple processes try to access P(S) at the same time, then only one process will be allowed while other processes will have to wait.
These operations are defined below:-
P(S) or wait(S):
If S > 0 then
Set S to S-1
Else
Block the calling process (i.e. Wait on S)
V(S) or signal(S):
If any processes are waiting on S
Start one of these processes
Else
Set S to S+1
So we can say that semaphore is a technique that ensures mutual exclusion between processes.
The C programming language has a set of interfaces or functions that manage semaphores.
Request:- If you liked this post, then tell us through comment and share it with your friends. Thank you.
NOTE : आप computer science से सम्बंधित content हमे इस google Form पर भेजे जो हम आपके द्वारा भेजे हुए content को वेबसाइट पर आपकी photo सहित प्रकाशित करेंगे।