OS Unit - 3
OS Unit - 3
Principles of Concurrency:
Principles of Concurrency
Both interleaved and overlapped processes can be viewed as examples of
concurrent processes, they both present the same problems.
The relative speed of execution cannot be predicted. It depends on the
following:
The activities of other processes
The way operating system handles interrupts
The scheduling policies of the operating system.
Problems in Concurrency:
pg. 1
Locating programming errors –
Advantages of Concurrency:
Better performance –
pg. 2
Issues of Concurrency:
Non-atomic –
Race conditions –
Blocking –
Starvation –
Deadlock –
pg. 3
being used must satisfy the property of mutual exclusion, without which it
would not be possible to get rid of a race condition.
Example:
In the clothes section of a supermarket, two people are shopping for clothes .
Boy A decides upon some clothes to buy and heads to the changing room to
try them out. Now, while boy A is inside the changing room, there is an
‘occupied’ sign on it – indicating that no one else can come in. Girl B has to
use the changing room too, so she has to wait till boy A is done using the
changing room.
pg. 4
Once boy A comes out of the changing room, the sign on it changes from
‘occupied’ to ‘vacant’ – indicating that another person can use it. Hence, girl
B proceeds to use the changing room, while the sign displays ‘occupied’
again.
The changing room is nothing but the critical section, boy A and girl B are two
different processes, while the sign outside the changing room indicates the
process synchronization mechanism being used.
H/W Support:
we are going to learn about hardware protection and it’s the type. so first
let’s see the type of hardware which is used in a computer system. we know
that a computer system contains the hardware like processor, monitor, RAM
and many more, and one thing that the operating system ensures that these
devices can not directly accessible by the user.
pg. 5
Basically, hardware protection is divided into 3 categories: CPU protection,
Memory Protection, and I/O protection. These are explained as following
below.
1. CPU Protection:
2. Memory Protection:
Bare register
Limit register
3. I/O Protection:
So when we’re ensuring the I/O protection then some cases will never
have occurred in the system as:
pg. 6
Semaphores:
Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for
process synchronization.
The definitions of wait and signal are as follows −
Wait
The wait operation decrements the value of its argument S, if it is positive.
If S is negative or zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
pg. 7
Types of Semaphores:
There are two main types of semaphores i.e. counting semaphores
and binary semaphores. Details about these are given as follows −
Counting Semaphores:
These are integer value semaphores and have an unrestricted value
domain. These semaphores are used to coordinate the resource
access, where the semaphore count is the number of available
resources. If the resources are added, semaphore count
automatically incremented and if the resources are removed, the
count is decremented.
Binary Semaphores:
The binary semaphores are like counting semaphores but their
value is restricted to 0 and 1. The wait operation only works when
the semaphore is 1 and the signal operation succeeds when
semaphore is 0. It is sometimes easier to implement binary
semaphores than counting semaphores.
Advantages of Semaphores:
Semaphores allow only one process into the critical section. They follow
the mutual exclusion principle strictly and are much more efficient than
some other methods of synchronization.
There is no resource wastage because of busy waiting in semaphores as
processor time is not wasted unnecessarily to check if a condition is
fulfilled to allow a process to access the critical section.
Semaphores are implemented in the machine independent code of the
microkernel. So they are machine independent.
pg. 8
Disadvantages of Semaphores:
Pipes:
pg. 9
Message Passing:
pg. 10
In the above diagram, both the processes P1 and P2 can access the message
queue and store and retrieve data.
Signals:
Example:
In the example below, the SIGINT ( = 2) signal is blocked and no signals are
pg. 11
pending.
Monitors:
pg. 12
between processes. For example Java Synchronized methods. Java provides
wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined
together in a special kind of module or a package.
2. The processes running outside the monitor can’t access the internal
variable of the monitor but can call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.
Condition
Variables
There are two types of operations that we can perform on the condition
variables of the monitor:
1. Wait
2. Signal
Wait Operation:
a.wait(): - The process that performs wait operation on the
condition variables are suspended and locate the suspended
process in a block queue of that condition variable.
pg. 13
Signal Operation:
pg. 14