SlideShare a Scribd company logo
1
Concurrency: Mutual Exclusion
and Synchronization
Chapter 4
2
• it is the ability to execute more than one program or
task simultaneously:
• Multi-core processors and parallel processing, allows multiple
processes and threads to be executed simultaneously.
• Multiple processes and threads can access the same memory
space, the same declared variable in code, or even read or
write to the same file.
• For example, multitasking on a single-core machine.
• Parallelism:
• Parallelism is when tasks run at the same time, e.g., on a
multicore processor.
Concurrency in Operating System
Problems in Concurrency
• There are various problems in concurrency. Some of them are as follows:
1. Locating the programming errors
It's difficult to spot a programming error because reports are usually repeatable
due to the varying states of shared components each time the code is executed.
2. Sharing Global Resources
Sharing global resources is difficult. If two processes utilize a global variable and
both alter the variable's value, the order in which the many changes are executed
is critical.
3. Locking the channel
• It could be inefficient for the OS to lock the resource and prevent other
processes from using it.
4. Optimal Allocation of Resources
3
4
Concurrency
5
Difficulties of Concurrency
• Sharing of global resources
• Operating system managing the
allocation of resources optimally
• Difficult to locate programming errors
• Communication among processes
• Sharing resources
• Synchronization of multiple processes
• Allocation of processor time
Critical Section Problem in
OS
6
7
Concurrency
• Multiple applications
– Multiprogramming
• Structured application
– Application can be a set of concurrent
processes
• Operating-system structure
– Operating system is a set of processes or
threads
8
A Simple Example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
9
A Simple Example
Process P1 Process P2
. .
chin = getchar(); .
. chin = getchar();
chout = chin; chout = chin;
putchar(chout); .
. putchar(chout);
. .
10
Operating System Concerns
• Keep track of various processes
• Allocate and deallocate resources
– Processor time
– Memory
– Files
– I/O devices
• Protect data and resources
• Output of process must be independent of the
speed of execution of other concurrent
processes
11
Process Interaction
• Processes unaware of each other
• Processes indirectly aware of each other
• Process directly aware of each other
12
13
Competition Among Processes
for Resources
• Mutual Exclusion
– Critical sections
• Only one program at a time is allowed in its
critical section
• Example only one process at a time is allowed
to send command to the printer
• Deadlock
• Starvation
14
Requirements for Mutual
Exclusion
• Only one process at a time is allowed in
the critical section for a resource
• A process that halts in its noncritical
section must do so without interfering
with other processes
• No deadlock or starvation
15
Requirements for Mutual
Exclusion
• A process must not be delayed access to
a critical section when there is no other
process using it
• No assumptions are made about relative
process speeds or number of processes
• A process remains inside its critical
section for a finite time only
16
Mutual Exclusion:
Hardware Support
• 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
17
Mutual Exclusion:
Hardware Support
• Special Machine Instructions
– Performed in a single instruction cycle
– Access to the memory location is blocked
for any other instructions
18
Mutual Exclusion:
Hardware Support
• Test and Set Instruction
boolean testset (int i) {
if (i == 0) {
i = 1;
return true;
}
else {
return false;
}
}
19
Mutual Exclusion:
Hardware Support
• Exchange Instruction
void exchange(int register,
int memory) {
int temp;
temp = memory;
memory = register;
register = temp;
}
20
Mutual Exclusion
21
Mutual Exclusion Machine
Instructions
• Advantages
– Applicable to any number of processes on
either a single processor or multiple
processors sharing main memory
– It is simple and therefore easy to verify
– It can be used to support multiple critical
sections
22
Mutual Exclusion Machine
Instructions
• Disadvantages
– Busy-waiting consumes processor time
– Starvation is possible when a process leaves
a critical section and more than one process
is waiting.
– Deadlock
• If a low priority process has the critical region
and a higher priority process needs, the higher
priority process will obtain the processor to wait
for the critical region
23
Semaphores
• Special variable called a semaphore is
used for signaling
• If a process is waiting for a signal, it is
suspended until that signal is sent.
24
Semaphores
• Semaphore is a variable that has an
integer value
– May be initialized to a non negative number
– Wait operation decrements the semaphore
value
– Signal operation increments semaphore
value
25
Producer/Consumer Problem
• One or more producers are generating
data and placing these in a buffer
• A single consumer is taking items out of
the buffer one at time
• Only one producer or consumer may
access the buffer at any one time
26
Monitors
• Monitor is a software module
• Chief characteristics
– Local data variables are accessible only by
the monitor
– Process enters monitor by invoking one of
its procedures
– Only one process may be executing in the
monitor at a time
27
28
Message Passing
• Enforce mutual exclusion
• Exchange information
send (destination, message)
receive (source, message)
29
Synchronization
• Sender and receiver may or may not be
blocking (waiting for message)
• Blocking send, blocking receive
– Both sender and receiver are blocked until
message is delivered
– Called a rendezvous
30
Synchronization
• Nonblocking send, blocking receive
– Sender continues on
– Receiver is blocked until the requested
message arrives
• Nonblocking send, nonblocking receive
– Neither party is required to wait
31
Addressing
• Direct addressing
– Send primitive includes a specific identifier
of the destination process
– Receive primitive could know ahead of time
which process a message is expected
– Receive primitive could use source
parameter to return a value when the
receive operation has been performed
32
Addressing
• Indirect addressing
– Messages are sent to a shared data structure
consisting of queues
– Queues are called mailboxes
– One process sends a message to the mailbox
and the other process picks up the message
from the mailbox
33
34
Message Format
35
Readers/Writers Problem
• Any number of readers may
simultaneously read the file
• Only one writer at a time may write to
the file
• If a writer is writing to the file, no reader
may read it

