0% found this document useful (0 votes)
18 views

Operating System: By: Vandana Kate

The document discusses process synchronization and solutions to the critical section problem. It describes the critical section, race conditions, and three properties that must be satisfied: mutual exclusion, progress, and bounded waiting. Peterson's solution is presented as one that guarantees mutual exclusion without deadlock.

Uploaded by

Priyanshu Pareek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Operating System: By: Vandana Kate

The document discusses process synchronization and solutions to the critical section problem. It describes the critical section, race conditions, and three properties that must be satisfied: mutual exclusion, progress, and bounded waiting. Peterson's solution is presented as one that guarantees mutual exclusion without deadlock.

Uploaded by

Priyanshu Pareek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Operating System

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-

• If multiple processes access the critical section


concurrently, then results produced might be inconsistent.
• This problem is called as 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-

• It acts as a gateway for a process to enter inside the critical section.


• It ensures that only one process is present inside the critical section at
any time.
• It does not allow any other process to enter inside the critical section
if one process is already present inside it.

Exit Section-

• It acts as an exit gate for a process to leave the critical section.


• When a process takes exit from the critical section, some changes are
made so that other processes can enter inside the critical section.
Properties to Implement Critical Section
To avoid any inconsistency in the result the three properties that should be considered mandatorily to
implement critical section are as follow:
1. Mutual Exclusion

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

• There can be many solutions to implement critical section


but, as we studied above the solution must satisfy three
criteria
– i.e. Mutual exclusion (only one process can enter critical section
at a time),
– Progress (a process not wishing to enter its critical section should
not block a process whishing to enter its critical section)
– and bounded wait (a process should not have to wait for
indefinite time to enter its critical region).
Solution For Critical Section
Problem??
• Here, observe one thing that every time P0 need the value
of turn=0 to enter its CS and every time P1 need value
of turn=1 to enter its CS. So here, even if P0 after executing
its CS immediately want to reenter its CS again. It has to
wait for P1 to make the value of turn = 0. Here, we have
achieved mutual exclusion but, progress is not achieved as
if P1 does not want to enter CS it will block P0 to enter its
CS.
Case:2
Problem???
• Consider the case when P0 has just confirmed that it wants
to enter CS by making flag[0]=T and just at that moment
the context switch happens and P1 get charge and start
executing. It makes flag[1]=T and further check for
while(flag[0]) which is true now, it will get blocked in the
loop and also blocks P0.
Peterson Solution

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;
}

int deposit (account, amount=Rs 100){


balance = account.balance;
balance += amount;
account.balance = balance;
return balance;
}
Case-01: If S1 = 0 and S2 = 0-

• In this case,
• Process P1 will be trapped inside an infinite while loop.
• However, process P2 gets the chance to execute.
• Process P2 breaks the while loop condition, executes the critical section and then sets S2 = 1.
• Now, S1 = 0 and S2 = 1.
• Now, process P2 can not enter the critical section again but process P1 can enter the critical
section.
• Process P1 breaks the while loop condition, executes the critical section and then sets S1 = 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.

• Thus,
• Processes P1 and P2 executes the critical section alternately starting with process P2.
• 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-02: If S1 = 0 and S2 = 1-

• 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.

You might also like