You are on page 1of 6

Operating System

Assignment# 3

Submitted By:
Ghulam Mohiuddin

Roll No:
17221598-117

Submitted To:
Ma’am Sara

MAY 16, 2019


Q.1: What is Process Synchronization?
On the basis of synchronization processes are categorized as one of the following
two types:
 Independent Process: Execution of one process does not affects the
execution of other processes.
 Cooperative Process: Execution of one process affects the execution of other
processes.
Process synchronization problem arises in the case of Cooperative process also
because resources are shared in Cooperative processes.
Process Synchronization is a technique which is used to coordinate the process
that use shared Data.
Q.2: What is Race Condition?
It is the condition where several processes tries to access the resources and modify
the shared data concurrently and outcome of the process depends on the particular
order of execution that leads to data inconsistency, this condition is called Race
Condition.

Example:
Q:3 What is Critical Section Problem?
A Critical Section is a code segment that accesses shared variables and has to be
executed as an atomic action. The critical section problem refers to the problem of
how to ensure that at most one process is executing its critical section at a given
time. Critical sections in different threads are not necessarily the same code
segment.
Criteria for Critical Section Problem
Formally, the following requirements should be satisfied.
 Mutual exclusion: When a thread is executing in its critical section, no
other threads can be executing in their critical sections.
 Progress: If no thread is executing in its critical section, and if there are
some threads that wish to enter their critical sections, then one of these
threads will get into the critical section.
 Bounded waiting: After a thread makes a request to enter its critical section,
there is a bound on the number of times that other threads are allowed to
enter their critical sections, before the request is granted.
In discussion of the critical section problem, we often assume that each thread is
executing the following code. It is also assumed that after first a thread enters a
critical section, it will eventually exit the critical section second a thread may
terminate in the non-critical section.
The critical-section problem is to design a protocol that the processes can use to
cooperate.
 Entry Section –
It is part of the process which decide the entry of a particular process in the
Critical Section, out of many other processes.
 Critical Section –
It is the part in which only one process is allowed to enter and modify the
shared Variable. This part of the process ensures that only no other process
can access the resource of shared data.
 Exit Section –
This process allows the other process that are waiting in the Entry Section, to
enter into the Critical Sections. It checks that a process that after a process has
finished execution in Critical Section can be removed through this Exit
Section.
 Remainder Section –
The other parts of the Code other than Entry Section, Critical Section and Exit
Section are known as Remainder Section.

1.Peterson’s solution:
Peterson's Algorithm (or Peterson's solution) is a concurrent
programming algorithm for mutual exclusion that allows two or more processes to
share a single-use resource without conflict, using only shared memory for
communication. It was formulated by Gary L. Peterson in 1981.While Peterson's
original formulation worked with only two processes, the algorithm can be
generalized for more than two.

The Algorithm:
The algorithm uses two variables, flag and turn . A flag[n] value of true indicates
that the process n wants to enter the critical section. Entrance to the critical
section is granted for process P0 if P1 does not want to enter its critical section or
if P1 has given priority to P0 by setting turn to 0.
bool flag[2] = {false, false};

int turn;

P0: flag[0] = true;


P0_gate: turn = 1;
while (flag[1] == true && turn == 1)
{
// busy wait
}
// critical section
...
// end of critical section
flag[0] = false;

P1: flag [1] = true;


P1_gate: turn = 0;
while (flag[0] == true && turn == 0)
{
// busy wait
}
// critical section
...
// end of critical section
flag[1] = false;

2.Mutux locks:
As the synchronization hardware solution is not easy to implement for everyone, a
strict software approach called Mutex Locks was introduced. In this approach, in
the entry section of code, a LOCK is acquired over the critical resources modified
and used inside critical section, and in the exit section that LOCK is released.
As the resource is locked while a process executes its critical section hence no
other process can access it.
acquire()
{ while (!available)
; /* busy wait */
available = false;;
}

do {

acquire lock

critical section

release lock

remainder section
while (true);
Solution to the critical-section problem using mutex locks.

You might also like