SlideShare a Scribd company logo
1 of 48
Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Chapter 5: Process
Synchronization
5.2 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Chapter 6: Process Scheduling
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
5.3 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Objectives
To introduce the critical-section problem, whose solutions can be
used to ensure the consistency of shared data
To present both software and hardware solutions of the critical-
section problem
To examine several classical process-synchronization problems
To explore several tools that are used to solve process
synchronization problems
5.4 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Background
Processes can execute concurrently
May be interrupted at any time, partially completing
execution
Concurrent access to shared data may result in data
inconsistency
Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes
5.5 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Interleaving and Overlapping Processes
processes may be interleaved on uniprocessors
5.6 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Interleaving and
Overlapping Processes
And not only interleaved but overlapped on
multi-processors
5.7 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Producer-Consumer Problem
in counter out
5.8 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Producer
while (true) {
/* produce an item in next produced */
while (counter == BUFFER SIZE)
; /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
counter++;
}
5.9 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
counter--;
/* consume the item in next consumed */
}
5.10 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Race Condition
A race condition occurs when
Multiple processes or threads read and write data items
They do so in a way where the final result depends on
the order of execution of the processes.
The output depends on who finishes the race last.
5.11 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Race Condition: Example
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 – 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 }
S5: consumer execute counter = register2 {counter = 4}
5.12 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Enforce Single Access
If we enforce a rule that only one process may enter the function at a time
then:
P1 & P2 run on separate processors
P1 enters echo first,
P2 tries to enter but is blocked – P2 suspends
P1 completes execution
P2 resumes and executes echo
5.13 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Competition among
Processes for Resources
Three main control problems:
Need for Mutual Exclusion
Critical sections
Deadlock
Starvation
5.14 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating table, writing
file, etc
When one process in critical section, no other may be in its critical
section
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in entry
section, may follow critical section with exit section, then remainder
section
5.15 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Critical Section
General structure of process pi is
5.16 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
16
Critical Regions (2)
Mutual exclusion using critical regions
5.17 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical section, then no
other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there exist some
processes that wish to enter their critical section, then the selection of the
processes that will enter the critical section next cannot be postponed
indefinitely
3. Bounded Waiting - A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has made
a request to enter its critical section and before that request is granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the n processes
Two approaches depending on if kernel is preemptive or non-preemptive
Preemptive – allows preemption of process when running in kernel mode
Non-preemptive – runs until exits kernel mode, blocks, or voluntarily
yields CPU
Essentially free of race conditions in kernel mode
5.18 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
18
Mutual Exclusion with Busy Waiting
Proposed solution to critical region problem
(a) Process 0. (b) Process 1.
5.19 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Peterson’s Solution
Good algorithmic description of solving the problem
Two process solution
Assume that the load and store instructions are atomic; that is, cannot be
interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
The variable turn indicates whose turn it is to enter the critical section
The flag array is used to indicate if a process is ready to enter the critical
section. flag[i] = true implies that process Pi is ready!
5.20 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = false;
remainder section
} while (true);
Provable that
1. Mutual exclusion is preserved
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
Algorithm for Process Pi
5.21 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Synchronization Hardware
Many systems provide hardware support for critical section code
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
 Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
 Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
5.22 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
Solution to Critical-section Problem Using Locks
5.23 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
23
Test & Set Lock Register
Entering and leaving a critical region using the
TSL instruction
5.24 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
test_and_set Instruction
Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
5.25 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Solution using test_and_set()
Shared boolean variable lock, initialized to FALSE
Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
5.27 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Mutex Locks
Previous solutions are complicated and generally inaccessible to
application programmers
OS designers build software tools to solve critical section problem
Simplest is mutex lock
Product critical regions with it by first acquire() a lock then
release() it
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Usually implemented via hardware atomic instructions
But this solution requires busy waiting
This lock therefore called a spinlock
5.28 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
acquire() and release()
acquire() {
while (!available)
; /* busy wait */
available = false;;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
5.29 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Semaphore
Synchronization tool that does not require busy waiting
Semaphore S – integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Less complicated
Can only be accessed via two indivisible (atomic) operations
}
5.30 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Semaphore Primitives
DOWN
UP
5.31 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Semaphore Usage
Counting semaphore – integer value can range over an unrestricted domain
Binary semaphore – integer value can range only between 0 and 1
Then a mutex lock
Can implement a counting semaphore S as a binary semaphore
Can solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
5.32 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal ()
on the same semaphore at the same time
Thus, implementation becomes the critical section problem where the wait and
signal code are placed in the critical section
Could now have busy waiting in critical section implementation
 But implementation code is short
 Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore
