You are on page 1of 19

Process

Synchronization
RCS214/RAP216
What is Process
Synchronization
 Process synchronization is a concept in operating systems (OS) that
refers to the coordination and control of the execution of processes
(independent units of program execution) to ensure that they operate
correctly in a shared or concurrent environment.
 The primary goal of process synchronization is to avoid conflicts, data
inconsistencies, and race conditions that may arise when multiple
processes access shared resources simultaneously.
Key issues
Key issues addressed by process synchronization include:
1. Race Conditions:
• Race conditions occur when the behavior of a program depends on the relative timing
of events, and the outcome becomes unpredictable. Synchronization helps in preventing
race conditions by regulating the access to shared resources.
2. Deadlocks:
• A deadlock is a situation where two or more processes are unable to proceed because
each is waiting for the other to release a resource. Synchronization mechanisms help
prevent or resolve deadlocks.
3. Critical Sections:
• Critical sections are portions of code that access shared resources and need to be
executed by only one process at a time to avoid conflicts. Process synchronization
mechanisms ensure that only one process can enter a critical section at any given time.
Race Conditions

• Race conditions usually occur if two or more processes are allowed to


modify the same shared variable at the same time. OR
• Race conditions occur when the behavior of a program depends on the
relative timing of events, and the outcome becomes unpredictable.
• To prevent race conditions, the operating system must perform
process synchronization to guarantee that only one process is updating
a shared variable at any one time.
Critical Section
• Part of the process that contains the instruction or instructions that access a shared
variable or resource or
• Critical sections are portions of code that access shared resources and need to be
executed by only one process at a time to avoid conflicts. Process synchronization
mechanisms ensure that only one process can enter a critical section at any given
time
Critical Section (Solution)
The solution to this problem involves three aspects.

1. Mutual exclusion requirement should always be met in order to guarantee that


only one process may enter its critical section at a time. If a process is currently
executing its critical section, any process that attempts to enter its own critical
section should wait until the other process leaves its critical section.
2. Progress requirement. The solution must guarantee that if a process wants to
enter its critical section and no other process is in its critical section, the process
will be able to execute in its critical section.
3. Bounded waiting requirement. The solution must guarantee that processes will
not wait for an indefinite amount of time before it can enter its critical section.
Critical Section (Software solutions )
 Software solutions to the critical section problem:

• The first solution uses a global or shared


variable called TurnToEnter.
• If TurnToEnter = 0, P0 can execute its
critical section. P0 changes its value to 1 to
indicate that it has finished executing its
critical section.
• If TurnToEnter = 1, P1 can execute its
critical section. P1 changes its value to 0 to
indicate that it has finished executing its
critical section. The figure below illustrates
this.
Critical Section(Software solutions )
• The second solution uses a two-element, Boolean
array called WantToEnter.
• If a process wants to enter its critical section, it sets
its variable to true.
• If WantToEnter[0] = true, P0 wants to enter its
critical section.
• If WantToEnter[1] = true, P1 wants to enter its
critical section. Then before a process enters its
critical section, it first checks the WantToEnter
variable of the process.
• If the WantToEnter variable of the other process is
also true, it will wait until it becomes false before
proceeding in its critical section. A process will set
its WantToEnter variable to false once it exits its
critical section.
Critical Section (Peterson’s Algorithm)

• Peterson’s Algorithm. The Peterson’s Algorithm uses


both TurnToEnter and WantToEnter variables.
• Suppose P0 wants to enter its critical section, it sets
WantToEnter[0] = true and TurnToEnter = 1.
• P0 then checks if P1 wants to enter its critical section
(WantToEnter[1] == true) and if it is P1’s turn to
enter its critical section (TurnToEnter == 1).
• P0 will only proceed to execute if one of these
conditions does not exist.
Critical Section
Critical Section (Bakery Algorithm)
• Bakery Algorithm. This follows the system
used by bakeries in attending to their
customers.
• Each customer that enters the bakery gets a
number.
• As customers enter the bakery, the numbers
increase by one.
• The customer that has the lowest number
will be served first.
• Once finished, the customer discards the
assigned number. The customer must get a
new number if he or she wants to be served
again.
Critical Section (Hardware solutions )
Hardware solutions to the critical section problem:
 Disabling interrupts.
This feature enables a process to disable interrupts before it starts modifying a
shared variable. If interrupts is disabled, the CPU will not be switched from one
process to another. The operating system will then simply allow the process to
complete the execution of the critical section even though its time quantum has
finished. Upon exiting the critical section, the process will enable interrupts.

 Special hardware instructions.


There are special machine instructions that will allow a process to modify a
variable or memory location atomically. This means that if a process executes an
atomic operation, no other process will be able to preempt it.
Critical Section (example)
An example of this special instruction is the test_and_set instruction.
This instruction is illustrated in the figure below.
Critical Section

• This algorithm uses a shared Boolean


variable called lock.
• The test_and_set instruction is used to
test if lock is true or false.
• The variable lock is true if a process is
inside its critical section. Otherwise,
lock is set to false.
Semaphores
 Semaphore is a tool that can easily be used to solve more complex
synchronization problems and does not use busy waiting.
1. wait (S)
• This operation waits for the value of the
semaphore S to become greater than 0. If this
condition is satisfied, wait will decrement its
value by 1. wait(S): while S <= 0 { }; S--;
2. signal (S)
• This operation increments the value of
semaphore S by 1. signal(S): S++;
• The wait operation is atomic or cannot be
interrupted once semaphore S is found to be
greater than 0.
Classic Synchronization Problems
The Dining Philosophers Problem
Restrictions:
1. A philosopher cannot start eating unless
he has both forks.
2. A philosopher cannot pick up both forks
at the same time. He has to do it one at
a time.
3. He cannot get the fork that is being used
by the philosopher to his right or to his
left.
Classic Synchronization Problems
• A possible solution is to use a semaphore to
represent each fork. The wait operation is
used when picking up a fork while the signal
operation is used when putting down a fork.

• The mutual exclusion requirement is satisfied


since each fork is represented by a semaphore
which guarantees that only one philosopher
can use a particular fork at a time.
Classic Synchronization Problems

• This solution is often called readers-


preference solution.
• If it happens that a steady stream of
incoming reader processes wants to
access the database, this may cause
the starvation of writer processes.
• Writer processes will have to wait for
an indefinite period of time before it
can access the database.
Classic Synchronization Problems
• A solution that favors the writer processes is
called writers-preference solution.
• It still does not grant a writer access to the
database if there are readers or writers already in
the database.
• However, a reader is denied access to the
database if a writer is currently accessing the
database or if there is a waiting writer process
(even if other reader processes are already in the
database).
• If it happens that a steady stream of incoming
writer processes wants to access the database,
this may cause the starvation of reader processes.

You might also like