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

Module III Part 3

The document discusses key concepts in process synchronization, including mutual exclusion, semaphores, deadlock, and inter-task communication. It outlines the critical section problem, necessary conditions for deadlock, and methods for handling deadlocks, as well as various synchronization tools like mutex locks and semaphores. Additionally, it covers classical synchronization problems such as the bounded buffer, readers-writers, and dining philosophers problems, along with communication models like shared memory and message passing.

Uploaded by

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

Module III Part 3

The document discusses key concepts in process synchronization, including mutual exclusion, semaphores, deadlock, and inter-task communication. It outlines the critical section problem, necessary conditions for deadlock, and methods for handling deadlocks, as well as various synchronization tools like mutex locks and semaphores. Additionally, it covers classical synchronization problems such as the bounded buffer, readers-writers, and dining philosophers problems, along with communication models like shared memory and message passing.

Uploaded by

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

Mutual Exclusion, Semaphores,

Deadlock, Synchronization,
Inter task Communication
Process Synchronization

• Task of coordinating the execution of processes in a way that


no two processes can have access to the same shared data and
resources.
Example
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
Banking Example
Critical Section Problem

• The critical section is a code segment where the shared variables


can be accessed.

• i.e. only one process can execute in its critical section at a time. All
the other processes have to wait to execute in their critical
sections.
Example
Solution to the Critical Section Problem

• Mutual Exclusion: only one process can be inside the critical


section at any time.

• Progress: In other words, any process can enter a critical section


if it is free.

• Bounded Waiting: each process must have a limited waiting


time.
Deadlock

Deadlock is a situation where a set of processes are blocked


because each process is holding a resource and waiting for
another resource acquired by some other process.
Deadlock Contd…
Necessary Conditions for Deadlock

• Deadlock can arise if following four conditions hold


simultaneously (Necessary Conditions)
Mutual Exclusion: One or more than one resource are
non-sharable (Only one process can use at a time)
Hold and Wait: A process is holding at least one resource and
waiting for resources.
No Preemption: A resource cannot be taken from a process unless
the process releases the resource.
Circular Wait: A set of processes are waiting for each other in
circular form.
Methods for Handling Deadlocks
• Ensure that the system will never enter a deadlock state:
• Deadlock prevention

• Deadlock avoidance

• Deadlock detection

• Deadlock recovery
Deadlock Prevention

► Ensure that at least one of the necessary condition for


deadlocks does not hold.
► Can be accomplished restraining the ways request can be made
Deadlock Avoidance

• Ensure that the system will never enter a deadlock state


• What is a trivial way of avoiding deadlocks?

• Requires that the system to have some additional a priori


information available on possible resource requests.
• Simplest and most useful model requires that each process declare the maximum
number of resources of each type that it may need
• The deadlock-avoidance algorithm dynamically examines the resource-allocation
state to ensure that there can never be a circular-wait condition
• Resource-allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes
Basic Facts
• If a system is in safe state ⇒ no deadlocks
• If a system is in unsafe state ⇒ possibility of deadlock
Synchronizing Hardware

► Many systems provide hardware support for critical section code

► Uniprocessors–could disable interrupts

Currently running code would execute without preemption

Generally too inefficient on multiprocessor systems

► Modern machines provide special atomic hardware instructions

Atomic = non-interruptable

Either test memory word and set value

Or swap contents of two memory words


Mutex Locks

• As the synchronization hardware solution is not easy to implement


for everyone, a 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.
Example
Semaphores

wait (S) {
► Synchronization tool that does not require
while S <= 0
busy waiting
; // no-op
► Semaphore S–integer variable
S--;
► Two standard operations modify S:
}
wait()and signal()
signal (S) {
► Less complicated
S++;
► Can only be accessed via two indivisible
}
(atomic) operations
Demonstration of Semaphores
• https://www.youtube.com/watch?v=PQ5aK5wLCQE
Types of Semaphores
• Counting semaphores