this is not a good solution
5.33 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Binary Semaphore Primitives
5.34 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Mutual Exclusion Using Semaphores
5.35 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely for an event that
can be caused by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
. .
signal(S); signal(Q);
signal(Q); signal(S);
Starvation – indefinite blocking
A process may never be removed from the semaphore queue in which it is
suspended
Priority Inversion – Scheduling problem when lower-priority process holds a
lock needed by higher-priority process
Solved via priority-inheritance protocol
5.36 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Classical Problems of Synchronization
Classical problems used to test newly-proposed synchronization schemes
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
5.37 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
37
The producer-consumer problem
5.38 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Dining-Philosophers Problem
Philosophers spend their lives thinking and eating
Don’t interact with their neighbors, occasionally try to pick up 2
chopsticks (one at a time) to eat from bowl
Need both to eat, then release both when done
In the case of 5 philosophers
Shared data
 Bowl of rice (data set)
 Semaphore chopstick [5] initialized to 1
5.39 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
39
Dining Philosophers (2)
A non-solution to the dining philosophers problem
5.40 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
40
Dining Philosophers (3)
5.41 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
41
Dining Philosophers (4)
5.42 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
42
The Sleeping Barber Problem (1)
5.43 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
#define CHAIRS 5 /* # chairs for waiting customers */
typedef int semaphore; /* use your imagination */
semaphore customers = 0; /* # of customers waiting for service */
semaphore barbers = 0; /* # of barbers waiting for customers */
semaphore mutex = 1; /* for mutual exclusion */
int waiting = 0; /* customer are waiting (not being cut) */
void barber(void)
{
while (TRUE) {
down(&customers); /* go to sleep if # of customers is 0 */
down(&mutex); /* acquire access to "waiting' */
waiting = waiting - 1; /* decrement count of waiting customers */
up(&barbers); /* one barber is now read y to cut hair */
up(&mutex); /* release 'waiting' */
cut_hair(); /* cut hair (outside critical region */
}
}
void customer(void)
{
down(&mutex); /* enter critical region */
if (waiting < CH AIRS) { /* if there are no free chairs, leave */
waiting = waiting + 1; /* increment count of waiting customers */
up(&customers); /* wake up barber if necessary */
up(&mutex); /* release access to 'waiting' */
down(&barbers); /* go to sleep if # of free barbers is 0 */
get_haircut(); /* be seated and be served */
} else {
up(&mutex); /* shop is full; do not wait */
}
}
5.44 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Monitors
A high-level abstraction that provides a convenient and effective mechanism for
process synchronization
Abstract data type, internal variables only accessible by code within the procedure
Only one process may be active within the monitor at a time
But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
procedure Pn (…) {……}
Initialization code (…) { … }
}
}
5.45 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Schematic view of a Monitor
5.46 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Condition Variables
condition x, y;
Two operations on a condition variable:
x.wait () – a process that invokes the operation is suspended until x.signal ()
x.signal () – resumes one of processes (if any) that invoked x.wait ()
 If no x.wait () on the variable, then it has no effect on the variable
5.47 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Monitor with Condition Variables
5.48 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Condition Variables Choices
If process P invokes x.signal (), with Q in x.wait () state, what should happen next?
If Q is resumed, then P must wait
Options include
Signal and wait – P waits until Q leaves monitor or waits for another condition
Signal and continue – Q waits until P leaves the monitor or waits for another condition
Both have pros and cons – language implementer can decide
Monitors implemented in Concurrent Pascal compromise
 P executing signal immediately leaves the monitor, Q is resumed
Implemented in other languages including Mesa, C#, Java
5.49 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads

More Related Content

What's hot

Chapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksChapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksGiulianoRanauro
 
Operating System 3
Operating System 3Operating System 3
Operating System 3tech2click
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronizationcscarcas
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemShafaan Khaliq Bhatti
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzGiulianoRanauro
 
Chapter 9 Operating Systems silberschatz
Chapter 9 Operating Systems silberschatzChapter 9 Operating Systems silberschatz
Chapter 9 Operating Systems silberschatzGiulianoRanauro
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptErenJeager20
 