More Related Content

Similar to CHAP4.pptx

Lecture- 2_Process Management.pdf
Lecture- 2_Process Management.pdfLecture- 2_Process Management.pdf
Lecture- 2_Process Management.pdf
Harika Pudugosula
 
Lecture 5 inter process communication
Lecture 5 inter process communicationLecture 5 inter process communication
Lecture 5 inter process communication
Kumbirai Junior Muzavazi
 
Operating System
Operating SystemOperating System
Operating System
Hitesh Mohapatra
 
Processes and operating systems
Processes and operating systemsProcesses and operating systems
Processes and operating systems
RAMPRAKASHT1
 
Operating System Notes (1).pdf
Operating System Notes (1).pdfOperating System Notes (1).pdf
Operating System Notes (1).pdf
shriyashpatil7
 
Operating System Notes help for interview pripration
Operating System Notes  help for interview priprationOperating System Notes  help for interview pripration
Operating System Notes help for interview pripration
ajaybiradar99999
 
Operating System Notes.pdf
Operating System Notes.pdfOperating System Notes.pdf
Operating System Notes.pdf
AminaArshad42
 
Lecture5
Lecture5Lecture5
Lecture5
Ali Shah
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
UsamaKhan987353
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
Nazir Ahmed
 
6 multiprogramming & time sharing
6 multiprogramming & time sharing6 multiprogramming & time sharing
6 multiprogramming & time sharingmyrajendra
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - Processes
Peter Tröger
 
Evolution, Strutcture and Operations.pptx
Evolution, Strutcture and Operations.pptxEvolution, Strutcture and Operations.pptx
Evolution, Strutcture and Operations.pptx
ssuser000e54
 
opearating system notes mumbai university.pptx
opearating system notes mumbai university.pptxopearating system notes mumbai university.pptx
opearating system notes mumbai university.pptx
ssuser3dfcef
 
Operating Systems PPT 1 (1).pdf
Operating Systems PPT 1 (1).pdfOperating Systems PPT 1 (1).pdf
Operating Systems PPT 1 (1).pdf
FahanaAbdulVahab
 
Engg-0505-IT-Operating-Systems-2nd-year.pdf
Engg-0505-IT-Operating-Systems-2nd-year.pdfEngg-0505-IT-Operating-Systems-2nd-year.pdf
Engg-0505-IT-Operating-Systems-2nd-year.pdf
nikhil287188
 

Similar to CHAP4.pptx (20)

Lecture- 2_Process Management.pdf
Lecture- 2_Process Management.pdfLecture- 2_Process Management.pdf
Lecture- 2_Process Management.pdf
 
Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
Os
OsOs
Os
 
Lecture 5 inter process communication
Lecture 5 inter process communicationLecture 5 inter process communication
Lecture 5 inter process communication
 
Operating System
Operating SystemOperating System
Operating System
 
Processes and operating systems
Processes and operating systemsProcesses and operating systems
Processes and operating systems
 
Operating System Notes (1).pdf
Operating System Notes (1).pdfOperating System Notes (1).pdf
Operating System Notes (1).pdf
 
Operating System Notes help for interview pripration
Operating System Notes  help for interview priprationOperating System Notes  help for interview pripration
Operating System Notes help for interview pripration
 
