Operating System: By: Vandana Kate
Operating System: By: Vandana Kate
Lecture 12
By:
Vandana Kate
We have discussed
• Process Synchronization controls the execution of
processes running concurrently so as to produce the
consistent results.
• Critical section is a part of the program where shared
resources are accessed by the process.
Critical Section Problem-
This type of condition where the sequence of execution of the processes affects the
result is called race condition.
Synchronization Mechanisms-
Entry Section-
Exit Section-
Only one process can execute its critical section at a time. The other process must wait
until the previous process has completed its critical section execution completely
2. Progress
If a process doesn’t want to enter in its critical section. It should not be permitted to
block another process from entering it, in its critical section.
3. Bounded Waiting
There is a bounded time up to which the process has to wait to enter its critical section
after making the request. The system can’t keep waiting, a process for the indefinite time
to enter its critical section. Anyhow the execution of the critical section takes a short
duration. So, every process requesting to enter its critical section get the chance within
the finite amount of time.
Solution to Implement Critical Section
Here even, if context switch happens the processes would not get into a deadlock as
in the previous case. This solution is called Peterson’s solution.
Key Takeaways
• There are multiple processes in the system and some of them share the common
resources, so they need to be synchronized.
• The race condition is when the order of execution, of the processes, sharing a common
resource, reflect the change in the result.
• To ignore race condition, the code for accessing the shared resources is written under
the critical section of the process.
• Two or more process can never enter the critical section simultaneously.
• A process not wishing to enter its critical section, should not block the entry of another
process in its critical section.
• A process should not be kept waiting for an indefinite time to enter its critical section.
• This is all about the critical section. The successful implementation of the critical section
avoids the race condition.
Race Condition
account.balance = Rs200;
int withdraw (account, amount = Rs50){
balance = account.balance;
balance -= amount;
account.balance = balance;
return balance;
}
• In this case,
• Process P2 will be trapped inside an infinite while loop.
• However, process P1 gets the chance to execute.
• Process P1 breaks the while loop condition, executes the critical section and then sets S 1 = 1.
• Now, S1 = 1 and S2 = 1.
• Now, process P1 can not enter the critical section again but process P2 can enter the critical section.
• Process P2 breaks the while loop condition, executes the critical section and then sets S 2 = 0.
• Now, S1 = 1 and S2 = 0.
• Now, process P2 can not enter the critical section again but process P1 can enter the critical section.
•
• Thus,
• Processes P1 and P2 executes the critical section alternately starting with process P 1.
• Mutual exclusion is guaranteed.
• Progress is not guaranteed because if one process does not execute, then other process would never be able to execute
again.
• Processes have to necessarily execute the critical section in strict alteration.
•
• Case-03: If S1 = 1 and S2 = 0-
•
• This case is same as case-02.
•
• Case-04: If S1 = 1 and S2 = 1-
•
• This case is same as case-01.
•
• Thus, Overall we can conclude-
• Processes P1 and P2 executes the critical section alternatively.
• Mutual exclusion is guaranteed.
• Progress is not guaranteed because if one process does not execute, then other
process would never be able to execute again.
• Processes have to necessarily execute the critical section in strict alteration.
•
• Thus, Option (A) is correct.