OS UNIT 3 NOTES
OS UNIT 3 NOTES
Process Coordination
Q. Explain Synchronization: Principles of Concurrency.
Concurrency is the execution of the multiple instruction sequences at the same
time.
It is different than parallelism wherein tasks are running in parallel, but use of
multi-processor machine is must.
Concurrency require multi-processor machine.
Processes are concurrent if they exist at the same time.
There are two types of concurrent processes.
a) Independent Process
The process that does not affect or is affected by the other process while its
execution then the process is called Independent Process.
Example The process that does not share any shared variable, database, files, etc.
b) Cooperative Process
Cooperating Process in the operating system is a process that gets affected by
other processes under execution or can affect any other process under execution.
For synchronization purpose: -
The task or process must communicate with each other through IPC mechanism
Successful use of concurrency among processes requires the ability to define critical
section/region and enforce mutual exclusion.
Concurrent access to shared data may result in data inconsistency.
Maintaining data consistency requires mechanisms to ensure the orderly execution
of cooperating processes.
Shared-memory solution to bounded-buffer problem allows at most n – 1 item in
buffer at the same time.
Synchronization problems
1. Readers-writer problem
2. Producer Consumer problem
3. Dining philosopher problem
2
I) Shared Memory Method
Ex: Producer-Consumer problem
There are two processes: Producer and Consumer.
The producer produces some items and the Consumer consumes that item.
The two processes share a common space or memory location known as a buffer
where the item produced by the Producer is stored and from which the Consumer
consumes the item if needed.
There are two versions of this problem: the first one is known as the unbounded
buffer problem in which the Producer can keep on producing items and there is no
limit on the size of the buffer, the second one is known as the bounded buffer
problem in which the Producer can produce up to a certain number of items before
it starts waiting for Consumer to consume it.
We will discuss the bounded buffer problem.
First, the Producer and the Consumer will share some common memory, then the
producer will start producing items.
If the total produced item is equal to the size of the buffer, the producer will wait
to get it consumed by the Consumer.
Similarly, the consumer will first check for the availability of the item.
If no item is available, the Consumer will wait for the Producer to produce it.
If there are items available, Consumer will consume them.
II) Messaging Passing Method
Now, we will start our discussion of the communication between processes via
message passing.
In this method, processes communicate with each other without using any kind of
shared memory.
If two processes p1 and p2 want to communicate with each other, they proceed as
follows:
Establish a communication link (if a link already exists, no need to establish it
again.)
Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
The message size can be of fixed size or of variable size.
If it is of fixed size, it is easy for an OS designer but complicated for a programmer
and if it is of variable size then it is easy for a programmer but complicated for the
OS designer.
A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id,
message length, and control information.
3
The control information contains information like what to do if runs out of buffer
space, sequence number, priority. Generally, message is sent using FIFO style.
Role of Synchronization in Inter Process Communication
It is one of the essential parts of inter process communication.
Typically, this is provided by interprocess communication control mechanisms, but
sometimes it can also be controlled by communication processes.
These are the following methods that used to provide the synchronization:
1. Mutual Exclusion
2. Semaphore
3. Barrier
4. Spinlock
It is generally required that only one process thread can enter the critical section at a
time.
This also helps in synchronization and creates a stable state to avoid the race condition.
Semaphore: -
Semaphore is a type of variable that usually controls the access to the shared resources
by several processes.
Semaphore is further divided into two types which are as follows:
Barrier: -
A barrier typically not allows an individual process to proceed unless all the
processes does not reach it.
It is used by many parallel languages, and collective routines impose barriers.
Spinlock: -
Spinlock is a type of lock as its name implies.
The processes are trying to acquire the spinlock waits or stays in a loop while
checking that the lock is available or not.
It is known as busy waiting because even though the process active, the process does
not perform any functional operation (or task).
Approaches to Interprocess Communication
4
These are a few different approaches for Inter- Process Communication:
1. Pipes
2. Shared Memory
3. Message Queue
4. Direct Communication
5. Indirect communication
6. Message Passing
7. FIFO
Pipe: -
The pipe is a type of data channel that is unidirectional in nature.
It means that the data in this type of data channel can be moved in only a single
direction at a time. Still, one can use two-channel of this type, so that he can be able to
send and receive data in two processes.
Typically, it uses the standard methods for input and output.
These pipes are used in all types of POSIX systems and in different versions of
window operating systems as well.
Shared Memory: -
Message Passing: -
5
It is a type of mechanism that allows processes to synchronize and communicate with
each other.
However, by using the message passing, the processes can communicate with each
other without restoring the hared variables.
Usually, the inter-process communication mechanism provides two operations that are
as follows:
send (message)
received (message)
Direct Communication: -
In this type of communication process, usually, a link is created or established between
two communicating processes.
However, in every pair of communicating processes, only one link can exist.i.e.,
Indirect Communication
Indirect communication can only exist or be established when processes share a common
mailbox, and each pair of these processes shares multiple communication links.
These shared links can be unidirectional or bi-directional.
FIFO: -
It is a type of general communication between two unrelated processes.
It can also be considered as full duplex, which means that one process can
communicate with another process and vice versa.
Why we need interprocess communication?
There are numerous reasons to use inter-process communication for sharing the
data.
Here are some of the most important reasons that are given below:
It helps to speedup modularity.
Computational, Privilege separation, Convenience helps operating system to
communicate with each other and synchronize their actions as well.
It is a way of a making sure that if one process is using a shared variable or file, the
other process will be excluded from doing the same thing.
It is a program object that prevents simultaneous access to a shared resource.
Concept used in concurrent programming by taking care of CRITICAL SECTION ( it
is a piece of code in which processes or threads access a shared resource)
Arrange resources such that NO TWO PROCESSES EVER ENTER IN THEIR
CRITICAL SECTION / REGION (at the same time).
Mutual exclusion is a mechanism to ensure that only one process is doing certain things
at one time, this avoids data inconsistency.
Mutual exclusion algorithm ensures that if a process is already performing write
operation on a data object no other process/thread is allowed to access the same object
7
until the first process has finished writing upon the data object and released the object
for other processes to read and write upon.
All others should be prevented from modifying shared data until the current process
finishes.
There is thus exclusion of one process by another. In certain regions of
an operating system.
The requirement of mutual exclusion was first Solution of a problem in concurrent
programming control, and is credited as the first topic in the study of concurrent
algorithms.
Distributed algorithm:
If a site possesses the unique token, it is allowed to enter its critical section.
This approach ensures Mutual exclusion as the token is unique.
Each request for critical section contains a sequence number.
This sequence number is used to distinguish old and current requests.
This approach ensures Mutual exclusion as the token is unique.
Example: Suzuki-Kasami’s Broadcast Algorithm
8
2) Non-token-based approach:
Instead of requesting permission to execute the critical section from all other sites, Each
site requests only a subset of sites which is called a quorum.
Any two subsets of sites or Quorum contains a common site.
This common site is responsible to ensure mutual exclusion.
Example: Maekawa’s Algorithm.
Interrupt Disabling
A process runs until it invokes an operating system service or until it is interrupted.
Disabling interrupts guarantees mutual exclusion.
Processor is limited in its ability to interleave programs.
Multiprocessing
• disabling interrupts on one processor will not guarantee mutual exclusion.
Special Machine Instructions
At a hardware level, access to a memory location excludes any other access to the same
location.
Performed in a single instruction cycle (Not subject to interference from other
instructions)
Reading and writing
Reading and testing
9
Q. Explain Mutual Exclusion: OS Support (Semaphores and MUTEX)?
It consists of
1. Semaphores
2. Monitors
3. Message Passing
Semaphores
Two or more processes can cooperate by means of simple signals
A process is forced to stop at a specified place until it has received a specific signal
Special variable called a semaphore is used for signaling
To transmit a signal via a semaphore s, a process executes primitive signal(s) (V -
increment in dutch)
To receive a signal via semaphore s, a process executes primitive wait(s) (P - test in
dutch)
If a process is waiting for a signal, it is suspended until that signal is sent.
Wait and signal operations cannot be interrupted.
Queue is used to hold processes waiting on the semaphore.
Mutual Exclusion:
Disabling interrupts: Each process disables all interrupts just after entering in its critical
section and reenable all interrupts just before leaving critical section.
With interrupts turned off the CPU could not be switched to other process.
Hence, no other process will enter its critical and mutual exclusion achieved.
Disable interrupt before entering in critical region What will happen if process forget to
enable interrupt?
10
Q. What will happen in multicore environment?
In this proposed solution, the integer variable 'turn' keeps track of whose turn is to enter
the critical section.
Initially, process A inspect turn, finds it to be 0, and enters in its critical section.
Process B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it
becomes 1.
Continuously testing a variable waiting for some value to appear is called the Busy-
Waiting.
Spin lock problem ,wastage of CPU cycles.
A lock that is uses busy waiting is called a spin lock.
Q. What is Semaphore?
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to
manage concurrent processes by using a simple integer value, which is known as a
semaphore.
A semaphore is simply an integer variable that is shared between threads.
This variable is used to solve the critical section problem and to achieve process
synchronization in the multiprocessing environment.
Semaphores are of two types:
Binary Semaphore –
This is also known as mutex lock.
11
It can have only two values – 0 and 1.
Its value is initialized to 1. It is used to implement the solution of critical section
problems with multiple processes.
Counting Semaphore –
Its value can range over an unrestricted domain.
It is used to control access to a resource that has multiple instances.
Q. What is Mutex?
Mutex ensures that the code in the critical section (which has shared resources) being
controlled will only be used by a single thread at a time.
Easy and efficient for implementation.
Can be one of two states: Locked or Unlocked.
A thread having the lock of mutex can use the critical section while other threads must
wait till the lock is released.
Mutex tries to solve the famous 'Producer Consumer Problem'.
As it ensures mutual exclusion, either the producer or consumer has their key(mutex),
so at a given time either the producer fills the buffer or the consumer consumes it but
both cannot occur at once.
Advantages of Mutex
Mutexes are nothing more than basic locks that must be obtained prior to entry in a
critical section and subsequently released.
Absence of race situations since a single thread is in the crucial region at a particular
time, and data consistency is there.
Disadvantages of Mutex
Use of mutex can lead to Starvation.
12
eg: A thread on gaining lock enters the critical section, but if this thread gets preempted
or goes to sleep, then the other process cannot enter the critical section as the other
process will wait till the lock is released.
Locking or unlocking cannot be done from a different context.
A single thread is allowed in the critical section at any instance.
The standard implementation may result in a busy waiting state, wasting CPU time.
It is object that contains both data and procedures needed to perform allocation of
shared resource.
It is PL construct that support both data access synchronization & control
synchronization.
Implemented in Pascal, JAVA, C++.
Monitor is an abstract data type for which only one process may be executing a
procedure at any given time.
The processes running outside the monitor can’t access the internal variable of the
monitor but can call procedures of the monitor.
JAVA makes extensive use of monitors to implement Mutual Exclusion.
It is the collection of condition variables and procedures combined together in a special
kind of module or a package.
cwait : it suspends execution of calling process on condition.
The suspended processes are placed in block queue of that condition variable
csignal : resume execution of some process blocked after a cwait on same condition.
When a process performs signal operation on condition variable, one of the blocked
processes is given chance
13
Advantages of Monitor:
Monitors have the advantage of making parallel programming easier and less error
prone than using techniques such as semaphore.
Disadvantages of Monitor:
Monitors have to be implemented as part of the programming language.
The compiler must generate code for them.
This gives the compiler the additional burden of having to know
what operating system facilities are available to control access to critical sections in
concurrent processes.
Some languages that do support monitors are Java,C#,Visual Basic,Ada and concurrent
Euclid.
1. Readers – Writers
2. Producer Consumer Problem (bounded buffer)
For Resource
For Process
Kill a process
This method of deadlock recovery via killing processes is the most basic method of
deadlock recovery.
Sometimes it is best to kill a process that can be restarted from the beginning with no ill
effects.
Assume we have opened an app, say "Google Chrome," but while browsing, the
browser may crash and stop working.
And our system has reached a point where we are unable to do anything; thus, restarting
your app is the best way to resolve this issue, followed by simply opening the task
manager to terminate the process.
16
Killing a process can solve our problem but the bigger concern is to decide which
process to kill.
Generally, the Operating system kills a process which has done least amount of work
until now.
17