SlideShare a Scribd company logo
1 of 19
OPERATING SYSTEM
Classical problems of Synchronization
Dr. Gopika S ,
Department of CS, KJC
Classical problems of Synchronization
โ€ข There are a number of classical problems of synchronization as examples of a
large class of concurrency-control problems. semaphores are used as solution for
synchronization, since that is the traditional way to present such solutions.
โ€ข However, actual implementations of these solutions could use mutex locks in
place of binary semaphores.
โ€ข These problems are used for testing nearly every newly proposed synchronization
scheme. The following problems of synchronization are considered as classical
problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
Bounded-buffer (or Producer-Consumer) Problem:
โ€ข Bounded Buffer problem is also called producer consumer problem. This problem
is generalized in terms of the Producer-Consumer problem.
โ€ข There is one Producer in the producer-consumer problem, Producer is producing
some items, whereas there is one Consumer that is consuming the items
produced by the Producer. The same memory buffer is shared by both producers
and consumers which is of fixed-size.
โ€ข The task of the Producer is to produce the item, put it into the memory buffer,
and again start producing items. Whereas the task of the Consumer is to
consume the item from the memory buffer.
โ€ข 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.
Producers produce a product and consumers consume the product, but both use
of one of the containers each time.
Producer Code
Consumer Code
โ€ข The producer should produce data only when the buffer is not full. In case it is
found that the buffer is full, the producer is not allowed to store any data into the
memory buffer.
โ€ข Data can only be consumed by the consumer if and only if the memory buffer is
not empty. In case it is found that the buffer is empty, the consumer is not
allowed to use any data from the memory buffer.
โ€ข Accessing memory buffer should not be allowed to producer and consumer at the
same time.
โ€ข If the producer is attempting to add an item at the same moment the
consumer is removing one, the outcome depends on whether the
producerโ€™s check for available space happens before or after the
consumer decrements the counter.
โ€ข A similar race condition arises if the queue is empty and both
functions are called. This race condition could be fixed by wrapping
the accesses to the counter with a mutex.
โ€ข Dining-Philosophers Problem:
โ€ข The Dining Philosopher Problem states that K philosophers seated
around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A
philosopher may eat if he can pickup the two chopsticks adjacent to
him. One chopstick may be picked up by any one of its adjacent
followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-
free manner.
Readers-writers problem
Consider a situation where we have a file shared between many people.
If one of the person tries editing the file, no other person should be reading or
writing at the same time, otherwise changes will not be visible to him/her.
However if some person is reading the file, then others may read it at the same
time.
Precisely in OS we call this situation as the readers-writers problem
โ€ข Problem parameters:
โ€ข One set of data is shared among a number of processes
โ€ข Once a writer is ready, it performs its write. Only one writer may write at a time
โ€ข If a process is writing, no other process can read it
โ€ข If at least one reader is reading, no other process can write
โ€ข Readers may not write and only read
Solution
Case Process 1 Process 2
Allowed/Not
Allowed
Case 1 Writing Writing Not Allowed
Case 2 Writing Reading Not Allowed
Case 3 Reading Writing Not Allowed
Case 4 Reading Reading Allowed
There are four Types of cases could happen here.
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
Writer process:
โ€ข Writer requests the entry to critical section.
โ€ข If allowed i.e. wait() gives a true value, it enters and performs
the write. If not allowed, it keeps on waiting.
โ€ข It exits the critical section.
Reader process:
1.Reader requests the entry to critical section.
2.If allowed:
1. it increments the count of number of readers inside the critical
section. If this reader is the first reader entering, it locks the wrt
semaphore to restrict the entry of writers if any reader is inside.
2. It then, signals mutex as any other reader is allowed to enter
while others are already reading.
3. After performing reading, it exits the critical section. When
exiting, it checks if no more reader is inside, it signals the
semaphore โ€œwrtโ€ as now, writer can enter the critical section.
3.If not allowed, it keeps on waiting.
The Dining Philosopher Problem โ€“
โ€ข The Dining Philosopher Problem states that K philosophers seated around a circular table
with one chopstick between each pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One
chopstick may be picked up by any one of its adjacent followers but not both.
Semaphore Solution to Dining Philosopher โ€“
Each philosopher is represented by the following pseudocode:
process P[i]
while true do
{
THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
There are three states of the philosopher: THINKING, HUNGRY, and EATING.
The dining philosopher demonstrates a large class of
concurrency control problems hence it's a classic
synchronization problem.
1. Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing
this it holds C0 chopstick and reduces semaphore C0 to 0, after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this
it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore C1 to 0
2. Similarly, suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute Wait(
take_chopstickC[i] ); by doing this it will try to hold C1 chopstick but will not be able to do that, since the value of semaphore
C1 has already been set to 0 by philosopher P0, therefore it will enter into an infinite loop because of which philosopher P1 will
not be able to pick chopstick C1 whereas if Philosopher P2 wants to eat, it will enter in Philosopher() function, and execute
Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and reduces semaphore C2 to 0, after that, it executes Wait(
take_chopstickC[(i+1) % 5] ); by doing this it holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore C3
to 0.
โ€ข The drawback of the above solution of the dining philosopher
problem
โ€ข From the above solution of the dining philosopher problem, we have
proved that no two neighboring philosophers can eat at the same
point in time. The drawback of the above solution is that this solution
can lead to a deadlock condition. This situation happens if all the
philosophers pick their left chopstick at the same time, which leads to
the condition of deadlock and none of the philosophers can eat.
What is Deadlock in Operating System (OS)?
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
The process requests for some resource.
OS grant the resource if it is available otherwise let the process waits.
The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a resource
which is being assigned to some another process. In this situation, none of the
process gets executed since the resource it needs, is held by some other process
which is also waiting for some other resource to be released.
Let us assume that there are three processes P1,
P2 and P3. There are three different resources R1,
R2 and R3. R1 is assigned to P1, R2 is assigned to
P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is being used by P2. P1
halts its execution since it can't complete without R2. P2 also
demands for R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3. P3 also demands for
R1 which is being used by P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among
the three processes.
None of the process is progressing and they are
all waiting.
The computer becomes unresponsive since all
the processes got blocked.
Difference between Starvation and Deadlock
Sr. Deadlock Starvation
1
Deadlock is a situation where no process
proceeds
Starvation is a situation where the low priority
process got blocked and the high priority
processes proceed.
2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.
3 Every Deadlock is always a starvation. Every starvation need not be deadlock.
4
The requested resource is blocked by the
other process.
The requested resource is continuously be used
by the higher priority processes.
5
Deadlock happens when Mutual exclusion,
hold and wait, No preemption and circular
wait occurs simultaneously.
It occurs due to the uncontrolled priority and
resource management.
Necessary conditions for Deadlocks
Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use
the same resource at the same time.
Hold and Wait
A process waits for some resources while holding another resource at the same time.
No preemption
The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.
Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.

More Related Content

Similar to Deadlock _Classic problems.pptx

12 deadlock concept
12 deadlock concept12 deadlock concept
12 deadlock conceptDeepaMaria10489
ย 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronizationKlintonChhun
ย 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating systemMuhammad Baqar Kazmi
ย 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationVaibhav Khanna
ย 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's ProblemYash Mittal
ย 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronizationsangrampatil81
ย 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system anushkashastri
ย 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
ย 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
ย 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
ย 
Operating System Process Synchronization
Operating System Process SynchronizationOperating System Process Synchronization
Operating System Process SynchronizationHaziq Naeem
ย 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationlodhran-hayat
ย 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptxUmesh Hengaju
ย 
4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptxDishaDwivedi2
ย 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptxAbuBakkarShayan
ย 

Similar to Deadlock _Classic problems.pptx (20)

12 deadlock concept
12 deadlock concept12 deadlock concept
12 deadlock concept
ย 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronization
ย 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
ย 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
ย 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's Problem
ย 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronization
ย 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system
ย 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
ย 
Operating systems
Operating systemsOperating systems
Operating systems
ย 
Operating systems
Operating systemsOperating systems
Operating systems
ย 
Operating System Process Synchronization
Operating System Process SynchronizationOperating System Process Synchronization
Operating System Process Synchronization
ย 
Process coordination
Process coordinationProcess coordination
Process coordination
ย 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
ย 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
ย 
Synchronization problems
Synchronization problemsSynchronization problems
Synchronization problems
ย 
Chapter06.ppt
Chapter06.pptChapter06.ppt
Chapter06.ppt
ย 
Adsa u4 ver 1.0
Adsa u4 ver 1.0Adsa u4 ver 1.0
Adsa u4 ver 1.0
ย 
4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx
ย 
Synchronization
SynchronizationSynchronization
Synchronization
ย 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptx
ย 

More from GopikaS12

Introduction to Tree .pptx
Introduction to Tree .pptxIntroduction to Tree .pptx
Introduction to Tree .pptxGopikaS12
ย 
C Programming language - introduction
C Programming  language - introduction  C Programming  language - introduction
C Programming language - introduction GopikaS12
ย 
C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructsGopikaS12
ย 
Heap creation from elements - Step by step Approach
Heap creation  from elements - Step by step Approach Heap creation  from elements - Step by step Approach
Heap creation from elements - Step by step Approach GopikaS12
ย 
Data Structures: Stack Operations
Data Structures:    Stack OperationsData Structures:    Stack Operations
Data Structures: Stack OperationsGopikaS12
ย 
Net content in computer architecture
Net content in computer  architectureNet content in computer  architecture
Net content in computer architectureGopikaS12
ย 
It unit 1
It   unit 1It   unit 1
It unit 1GopikaS12
ย 

More from GopikaS12 (7)

Introduction to Tree .pptx
Introduction to Tree .pptxIntroduction to Tree .pptx
Introduction to Tree .pptx
ย 
C Programming language - introduction
C Programming  language - introduction  C Programming  language - introduction
C Programming language - introduction
ย 
C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructs
ย 
Heap creation from elements - Step by step Approach
Heap creation  from elements - Step by step Approach Heap creation  from elements - Step by step Approach
Heap creation from elements - Step by step Approach
ย 
Data Structures: Stack Operations
Data Structures:    Stack OperationsData Structures:    Stack Operations
Data Structures: Stack Operations
ย 
Net content in computer architecture
Net content in computer  architectureNet content in computer  architecture
Net content in computer architecture
ย 
It unit 1
It   unit 1It   unit 1
It unit 1
ย 

Recently uploaded

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
ย 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Standamitlee9823
ย 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
ย 

Deadlock _Classic problems.pptx

  • 1. OPERATING SYSTEM Classical problems of Synchronization Dr. Gopika S , Department of CS, KJC
  • 2. Classical problems of Synchronization โ€ข There are a number of classical problems of synchronization as examples of a large class of concurrency-control problems. semaphores are used as solution for synchronization, since that is the traditional way to present such solutions. โ€ข However, actual implementations of these solutions could use mutex locks in place of binary semaphores. โ€ข These problems are used for testing nearly every newly proposed synchronization scheme. The following problems of synchronization are considered as classical problems: 1. Bounded-buffer (or Producer-Consumer) Problem, 2. Dining-Philosophers Problem, 3. Readers and Writers Problem,
  • 3. Bounded-buffer (or Producer-Consumer) Problem: โ€ข Bounded Buffer problem is also called producer consumer problem. This problem is generalized in terms of the Producer-Consumer problem. โ€ข There is one Producer in the producer-consumer problem, Producer is producing some items, whereas there is one Consumer that is consuming the items produced by the Producer. The same memory buffer is shared by both producers and consumers which is of fixed-size. โ€ข The task of the Producer is to produce the item, put it into the memory buffer, and again start producing items. Whereas the task of the Consumer is to consume the item from the memory buffer. โ€ข 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. Producers produce a product and consumers consume the product, but both use of one of the containers each time.
  • 5. โ€ข The producer should produce data only when the buffer is not full. In case it is found that the buffer is full, the producer is not allowed to store any data into the memory buffer. โ€ข Data can only be consumed by the consumer if and only if the memory buffer is not empty. In case it is found that the buffer is empty, the consumer is not allowed to use any data from the memory buffer. โ€ข Accessing memory buffer should not be allowed to producer and consumer at the same time.
  • 6. โ€ข If the producer is attempting to add an item at the same moment the consumer is removing one, the outcome depends on whether the producerโ€™s check for available space happens before or after the consumer decrements the counter. โ€ข A similar race condition arises if the queue is empty and both functions are called. This race condition could be fixed by wrapping the accesses to the counter with a mutex.
  • 7. โ€ข Dining-Philosophers Problem: โ€ข The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both. This problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation- free manner.
  • 8. Readers-writers problem Consider a situation where we have a file shared between many people. If one of the person tries editing the file, no other person should be reading or writing at the same time, otherwise changes will not be visible to him/her. However if some person is reading the file, then others may read it at the same time. Precisely in OS we call this situation as the readers-writers problem โ€ข Problem parameters: โ€ข One set of data is shared among a number of processes โ€ข Once a writer is ready, it performs its write. Only one writer may write at a time โ€ข If a process is writing, no other process can read it โ€ข If at least one reader is reading, no other process can write โ€ข Readers may not write and only read
  • 9. Solution Case Process 1 Process 2 Allowed/Not Allowed Case 1 Writing Writing Not Allowed Case 2 Writing Reading Not Allowed Case 3 Reading Writing Not Allowed Case 4 Reading Reading Allowed There are four Types of cases could happen here. Here priority means, no reader should wait if the share is currently opened for reading. Three variables are used: mutex, wrt, readcnt to implement solution
  • 10. Writer process: โ€ข Writer requests the entry to critical section. โ€ข If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it keeps on waiting. โ€ข It exits the critical section. Reader process: 1.Reader requests the entry to critical section. 2.If allowed: 1. it increments the count of number of readers inside the critical section. If this reader is the first reader entering, it locks the wrt semaphore to restrict the entry of writers if any reader is inside. 2. It then, signals mutex as any other reader is allowed to enter while others are already reading. 3. After performing reading, it exits the critical section. When exiting, it checks if no more reader is inside, it signals the semaphore โ€œwrtโ€ as now, writer can enter the critical section. 3.If not allowed, it keeps on waiting.
  • 11. The Dining Philosopher Problem โ€“ โ€ข The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both.
  • 12. Semaphore Solution to Dining Philosopher โ€“ Each philosopher is represented by the following pseudocode: process P[i] while true do { THINK; PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]); EAT; PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]) } There are three states of the philosopher: THINKING, HUNGRY, and EATING. The dining philosopher demonstrates a large class of concurrency control problems hence it's a classic synchronization problem.
  • 13. 1. Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it holds C0 chopstick and reduces semaphore C0 to 0, after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore C1 to 0 2. Similarly, suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it will try to hold C1 chopstick but will not be able to do that, since the value of semaphore C1 has already been set to 0 by philosopher P0, therefore it will enter into an infinite loop because of which philosopher P1 will not be able to pick chopstick C1 whereas if Philosopher P2 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and reduces semaphore C2 to 0, after that, it executes Wait( take_chopstickC[(i+1) % 5] ); by doing this it holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore C3 to 0.
  • 14. โ€ข The drawback of the above solution of the dining philosopher problem โ€ข From the above solution of the dining philosopher problem, we have proved that no two neighboring philosophers can eat at the same point in time. The drawback of the above solution is that this solution can lead to a deadlock condition. This situation happens if all the philosophers pick their left chopstick at the same time, which leads to the condition of deadlock and none of the philosophers can eat.
  • 15.
  • 16. What is Deadlock in Operating System (OS)? Every process needs some resources to complete its execution. However, the resource is granted in a sequential order. The process requests for some resource. OS grant the resource if it is available otherwise let the process waits. The process uses it and release on the completion. A Deadlock is a situation where each of the computer process waits for a resource which is being assigned to some another process. In this situation, none of the process gets executed since the resource it needs, is held by some other process which is also waiting for some other resource to be released.
  • 17. Let us assume that there are three processes P1, P2 and P3. There are three different resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3. After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since it can't complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its execution because it can't continue without R3. P3 also demands for R1 which is being used by P1 therefore P3 also stops its execution. In this scenario, a cycle is being formed among the three processes. None of the process is progressing and they are all waiting. The computer becomes unresponsive since all the processes got blocked.
  • 18. Difference between Starvation and Deadlock Sr. Deadlock Starvation 1 Deadlock is a situation where no process proceeds Starvation is a situation where the low priority process got blocked and the high priority processes proceed. 2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite. 3 Every Deadlock is always a starvation. Every starvation need not be deadlock. 4 The requested resource is blocked by the other process. The requested resource is continuously be used by the higher priority processes. 5 Deadlock happens when Mutual exclusion, hold and wait, No preemption and circular wait occurs simultaneously. It occurs due to the uncontrolled priority and resource management.
  • 19. Necessary conditions for Deadlocks Mutual Exclusion A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use the same resource at the same time. Hold and Wait A process waits for some resources while holding another resource at the same time. No preemption The process which once scheduled will be executed till the completion. No other process can be scheduled by the scheduler meanwhile. Circular Wait All the processes must be waiting for the resources in a cyclic manner so that the last process is waiting for the resource which is being held by the first process.