5 process synchronization
5 process synchronization5 process synchronization
5 process synchronizationBaliThorat1
 
Silberschatz / OS Concepts
Silberschatz /  OS Concepts Silberschatz /  OS Concepts
Silberschatz / OS Concepts Alanisca Alanis
 
Unit II - 1 - Operating System Process
Unit II - 1 - Operating System ProcessUnit II - 1 - Operating System Process
Unit II - 1 - Operating System Processcscarcas
 
Evolution of operating system
Evolution of operating systemEvolution of operating system
Evolution of operating systemArshad khan
 
Slides For Operating System Concepts By Silberschatz Galvin And Gagne
Slides For Operating System Concepts By Silberschatz Galvin And GagneSlides For Operating System Concepts By Silberschatz Galvin And Gagne
Slides For Operating System Concepts By Silberschatz Galvin And Gagnesarankumar4445
 

What's hot (20)

Chapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksChapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocks
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
 
4 threads
4 threads4 threads
4 threads
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronization
 
Chapter 3: Processes
Chapter 3: ProcessesChapter 3: Processes
Chapter 3: Processes
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatz
 
Chapter 9 Operating Systems silberschatz
Chapter 9 Operating Systems silberschatzChapter 9 Operating Systems silberschatz
Chapter 9 Operating Systems silberschatz
 
Ch1-Operating System Concepts
Ch1-Operating System ConceptsCh1-Operating System Concepts
Ch1-Operating System Concepts
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.ppt
 
5 process synchronization
5 process synchronization5 process synchronization
5 process synchronization
 
Silberschatz / OS Concepts
Silberschatz /  OS Concepts Silberschatz /  OS Concepts
Silberschatz / OS Concepts
 
Unit II - 1 - Operating System Process
Unit II - 1 - Operating System ProcessUnit II - 1 - Operating System Process
Unit II - 1 - Operating System Process
 
Ch1-Operating System Concept
Ch1-Operating System ConceptCh1-Operating System Concept
Ch1-Operating System Concept
 
Chapter 3 - Processes
Chapter 3 - ProcessesChapter 3 - Processes
Chapter 3 - Processes
 
Operating System
Operating SystemOperating System
Operating System
 
Evolution of operating system
Evolution of operating systemEvolution of operating system
Evolution of operating system
 
operating system structure
operating system structureoperating system structure
operating system structure
 
Slides For Operating System Concepts By Silberschatz Galvin And Gagne
Slides For Operating System Concepts By Silberschatz Galvin And GagneSlides For Operating System Concepts By Silberschatz Galvin And Gagne
Slides For Operating System Concepts By Silberschatz Galvin And Gagne
 

Similar to Lecture#5

Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronizationbismahmalik22
 
ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].pptmonirJihad2
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptAbdikani34
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfsharada3
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating systemSami Mughal
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentBhartiRani14
 
process synchronisation operating system
process synchronisation operating systemprocess synchronisation operating system
process synchronisation operating systemcodefast
 
Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )HimanshuSharma1389
 
05-Process-Synchronization.pdf
05-Process-Synchronization.pdf05-Process-Synchronization.pdf
05-Process-Synchronization.pdfmilisjojo
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)dsuyal1
 
Process Synchronisation Operating System
Process Synchronisation Operating SystemProcess Synchronisation Operating System
Process Synchronisation Operating SystemDRAnithaSofiaLizCSES
 

Similar to Lecture#5 (20)

ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronization
 
ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].ppt
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio ppt
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdf
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating system
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu student
 
process synchronisation operating system
process synchronisation operating systemprocess synchronisation operating system
process synchronisation operating system
 
ch6.ppt
ch6.pptch6.ppt
ch6.ppt
 
ch6-1.ppt
ch6-1.pptch6-1.ppt
ch6-1.ppt
 
Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
PS.ppt
PS.pptPS.ppt
PS.ppt
 
chapter4.pptx
chapter4.pptxchapter4.pptx
chapter4.pptx
 
05-Process-Synchronization.pdf
05-Process-Synchronization.pdf05-Process-Synchronization.pdf
05-Process-Synchronization.pdf
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)
 
Process Synchronisation Operating System
Process Synchronisation Operating SystemProcess Synchronisation Operating System
Process Synchronisation Operating System
 

More from Adil Alpkoçak