• Binary semaphores
Counting Semaphores
• This type of Semaphore uses a count that helps task to be
acquired or released numerous times. If the initial count = 0,
the counting semaphore should be created in the unavailable
state.
Binary Semaphores
• The binary semaphores are quite similar to counting semaphores,
but their value is restricted to 0 and 1. In this type of semaphore,
the wait operation works only if semaphore = 1, and the signal
operation succeeds when semaphore= 0. It is easy to implement
than counting semaphores.
Classical Problems of Synchronization
• Bounded Buffer (Producer-Consumer) Problem

• Dining Philosophers Problem

• The Readers Writers Problem


Bounded Buffer Problem

• This problem is generalised in terms of the Producer Consumer problem, where a finite
buffer pool is used to exchange messages between producer and consumer processes.

• Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.

• Solution to this problem is, creating two counting semaphores "full" and "empty" to keep
track of the current number of full and empty buffers respectively.
Contd..
Solution

► One solution of this problem is to use semaphores. The semaphores which will be used
here are:

mutex, a binary semaphore which is used to acquire and release the lock.

empty, a counting semaphore whose initial value is the number of slots in the buffer,
since, initially all slots are empty.

full, a counting semaphore whose initial value is 0.


Readers Writers Problem

• In this problem there are some processes(called readers) that only


read the shared data, and never change it, and there are other
processes(called writers) who may change the data in addition to
reading, or instead of reading it.
• There are various type of readers-writers problem, most centred on
relative priorities of readers and writers.
Example

Reader Process
Writer Process
Dinning Philosopher Problem
• At any instant, a philosopher is either eating or thinking. When a
philosopher wants to eat, he uses two chopsticks - one from their
left and one from their right. When a philosopher wants to think, he
keeps down both chopsticks at their original place.
Solution
• A philosopher must be allowed to pick up the chopsticks only if both the
left and right chopsticks are available.

• Allow only four philosophers to sit at the table. That way, if all the four
philosophers pick up four chopsticks, there will be one chopstick left on the
table. So, one philosopher can start eating and eventually, two chopsticks
will be available. In this way, deadlocks can be avoided.
Inter-task Communication
• Process is of two types

• Independent Processes – Processes which do not share data with other processes .

• Cooperating Processes – Processes that shares data with other processes.


Inter-task Communication

mechanism which allows processes to


communicate with each other and
synchronize their actions.
Why ITC?
Information sharing

Computation speedup

Modularity

Convenience
Communication Model of ITC
Shared Memory Vs Message Passing
1. Shared Memory
• Ex: Producer Consumer Problem

unbounded-buffer
bounded-buffer
2. Message Passing Method

• processes communicate with each other without resorting to shared variables

• IPC facility provides two operations:

send(message)

receive(message)

• processes P and Q wish to communicate, they need to:

Establish a communication link between them

Exchange messages via send/receive


Implementation of communication link in
Message Passing System

• Physical:
Shared memory
Hardware bus
Network
• Logical:
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
(I) Direct Communication

• Processes must name each other explicitly:


send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
• Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Direct Communication - Example

Example: Print Server


(II) Indirect Communication

• Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
• Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Indirect Communication - Example

Example: Mailbox
(iii) Synchronous Vs Asynchronous

► Message passing may be either blocking or non-blocking


► Blocking is considered synchronous
Blocking send -- the sender is blocked until the message is received
Blocking receive -- the receiver is blocked until a message is available
► Non-blocking is considered asynchronous
Non-blocking send -- the sender sends the message and continue
Non-blocking receive -- the receiver receives:
A valid message, or
Null message
iv Buffering
► Queue of messages attached to the link.

► implemented in one of three ways

1. Zero capacity – no messages are queued on a link. Sender must wait for receiver (rendezvous)

2. Bounded capacity – finite length of n messages Sender must wait if link full

3. Unbounded capacity – infinite length Sender never waits

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