Operating System Notes.pdf
Operating System Notes.pdfOperating System Notes.pdf
Operating System Notes.pdf
 
OS_module2. .pptx
OS_module2.                          .pptxOS_module2.                          .pptx
OS_module2. .pptx
 
Lecture5
Lecture5Lecture5
Lecture5
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
 
6 multiprogramming & time sharing
6 multiprogramming & time sharing6 multiprogramming & time sharing
6 multiprogramming & time sharing
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - Processes
 
Evolution, Strutcture and Operations.pptx
Evolution, Strutcture and Operations.pptxEvolution, Strutcture and Operations.pptx
Evolution, Strutcture and Operations.pptx
 
opearating system notes mumbai university.pptx
opearating system notes mumbai university.pptxopearating system notes mumbai university.pptx
opearating system notes mumbai university.pptx
 
Operating Systems PPT 1 (1).pdf
Operating Systems PPT 1 (1).pdfOperating Systems PPT 1 (1).pdf
Operating Systems PPT 1 (1).pdf
 
Lect07
Lect07Lect07
Lect07
 
Engg-0505-IT-Operating-Systems-2nd-year.pdf
Engg-0505-IT-Operating-Systems-2nd-year.pdfEngg-0505-IT-Operating-Systems-2nd-year.pdf
Engg-0505-IT-Operating-Systems-2nd-year.pdf
 

More from ansariparveen06

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptx
ansariparveen06
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).ppt
ansariparveen06
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.ppt
ansariparveen06
 
kmap.pptx
kmap.pptxkmap.pptx
kmap.pptx
ansariparveen06
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptx
ansariparveen06
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptx
ansariparveen06
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
ansariparveen06
 
dsa.ppt
dsa.pptdsa.ppt
11-IOManagement.ppt
11-IOManagement.ppt11-IOManagement.ppt
11-IOManagement.ppt
ansariparveen06
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.ppt
ansariparveen06
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptx
ansariparveen06
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx
ansariparveen06
 
green IT cooling.pptx
green IT cooling.pptxgreen IT cooling.pptx
green IT cooling.pptx
ansariparveen06
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
ansariparveen06
 
chp9 green IT.pptx
chp9 green IT.pptxchp9 green IT.pptx
chp9 green IT.pptx
ansariparveen06
 
regex.ppt
regex.pptregex.ppt
regex.ppt
ansariparveen06
 
BOM.ppt
BOM.pptBOM.ppt
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptx
ansariparveen06
 

More from ansariparveen06 (20)

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptx
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).ppt
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.ppt
 
pscheduling.ppt
pscheduling.pptpscheduling.ppt
pscheduling.ppt
 
kmap.pptx
kmap.pptxkmap.pptx
kmap.pptx
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptx
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptx
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
11-IOManagement.ppt
11-IOManagement.ppt11-IOManagement.ppt
11-IOManagement.ppt
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.ppt
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptx
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx
 
green IT cooling.pptx
green IT cooling.pptxgreen IT cooling.pptx
green IT cooling.pptx
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
 
chp9 green IT.pptx
chp9 green IT.pptxchp9 green IT.pptx
chp9 green IT.pptx
 
regex.ppt
regex.pptregex.ppt
regex.ppt
 
BOM.ppt
BOM.pptBOM.ppt
BOM.ppt
 
Cooling.pptx
Cooling.pptxCooling.pptx
Cooling.pptx
 
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptx
 

Recently uploaded

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