Expert Systems vs Clinical Decision Support Systems
Expert Systems vs Clinical Decision Support SystemsExpert Systems vs Clinical Decision Support Systems
Expert Systems vs Clinical Decision Support SystemsAdil Alpkoçak
 
Fotografcilikta Minimalizm (iFOD)
Fotografcilikta Minimalizm (iFOD)Fotografcilikta Minimalizm (iFOD)
Fotografcilikta Minimalizm (iFOD)Adil Alpkoçak
 
Socialnetworkanalysis 100225055227-phpapp02
Socialnetworkanalysis 100225055227-phpapp02Socialnetworkanalysis 100225055227-phpapp02
Socialnetworkanalysis 100225055227-phpapp02Adil Alpkoçak
 
Fotoğrafçılıkta Minimalizm
Fotoğrafçılıkta MinimalizmFotoğrafçılıkta Minimalizm
Fotoğrafçılıkta MinimalizmAdil Alpkoçak
 

More from Adil Alpkoçak (7)

Expert Systems vs Clinical Decision Support Systems
Expert Systems vs Clinical Decision Support SystemsExpert Systems vs Clinical Decision Support Systems
Expert Systems vs Clinical Decision Support Systems
 
Fotografcilikta Minimalizm (iFOD)
Fotografcilikta Minimalizm (iFOD)Fotografcilikta Minimalizm (iFOD)
Fotografcilikta Minimalizm (iFOD)
 
Socialnetworkanalysis 100225055227-phpapp02
Socialnetworkanalysis 100225055227-phpapp02Socialnetworkanalysis 100225055227-phpapp02
Socialnetworkanalysis 100225055227-phpapp02
 
Lecture #2 xml
Lecture #2 xmlLecture #2 xml
Lecture #2 xml
 
Lecture#01
Lecture#01Lecture#01
Lecture#01
 
Mir lecture1
Mir lecture1Mir lecture1
Mir lecture1
 
Fotoğrafçılıkta Minimalizm
Fotoğrafçılıkta MinimalizmFotoğrafçılıkta Minimalizm
Fotoğrafçılıkta Minimalizm
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

Lecture#5

  • 1. Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Chapter 5: Process Synchronization
  • 2. 5.2 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Chapter 6: Process Scheduling Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Alternative Approaches
  • 3. 5.3 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To present both software and hardware solutions of the critical- section problem To examine several classical process-synchronization problems To explore several tools that are used to solve process synchronization problems
  • 4. 5.4 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Background Processes can execute concurrently May be interrupted at any time, partially completing execution Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes
  • 5. 5.5 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Interleaving and Overlapping Processes processes may be interleaved on uniprocessors
  • 6. 5.6 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Interleaving and Overlapping Processes And not only interleaved but overlapped on multi-processors
  • 7. 5.7 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Producer-Consumer Problem in counter out
  • 8. 5.8 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Producer while (true) { /* produce an item in next produced */ while (counter == BUFFER SIZE) ; /* do nothing */ buffer[in] = next produced; in = (in + 1) % BUFFER SIZE; counter++; }
  • 9. 5.9 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Consumer while (true) { while (counter == 0) ; /* do nothing */ next consumed = buffer[out]; out = (out + 1) % BUFFER SIZE; counter--; /* consume the item in next consumed */ }
  • 10. 5.10 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Race Condition A race condition occurs when Multiple processes or threads read and write data items They do so in a way where the final result depends on the order of execution of the processes. The output depends on who finishes the race last.
  • 11. 5.11 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Race Condition: Example counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2 Consider this execution interleaving with “count = 5” initially: S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 – 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4}
  • 12. 5.12 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Enforce Single Access If we enforce a rule that only one process may enter the function at a time then: P1 & P2 run on separate processors P1 enters echo first, P2 tries to enter but is blocked – P2 suspends P1 completes execution P2 resumes and executes echo
  • 13. 5.13 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Competition among Processes for Resources Three main control problems: Need for Mutual Exclusion Critical sections Deadlock Starvation
  • 14. 5.14 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Critical Section Problem Consider system of n processes {p0, p1, … pn-1} Each process has critical section segment of code Process may be changing common variables, updating table, writing file, etc When one process in critical section, no other may be in its critical section Critical section problem is to design protocol to solve this Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section
  • 15. 5.15 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Critical Section General structure of process pi is
  • 16. 5.16 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 16 Critical Regions (2) Mutual exclusion using critical regions
  • 17. 5.17 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted  Assume that each process executes at a nonzero speed  No assumption concerning relative speed of the n processes Two approaches depending on if kernel is preemptive or non-preemptive Preemptive – allows preemption of process when running in kernel mode Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU Essentially free of race conditions in kernel mode
  • 18. 5.18 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 18 Mutual Exclusion with Busy Waiting Proposed solution to critical region problem (a) Process 0. (b) Process 1.
  • 19. 5.19 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Peterson’s Solution Good algorithmic description of solving the problem Two process solution Assume that the load and store instructions are atomic; that is, cannot be interrupted The two processes share two variables: int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!
  • 20. 5.20 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition do { flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } while (true); Provable that 1. Mutual exclusion is preserved 2. Progress requirement is satisfied 3. Bounded-waiting requirement is met Algorithm for Process Pi
  • 21. 5.21 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Synchronization Hardware Many systems provide hardware support for critical section code All solutions below based on idea of locking Protecting critical regions via locks Uniprocessors – could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems  Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions  Atomic = non-interruptible Either test memory word and set value Or swap contents of two memory words
  • 22. 5.22 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition do { acquire lock critical section release lock remainder section } while (TRUE); Solution to Critical-section Problem Using Locks
  • 23. 5.23 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 23 Test & Set Lock Register Entering and leaving a critical region using the TSL instruction
  • 24. 5.24 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition test_and_set Instruction Definition: boolean test_and_set (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
  • 25. 5.25 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Solution using test_and_set() Shared boolean variable lock, initialized to FALSE Solution: do { while (test_and_set(&lock)) ; /* do nothing */ /* critical section */ lock = false; /* remainder section */ } while (true);
  • 26. 5.27 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Mutex Locks Previous solutions are complicated and generally inaccessible to application programmers OS designers build software tools to solve critical section problem Simplest is mutex lock Product critical regions with it by first acquire() a lock then release() it Boolean variable indicating if lock is available or not Calls to acquire() and release() must be atomic Usually implemented via hardware atomic instructions But this solution requires busy waiting This lock therefore called a spinlock
  • 27. 5.28 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition acquire() and release() acquire() { while (!available) ; /* busy wait */ available = false;; } release() { available = true; } do { acquire lock critical section release lock remainder section } while (true);
  • 28. 5.29 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Semaphore Synchronization tool that does not require busy waiting Semaphore S – integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Less complicated Can only be accessed via two indivisible (atomic) operations }
  • 29. 5.30 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Semaphore Primitives DOWN UP
  • 30. 5.31 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Semaphore Usage Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1 Then a mutex lock Can implement a counting semaphore S as a binary semaphore Can solve various synchronization problems Consider P1 and P2 that require S1 to happen before S2 P1: S1; signal(synch); P2: wait(synch); S2;
  • 31. 5.32 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Semaphore Implementation Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section Could now have busy waiting in critical section implementation  But implementation code is short  Little busy waiting if critical section rarely occupied Note that applications may spend lots of time in critical sections and therefore this is not a good solution
  • 32. 5.33 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Binary Semaphore Primitives
  • 33. 5.34 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Mutual Exclusion Using Semaphores
  • 34. 5.35 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S); . . signal(S); signal(Q); signal(Q); signal(S); Starvation – indefinite blocking A process may never be removed from the semaphore queue in which it is suspended Priority Inversion – Scheduling problem when lower-priority process holds a lock needed by higher-priority process Solved via priority-inheritance protocol
  • 35. 5.36 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Classical Problems of Synchronization Classical problems used to test newly-proposed synchronization schemes Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem
  • 36. 5.37 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 37 The producer-consumer problem
  • 37. 5.38 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Dining-Philosophers Problem Philosophers spend their lives thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl Need both to eat, then release both when done In the case of 5 philosophers Shared data  Bowl of rice (data set)  Semaphore chopstick [5] initialized to 1
  • 38. 5.39 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 39 Dining Philosophers (2) A non-solution to the dining philosophers problem
  • 39. 5.40 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 40 Dining Philosophers (3)
  • 40. 5.41 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 41 Dining Philosophers (4)
  • 41. 5.42 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition 42 The Sleeping Barber Problem (1)
  • 42. 5.43 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition #define CHAIRS 5 /* # chairs for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0; /* # of customers waiting for service */ semaphore barbers = 0; /* # of barbers waiting for customers */ semaphore mutex = 1; /* for mutual exclusion */ int waiting = 0; /* customer are waiting (not being cut) */ void barber(void) { while (TRUE) { down(&customers); /* go to sleep if # of customers is 0 */ down(&mutex); /* acquire access to "waiting' */ waiting = waiting - 1; /* decrement count of waiting customers */ up(&barbers); /* one barber is now read y to cut hair */ up(&mutex); /* release 'waiting' */ cut_hair(); /* cut hair (outside critical region */ } } void customer(void) { down(&mutex); /* enter critical region */ if (waiting < CH AIRS) { /* if there are no free chairs, leave */ waiting = waiting + 1; /* increment count of waiting customers */ up(&customers); /* wake up barber if necessary */ up(&mutex); /* release access to 'waiting' */ down(&barbers); /* go to sleep if # of free barbers is 0 */ get_haircut(); /* be seated and be served */ } else { up(&mutex); /* shop is full; do not wait */ } }
  • 43. 5.44 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Abstract data type, internal variables only accessible by code within the procedure Only one process may be active within the monitor at a time But not powerful enough to model some synchronization schemes monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……} Initialization code (…) { … } } }
  • 44. 5.45 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Schematic view of a Monitor
  • 45. 5.46 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Condition Variables condition x, y; Two operations on a condition variable: x.wait () – a process that invokes the operation is suspended until x.signal () x.signal () – resumes one of processes (if any) that invoked x.wait ()  If no x.wait () on the variable, then it has no effect on the variable
  • 46. 5.47 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Monitor with Condition Variables
  • 47. 5.48 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Condition Variables Choices If process P invokes x.signal (), with Q in x.wait () state, what should happen next? If Q is resumed, then P must wait Options include Signal and wait – P waits until Q leaves monitor or waits for another condition Signal and continue – Q waits until P leaves the monitor or waits for another condition Both have pros and cons – language implementer can decide Monitors implemented in Concurrent Pascal compromise  P executing signal immediately leaves the monitor, Q is resumed Implemented in other languages including Mesa, C#, Java
  • 48. 5.49 Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Synchronization Examples Solaris Windows XP Linux Pthreads

Editor's Notes

  1. Illustration of the problem:Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
  2. Processes P1 and P2 are both executing, each on a separate processor. P1 invokes the echo procedure. 2. While P1 is inside the echo procedure, P2 invokes echo. Because P1 is still inside the echo procedure (whether P1 is suspended or executing), P2 is blocked from entering the procedure. Therefore, P2 is suspended awaiting the availability of the echo procedure. 3. At a later time, process P1 completes execution of echo, exits that procedure, and continues executing. Immediately upon the exit of P1 from echo, P2 is resumed and begins executing echo.
  3. Need for mutual exclusion. Suppose two or more processes require access to a single nonsharable resource, such as a printer. During the course of execution, each process will be sending commands to the I/O device, receiving status information, sending data, and/or receiving data. We will refer to such a resource as a critical resource, and the portion of the program that uses it a critical section of the program. It is important that only one program at a time be allowed in its critical section. We cannot simply rely on the OS to understand and enforce this restriction because the detailed requirements may not be obvious. In the case of the printer, for example, we want any individual process to have control of the printer while it prints an entire file. Otherwise, lines from competing processes will be interleaved. The enforcement of mutual exclusion creates two additional control problems: deadlock. Two processes is waiting for the same resources (or each waiting for a resource that the other has exclusive use to) Neither will release the resource that it already owns until it has acquired the other resource and performed the function requiring both resources. The two processes are deadlocked. starvation. The OS may grant access to resources to a number of processes while neglecting another
  4. An example of a semaphore
  5. A more restrictive semaphore which may only have the value of 0 or 1 A similar concept related to the binary semaphore is the mutex. A key difference between the two is that the process that locks the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1). In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it.
  6. This shows a straightforward solution to the mutual exclusion problem using a semaphore Consider n processes, identified in the array P(i), all of which need access to the same resource s. Each process has a critical section used to access the resource. In each process, a semWait(s) is executed just before its critical section. If the value of s becomes negative, the process is blocked. If the value is 1, then it is decremented to 0 and the process immediately enters its critical section; because s is no longer positive, no other process will be able to enter its critical section.