Unit-3_PartB
Unit-3_PartB
Unit – 3
(Part-B)
Process Synchronization
Mayank Mishra
School of Electronics Engineering
KIIT-Deemed to be University
Mutex Locks
➢ The hardware-based solutions to the critical-section problem
are complicated as well as generally inaccessible to
application programmers.
➢ If the lock is available, a call to acquire() succeeds, and the lock is then considered
unavailable. A process that attempts to acquire an unavailable lock is blocked until the lock
is released.
Mutex Locks (Contd.)
Process P1 Process P2
➢ Calls to either or acquire() release() must be performed atomically.
➢ The main disadvantage of the implementation given here is that it requires busy waiting.
➢ While a process is in its critical section, any other process that tries to enter its critical
section must loop continuously in the call to acquire(). This continual looping is clearly a
problem in a real multiprogramming system, where a single CPU core is shared among
many processes. Busy waiting also wastes CPU cycles that some other process might be
able to use productively.
➢ The type of mutex lock we have been describing is also called a Spinlock because the
process “spins” while waiting for the lock to become available.
➢ Spinlocks do have an advantage, however, in that no context switch is required when a
process must wait on a lock, and a context switch may take considerable time.
➢ In certain circumstances on multicore systems, spinlocks are in fact the preferable choice
for locking. If a lock is to be held for a short duration, one thread can “spin” on one
processing core while another thread performs its critical section on another core.
➢ On modern multicore computing systems, spinlocks are widely used in many operating
systems.
Semaphores
➢ Semaphore proposed by Dutch computer scientist Edsger Dijkstra, is a technique to manage
concurrent processes by using a simple integer value, which is known as Semaphores.
➢ A semaphore S is an integer variable that, apart from initialization, is accessed only through
two standard atomic operations: wait() and signal().
Semaphores (Contd.)
➢ The wait() operation was originally termed P (from the Dutch proberen, “to test”);
signal() was originally called V (from verhogen, “to increment”).
Semaphores (Contd.)
Semaphores (Contd.)
➢ All modifications to the integer value of the semaphore in the wait() and signal() operations
must be executed atomically. That is, when one process modifies the semaphore value, no other
process can simultaneously modify that same semaphore value.
➢ In addition, in the case of wait(S),the testing of the integer value of S (S ≤ 0), as well as its
possible modification (S--), must be executed without interruption.
➢ The main disadvantage of the semaphore definition that was discussed is that it requires busy
waiting.
➢ While a process is in its critical section, any other process that tries to enter its critical section
must loop continuously in the entry code.
➢ Busy waiting wastes CPU cycles that some other process might be able to use productively.
➢ This type of semaphore is also called a spinlock because the process “spins” while waiting for the
lock.
To overcome the need for busy waiting, we can modify the definition of
the wait ( ) and signal ( ) semaphore operations
➢ When a process executes the wait( ) operation and finds that the semaphore value is not positive,
it must wait.
❑However, rather than engaging in busy waiting, the process can block itself.
❑The block operation places a process into a waiting queue associated with the
semaphore, and the state of the process is switched to the waiting state.
➢ Then control is transferred to the CPU scheduler, which selects another process to execute.
Binary Semaphores
Down( )/ Wait ( )/ P ( ) Up( )/ Signal ( )/ V ( )/ Post ( )/ Release ( )
Solution:
We know-
P operation also called as wait operation decrements the value of semaphore variable by 1.
V operation also called as signal operation increments the value of semaphore variable by
1.
Thus,
Final value of semaphore variable S
= 10 – 6 + 4
=8
Question 2: A counting semaphore S is initialized to 7. Then, 20 P operations and
15 V operations are performed on S. What is the final value of S?
Question 2: A counting semaphore S is initialized to 7. Then, 20 P operations and
15 V operations are performed on S. What is the final value of S?
Solution:
We know-
P operation also called as wait operation decrements the value of semaphore variable by 1.
V operation also called as signal operation increments the value of semaphore variable by
1.
Thus,
= 7 – 20 + 15
=2
Question 3: A shared variable x, initialized to zero, is operated on by four concurrent
processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory,
increments by one, stores it to memory and then terminates. Each of the processes Y and Z
reads x from memory, decrements by two, stores it to memory, and then terminates. Each
process before reading x invokes the P operation (i.e. wait) on a counting semaphore S and
invokes the V operation (i.e. signal) on the semaphore S after storing x to memory.
Semaphore S is initialized to two. What is the maximum possible value of x after all
processes complete execution?
A) -2
B) -1
C) 2
D) None of the above
Solution:
Solution:
A) -2
B) -1
C) 2
D) None of the above
Solution:
Final Answer: X= -4
Question 5: If a counting semaphore present value is 20, which of the following operations
will result in semaphore value 27 ?
A) 10
B) 8
C) 9
D) None
Question 6: Consider a non-negative counting semaphore S. The operation P(S)
decrements S, and V(S) increments S. During an execution, 18 P(S) operations and 7 V(S)
operations are issued in some order. The largest initial value of S for which atleast one P(S)
operation will remain blocked________?
A) 10
B) 8
C) 9
S-18+7 =-1
D) None
Correct Answer: Option A
References
1. Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, “Operating System Concepts,” Eleventh
Edition (Willey).
2. Andrew S. Tanenbaum, “Modern Operating Systems”, Fourth Edition (Pearson Publications), 2014.
3. https://www.geeksforgeeks.org/
4. https://www.javatpoint.com/
5. https://www.tutorialspoint.com/
6. https://www.nesoacademy.org/
7. https://www.baeldung.com/
8. https://www.educative.io/