CHAP4.pptx

  • 1. 1 Concurrency: Mutual Exclusion and Synchronization Chapter 4
  • 2. 2 • it is the ability to execute more than one program or task simultaneously: • Multi-core processors and parallel processing, allows multiple processes and threads to be executed simultaneously. • Multiple processes and threads can access the same memory space, the same declared variable in code, or even read or write to the same file. • For example, multitasking on a single-core machine. • Parallelism: • Parallelism is when tasks run at the same time, e.g., on a multicore processor. Concurrency in Operating System
  • 3. Problems in Concurrency • There are various problems in concurrency. Some of them are as follows: 1. Locating the programming errors It's difficult to spot a programming error because reports are usually repeatable due to the varying states of shared components each time the code is executed. 2. Sharing Global Resources Sharing global resources is difficult. If two processes utilize a global variable and both alter the variable's value, the order in which the many changes are executed is critical. 3. Locking the channel • It could be inefficient for the OS to lock the resource and prevent other processes from using it. 4. Optimal Allocation of Resources 3
  • 5. 5 Difficulties of Concurrency • Sharing of global resources • Operating system managing the allocation of resources optimally • Difficult to locate programming errors • Communication among processes • Sharing resources • Synchronization of multiple processes • Allocation of processor time
  • 7. 7 Concurrency • Multiple applications – Multiprogramming • Structured application – Application can be a set of concurrent processes • Operating-system structure – Operating system is a set of processes or threads
  • 8. 8 A Simple Example void echo() { chin = getchar(); chout = chin; putchar(chout); }
  • 9. 9 A Simple Example Process P1 Process P2 . . chin = getchar(); . . chin = getchar(); chout = chin; chout = chin; putchar(chout); . . putchar(chout); . .
  • 10. 10 Operating System Concerns • Keep track of various processes • Allocate and deallocate resources – Processor time – Memory – Files – I/O devices • Protect data and resources • Output of process must be independent of the speed of execution of other concurrent processes
  • 11. 11 Process Interaction • Processes unaware of each other • Processes indirectly aware of each other • Process directly aware of each other
  • 12. 12
  • 13. 13 Competition Among Processes for Resources • Mutual Exclusion – Critical sections • Only one program at a time is allowed in its critical section • Example only one process at a time is allowed to send command to the printer • Deadlock • Starvation
  • 14. 14 Requirements for Mutual Exclusion • Only one process at a time is allowed in the critical section for a resource • A process that halts in its noncritical section must do so without interfering with other processes • No deadlock or starvation
  • 15. 15 Requirements for Mutual Exclusion • A process must not be delayed access to a critical section when there is no other process using it • No assumptions are made about relative process speeds or number of processes • A process remains inside its critical section for a finite time only
  • 16. 16 Mutual Exclusion: Hardware Support • 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
  • 17. 17 Mutual Exclusion: Hardware Support • Special Machine Instructions – Performed in a single instruction cycle – Access to the memory location is blocked for any other instructions
  • 18. 18 Mutual Exclusion: Hardware Support • Test and Set Instruction boolean testset (int i) { if (i == 0) { i = 1; return true; } else { return false; } }
  • 19. 19 Mutual Exclusion: Hardware Support • Exchange Instruction void exchange(int register, int memory) { int temp; temp = memory; memory = register; register = temp; }
  • 21. 21 Mutual Exclusion Machine Instructions • Advantages – Applicable to any number of processes on either a single processor or multiple processors sharing main memory – It is simple and therefore easy to verify – It can be used to support multiple critical sections
  • 22. 22 Mutual Exclusion Machine Instructions • Disadvantages – Busy-waiting consumes processor time – Starvation is possible when a process leaves a critical section and more than one process is waiting. – Deadlock • If a low priority process has the critical region and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region
  • 23. 23 Semaphores • Special variable called a semaphore is used for signaling • If a process is waiting for a signal, it is suspended until that signal is sent.
  • 24. 24 Semaphores • Semaphore is a variable that has an integer value – May be initialized to a non negative number – Wait operation decrements the semaphore value – Signal operation increments semaphore value
  • 25. 25 Producer/Consumer Problem • One or more producers are generating data and placing these in a buffer • A single consumer is taking items out of the buffer one at time • Only one producer or consumer may access the buffer at any one time
  • 26. 26 Monitors • Monitor is a software module • Chief characteristics – Local data variables are accessible only by the monitor – Process enters monitor by invoking one of its procedures – Only one process may be executing in the monitor at a time
  • 27. 27
  • 28. 28 Message Passing • Enforce mutual exclusion • Exchange information send (destination, message) receive (source, message)
  • 29. 29 Synchronization • Sender and receiver may or may not be blocking (waiting for message) • Blocking send, blocking receive – Both sender and receiver are blocked until message is delivered – Called a rendezvous
  • 30. 30 Synchronization • Nonblocking send, blocking receive – Sender continues on – Receiver is blocked until the requested message arrives • Nonblocking send, nonblocking receive – Neither party is required to wait
  • 31. 31 Addressing • Direct addressing – Send primitive includes a specific identifier of the destination process – Receive primitive could know ahead of time which process a message is expected – Receive primitive could use source parameter to return a value when the receive operation has been performed
  • 32. 32 Addressing • Indirect addressing – Messages are sent to a shared data structure consisting of queues – Queues are called mailboxes – One process sends a message to the mailbox and the other process picks up the message from the mailbox
  • 33. 33
  • 35. 35 Readers/Writers Problem • Any number of readers may simultaneously read the file • Only one writer at a time may write to the file • If a writer is writing to the file, no reader may read it