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

operating system synchronizatin

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

operating system synchronizatin

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

Process Synchronization in OS

When two or more process cooperates with each other, their order of execution must be
preserved otherwise there can be conflicts in their execution and inappropriate outputs can
be produced.

Process Synchronization was introduced to handle problems that arose while multiple
process executions.

Process is categorized into two types on the basis of synchronization and these are given
below:

 Independent Process

 Cooperative Process

Independent Processes

Two processes are said to be independent if the execution of one process does not affect the
execution of another process.

Cooperative Processes

Two processes are said to be cooperative if the execution of one process affects the
execution of another process. These processes need to be synchronized so that the order of
execution can be guaranteed.

cooperative process:-

A cooperative process is the one which can affect the execution of other process or can be
affected by the execution of other process. Such processes need to be synchronized so that
their order of execution can be guaranteed.

The procedure involved in preserving the appropriate order of execution of cooperative


processes is known as Process Synchronization.
Race Condition:-

At the time when more than one process is either executing the same code or accessing the
same memory or any shared variable; In that condition, there is a possibility that the output
or the value of the shared variable is wrong so for that purpose all the processes are doing
the race to say that my output is correct. This condition is commonly known as a race
condition.

Mainly this condition is a situation that may occur inside the critical section. Race
condition in the critical section happens when the result of multiple thread execution differs
according to the order in which the threads execute.

Critical Section
The regions of a program that try to access shared resources and may cause race conditions
are called critical section. To avoid race condition among the processes, we need to assure
that only one process at a time can execute within the critical section.

Critical Section Problem in OS (Operating System)

Critical Section is the part of a program which tries to access shared resources. That
resource may be any resource in a computer like a memory location, Data structure, CPU or
any IO device.

The critical section cannot be executed by more than one process at the same time;
operating system faces the difficulties in allowing and disallowing the processes from
entering the critical section.

The critical section problem is used to design a set of protocols which can ensure that the
Race condition among the processes will never arise.
In order to synchronize the cooperative processes, our main task is to solve the critical
section problem. We need to provide a solution in such a way that the following conditions
can be satisfied.

A Critical Section is a code segment that accesses shared variables and


has to be executed as an atomic action. It means that in a group of
cooperating processes, at a given point of time, only one process must be
executing its critical section.

Entry Section

In this section mainly the process requests for its entry in the critical
section.

Exit Section

This section is followed by the critical section.

Requirements of Synchronization mechanisms


1. Mutual Exclusion

Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if
one process is executing inside critical section then the other process must not enter
in the critical section.

2. Progress

progress means that if one process doesn't need to execute into critical section then
it should not stop other processes to get into the critical section.

Secondary
1. Bounded Waiting

We should be able to predict the waiting time for every process to get into the critical
section. The process must not be endlessly waiting for getting into the critical
section.

2. Architectural Neutrality

Our mechanism must be architectural neutral. It means that if our solution is working
fine on one architecture then it should also run on the other ones as well.

Introduction to Semaphores
Semaphore is a Hardware Solution. This Hardware solution is written or given to critical
section problem.
In 1965, Dijkstra proposed a new and very significant technique for managing concurrent
processes by using the value of a simple integer variable to synchronize the progress of
interacting processes. This integer variable is called a semaphore. So it is basically a
synchronizing tool and is accessed only through two low standard atomic
operations, wait and signal designated by P(S) and V(S) respectively.

In very simple words, the semaphore is a variable that can hold only a non-negative
Integer value, shared between all the threads, with operations wait and signal, which work
as follow:

P(S): if S >= 1 then S := S - 1

else <block and enqueue the process>;

V(S): if <some process is blocked on the queue>

then <unblock a process>

else S := S + 1;

The classical definitions of wait and signal are:

 Wait: This operation decrements the value of its argument S, as


soon as it would become non-negative(greater than or equal to 1).
This Operation mainly helps you to control the entry of a task into
the critical section. In the case of the negative or zero value, no
operation is executed. wait() operation was originally termed as P; so
it is also known as P(S) operation. The definition of wait operation
is as follows:

wait(S)

while (S<=0);//no operation

S--;

Note:
When one process modifies the value of a semaphore then, no other
process can simultaneously modify that same semaphore's value. In the
above case the integer value of S(S<=0) as well as the possible
modification that is S-- must be executed without any interruption.

 Signal: Increments the value of its argument S, as there is no more


process blocked on the queue. This Operation is mainly used to
control the exit of a task from the critical section. signal() operation
was originally termed as V; so it is also known as V(S) operation.
The definition of signal operation is as follows:

signal(S)

S++;

Also, note that all the modifications to the integer value of semaphore in
the wait() and signal() operations must be executed indivisibly.

Properties of Semaphores

1. It's simple and always have a non-negative integer value.

2. Works with many processes.

3. Can have many different critical sections with different semaphores.

4. Each critical section has unique access semaphores.

5. Can permit multiple processes into the critical section at once, if


desirable.

We will now cover the types of semaphores in the Operating system;

Types of Semaphores

Semaphores are mainly of two types in Operating system:

1. Binary Semaphore:
It is a special form of semaphore used for implementing mutual
exclusion, hence it is often called a Mutex. A binary semaphore is
initialized to 1 and only takes the values 0 and 1 during the
execution of a program. In Binary Semaphore, the wait operation
works only if the value of semaphore = 1, and the signal operation
succeeds when the semaphore= 0. Binary Semaphores are easier to
implement than counting semaphores.

2. Counting Semaphores:

These are used to implement bounded concurrency. The


Counting semaphores can range over an unrestricted domain.
These can be used to control access to a given resource that
consists of a finite number of Instances. Here the semaphore count
is used to indicate the number of available resources. If the
resources are added then the semaphore count automatically gets
incremented and if the resources are removed, the count is
decremented. Counting Semaphore has no mutual exclusion.

Example of Use

Here is a simple step-wise implementation involving declaration and


usage of semaphore.

Shared var mutex: semaphore = 1;

Process i

begin

P(mutex);

execute CS;

V(mutex);

End;
Advantages of Semaphores

Benefits of using Semaphores are as given below:

 With the help of semaphores, there is a flexible management of


resources.

 Semaphores are machine-independent and they should be run in


the machine-independent code of the microkernel.

 Semaphores do not allow multiple processes to enter in the critical


section.

 They allow more than one thread to access the critical section.

 As semaphores follow the mutual exclusion principle strictly and


these are much more efficient than some other methods of
synchronization.

 No wastage of resources in semaphores because of busy waiting in


semaphores as processor time is not wasted unnecessarily to check
if any condition is fulfilled in order to allow a process to access the
critical section.

Disadvantages of Semaphores

 One of the biggest limitations is that semaphores may lead to


priority inversion; where low priority processes may access the
critical section first and high priority processes may access the
critical section later.

 To avoid deadlocks in the semaphore, the Wait and Signal


operations are required to be executed in the correct order.

 Using semaphores at a large scale is impractical; as their use leads


to loss of modularity and this happens because the wait() and
signal() operations prevent the creation of the structured layout for
the system.
 Their use is not enforced but is by convention only.

 With improper use, a process may block indefinitely. Such a


situation is called Deadlock. We will be studying deadlocks in detail
in coming lessons.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy