SlideShare a Scribd company logo
Silberschatz, Galvin and Gagne ©2018
Operating System Concepts
Chapter 7: Synchronization
Examples
2
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Chapter 7: Synchronization Examples
■ Explain the bounded-buffer, readers-writers, and dining philosophers
synchronization problems
■ Describe the tools used by Linux and Windows to solve
synchronization problems
■ Illustrate how POSIX and Java can be used to solve process
synchronization problems
3
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Classical Problems of Synchronization
■ Classical problems used to test newly-proposed synchronization
schemes
● Bounded-Buffer Problem
● Readers and Writers Problem
● Dining-Philosophers Problem
4
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded-Buffer Problem
■ n buffers, each can hold one item
■ Semaphore mutex initialized to the value 1
■ Semaphore full initialized to the value 0
■ Semaphore empty initialized to the value n
5
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
■ The structure of the producer process
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
}
Semaphore mutex = 1
Semaphore full = 0
Semaphore empty = n
6
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Problem (Cont.)
■ The structure of the consumer process
while (true) {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
}
Semaphore mutex = 1
Semaphore full = 0
Semaphore empty = n
7
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem
■ A data set is shared among a number of concurrent processes
● Readers – only read the data set; they do not perform any updates
● Writers – can both read and write
■ Problem – allow multiple readers to read at the same time
● Only one single writer can access the shared data at the same time
■ Several variations of how readers and writers are considered – all
involve some form of priorities
■ Shared Data
● Data set
● Semaphore rw_mutex initialized to 1
● Semaphore mutex initialized to 1
● Integer read_count initialized to 0
8
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
■ The structure of a writer process
while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
Semaphore rw_mutex = 1
Semaphore mutex = 1
Integer read_count = 0
9
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem (Cont.)
■ The structure of a reader process
while (true){
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
... /* reading is performed *
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
}
Semaphore rw_mutex = 1
Semaphore mutex = 1
Integer read_count = 0
while (true) {
wait(rw_mutex);
...
/* writing is performed
*/
...
signal(rw_mutex);
}
10
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Readers-Writers Problem Variations
■ First variation – no reader kept waiting unless writer has permission
to use shared object
■ Second variation – once writer is ready, it performs the write ASAP
■ Both may have starvation leading to even more variations
■ Problem is solved on some systems by kernel providing reader-writer
locks
11
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem
■ Philosophers spend their lives alternating 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
4 Bowl of rice (data set)
4 Semaphore chopstick [5] initialized to 1
12
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Dining-Philosophers Problem Algorithm
■ Semaphore Solution
■ The structure of Philosopher i:
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
/* eat for awhile */
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
/* think for awhile */
}
■ What is the problem with this algorithm?
13
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers {
enum { THINKING, HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self[i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
}
14
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Solution to Dining Philosophers (Cont.)
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
15
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Solution to Dining Philosophers (Cont.)
■ Each philosopher i invokes the operations pickup() and putdown() in
the following sequence:
DiningPhilosophers.pickup(i);
/** EAT **/
DiningPhilosophers.putdown(i);
■ No deadlock, but starvation is possible
16
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Kernel Synchronization - Windows
■ Uses interrupt masks to protect access to global resources on
uniprocessor systems
■ Uses spinlocks on multiprocessor systems
● Spin locking-thread will never be preempted
■ Also provides dispatcher objects user-land which may act mutexes,
semaphores, events, and timers
● Events
4 An event acts much like a condition variable
● Timers notify one or more thread when time expired
● Dispatcher objects either signaled-state (object available) or non-signaled
state (thread will block)
17
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Kernel Synchronization - Windows
■ Mutex dispatcher object
18
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Linux Synchronization
■ Linux:
● Prior to kernel Version 2.6, disables interrupts to implement short critical
sections
● Version 2.6 and later, fully preemptive
■ Linux provides:
● Semaphores
● atomic integers
● spinlocks
● reader-writer versions of both
■ On single-CPU system, spinlocks replaced by enabling and disabling
kernel preemption
19
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Linux Synchronization
■ Atomic variables
● atomic_t is the type for atomic integer
■ Consider the variables
atomic_t counter;
int value;
20
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Synchronization
■ POSIX API provides
● mutex locks
● semaphores
● condition variable
■ Widely used on UNIX, Linux, and macOS
21
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Mutex Locks
■ Creating and initializing the lock
■ Acquiring and releasing the lock
22
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Semaphores
■ POSIX provides two versions – named and unnamed
■ Named semaphores can be used by unrelated processes, unnamed
cannot
23
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Named Semaphores
■ Creating an initializing the semaphore:
■ Another process can access the semaphore by referring to its name
SEM.
■ Acquiring and releasing the semaphore:
24
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Unnamed Semaphores
■ Creating an initializing the semaphore:
■ Acquiring and releasing the semaphore:
25
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Condition Variables
■ Since POSIX is typically used in C/C++ and these languages do not
provide a monitor, POSIX condition variables are associated with a
POSIX mutex lock to provide mutual exclusion: Creating and
initializing the condition variable:
26
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
POSIX Condition Variables
■ Thread waiting for the condition a == b to become true:
■ Thread signaling another thread waiting on the condition variable:
27
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Synchronization
■ Java provides rich set of synchronization features:
● Java monitors
● Reentrant locks
● Semaphores
● Condition variables
28
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Monitors
■ Every Java object has associated with it a single lock.
■ If a method is declared as synchronized, a calling thread must own
the lock for the object.
■ If the lock is owned by another thread, the calling thread must wait for
the lock until it is released.
■ Locks are released when the owning thread exits the synchronized
method.
29
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
30
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Synchronization
■ A thread that tries to acquire an unavailable lock is placed in the
object’s entry set:
31
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Synchronization
■ Similarly, each object also has a wait set.
■ When a thread calls wait():
1. It releases the lock for the object
2. The state of the thread is set to blocked
3. The thread is placed in the wait set for the object
32
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Synchronization
■ A thread typically calls wait() when it is waiting for a condition to
become true.
■ How does a thread get notified?
■ When a thread calls notify():
1. An arbitrary thread T is selected from the wait set
2. T is moved from the wait set to the entry set
3. Set the state of T from blocked to runnable.
■ T can now compete for the lock to check if the condition it was waiting
for is now true.
33
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
34
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Bounded Buffer – Java Synchronization
35
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Reentrant Locks
■ Similar to mutex locks
■ The finally clause ensures the lock will be released in case an
exception occurs in the try block.
36
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Semaphores
■ Constructor:
■ Usage:
37
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
■ Condition variables are associated with an ReentrantLock.
■ Creating a condition variable using newCondition() method of
ReentrantLock:
■ A thread waits by calling the await() method, and signals by calling
the signal() method.
38
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
■ Example:
● Five threads numbered 0 .. 4
● Shared variable turn indicating which thread’s turn it is.
● Thread calls doWork() when it wishes to do some work. (But it may only
do work if it is their turn.
● If not their turn, wait
● If their turn, do some work for awhile …...
● When completed, notify the thread whose turn is next.
■ Necessary data structures:
39
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Java Condition Variables
40
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Alternative Approaches
■ Transactional Memory
■ OpenMP
■ Functional Programming Languages
41
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Transactional Memory
■ Consider a function update() that
must be called atomically. One
option is to use mutex locks:
■ A memory transaction is a
sequence of read-write operations
to memory that are performed
atomically. A transaction can be
completed by adding atomic{S}
which ensure statements in S are
executed atomically:
42
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
OpenMP
■ OpenMP is a set of compiler directives and API that support parallel
programming.
void update(int value)
{
#pragma omp critical
{
count += value
}
}
■ The code contained within the #pragma omp critical directive
is treated as a critical section and performed atomically.
43
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Functional Programming Languages
■ Functional programming languages offer a different paradigm than
procedural languages in that they do not maintain state.
■ Variables are treated as immutable and cannot change state once
they have been assigned a value.
■ There is increasing interest in functional languages such as Erlang
and Scala for their approach in handling data races.
44
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Summary
■ Classic problems of process synchronization include the bounded-
buffer, readers–writers, and dining-philosophers problems. Solutions
to these problems can be developed using the tools presented in
Chapter 6, including mutex locks, semaphores, monitors, and
condition variables.
■ Windows uses dispatcher objects as well as events to implement
process synchronization tools.
■ Linux uses a variety of approaches to protect against race conditions,
including atomic variables, spinlocks, and mutex locks.
■ The POSIX API provides mutex locks, semaphores, and condition
variables. POSIX provides two forms of semaphores: named and
unnamed. Several unrelated processes can easily access the same
named semaphore by sim- ply referring to its name. Unnamed
semaphores cannot be shared as easily, and require
placing the semaphore in a region of shared memory.
45
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Summary (Cont.)
■ Java has a rich library and API for synchronization. Available tools
include monitors (which are provided at the language level) as well as
reentrant locks, semaphores, and condition variables (which are
supported by the API).
■ Alternative approaches to solving the critical-section problem include
transactional memory, OpenMP, and functional languages.
Functional languages are particularly intriguing, as they offer a
different programming paradigm from procedural languages. Unlike
procedural languages, functional languages do not maintain state and
therefore are generally immune from race conditions and critical
sections.
46
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Exercise (1/2)
■ #define MAX_RESOURCES 5
■ int available_resources = MAX_RESOURCES;
46
/* decrease available_resources by
count resources return 0 if sufficient
resources available,
otherwise return -1 */
int decrease_count(int count) {
if (available_resources < count)
return -1;
else {
available_resources -=count;
return 0;
}
}
/* increase available_resources by count */
int increase_count(int count) {
available_resources += count;
return 0;
}
1.Identify the data involved in the race
condition.
2.Identify the location (or locations) in
the code where the race condition
occurs.
47
Operating System Concepts Silberschatz, Galvin and Gagne ©2018
Exercise (2/2)
monitor resources
{
int available_resources;
condition resources_avail;
int decrease_count(int count)
{
WHILE (available_resources < count)
resources_avail.wait();
available_resources = available_resources - count;
}
int increase_count(int count)
{
available_resources = available_resources + count;
resources_avail.signal();
}
}
3. Does the above code satisfy the conditions of mutual exclusion problem?
47
Silberschatz, Galvin and Gagne ©2018
Operating System Concepts
End of Chapter 7

More Related Content

Similar to ch7_EN_BK_sync2.pdf

6.Process Synchronization
6.Process Synchronization6.Process Synchronization
6.Process Synchronization
Senthil Kanth
 
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter HeckOSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
NETWAYS
 
process synchronisation operating system
process synchronisation operating systemprocess synchronisation operating system
process synchronisation operating system
codefast
 
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
cscarcas
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Ratpack 101 - GeeCON 2015
Ratpack 101 - GeeCON 2015Ratpack 101 - GeeCON 2015
Ratpack 101 - GeeCON 2015
Alvaro Sanchez-Mariscal
 
ch4_EN_BK_Threads.pdf
ch4_EN_BK_Threads.pdfch4_EN_BK_Threads.pdf
ch4_EN_BK_Threads.pdf
HoNguyn746501
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
Ahmad Alarbeed
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronization
bismahmalik22
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
Srinivasan Raghvan
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Daniel Bristot de Oliveira
 
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
mohammedabomashowrms
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
C.U
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance Tuning
Jeremy Leisy
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
vijay974055
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
karinaabyys
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio ppt
Abdikani34
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdf
sharada3
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating system
Sami Mughal
 

Similar to ch7_EN_BK_sync2.pdf (20)

6.Process Synchronization
6.Process Synchronization6.Process Synchronization
6.Process Synchronization
 
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter HeckOSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
OSMC 2017 | Log Monitoring with Logstash and Icinga by Walter Heck
 
process synchronisation operating system
process synchronisation operating systemprocess synchronisation operating system
process synchronisation operating system
 
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
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Ratpack 101 - GeeCON 2015
Ratpack 101 - GeeCON 2015Ratpack 101 - GeeCON 2015
Ratpack 101 - GeeCON 2015
 
ch4_EN_BK_Threads.pdf
ch4_EN_BK_Threads.pdfch4_EN_BK_Threads.pdf
ch4_EN_BK_Threads.pdf
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronization
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
 
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
Lecture 9.pdfLecture 9.pdfLecture 9.pdfLecture 9.pdf
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance Tuning
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
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
 

More from HoNguyn746501

ch2_EN_BK.pdf
ch2_EN_BK.pdfch2_EN_BK.pdf
ch2_EN_BK.pdf
HoNguyn746501
 
ch10_massSt.pdf
ch10_massSt.pdfch10_massSt.pdf
ch10_massSt.pdf
HoNguyn746501
 
ch9_virMem.pdf
ch9_virMem.pdfch9_virMem.pdf
ch9_virMem.pdf
HoNguyn746501
 
ch11_fileInterface.pdf
ch11_fileInterface.pdfch11_fileInterface.pdf
ch11_fileInterface.pdf
HoNguyn746501
 
ch5_EN_CPUSched.pdf
ch5_EN_CPUSched.pdfch5_EN_CPUSched.pdf
ch5_EN_CPUSched.pdf
HoNguyn746501
 
ch12_fileImplementation.pdf
ch12_fileImplementation.pdfch12_fileImplementation.pdf
ch12_fileImplementation.pdf
HoNguyn746501
 
ch3_EN_BK_Process.pdf
ch3_EN_BK_Process.pdfch3_EN_BK_Process.pdf
ch3_EN_BK_Process.pdf
HoNguyn746501
 
ch8_mainMem.pdf
ch8_mainMem.pdfch8_mainMem.pdf
ch8_mainMem.pdf
HoNguyn746501
 
ch1_EN_BK-2.pdf
ch1_EN_BK-2.pdfch1_EN_BK-2.pdf
ch1_EN_BK-2.pdf
HoNguyn746501
 

More from HoNguyn746501 (9)

ch2_EN_BK.pdf
ch2_EN_BK.pdfch2_EN_BK.pdf
ch2_EN_BK.pdf
 
ch10_massSt.pdf
ch10_massSt.pdfch10_massSt.pdf
ch10_massSt.pdf
 
ch9_virMem.pdf
ch9_virMem.pdfch9_virMem.pdf
ch9_virMem.pdf
 
ch11_fileInterface.pdf
ch11_fileInterface.pdfch11_fileInterface.pdf
ch11_fileInterface.pdf
 
ch5_EN_CPUSched.pdf
ch5_EN_CPUSched.pdfch5_EN_CPUSched.pdf
ch5_EN_CPUSched.pdf
 
ch12_fileImplementation.pdf
ch12_fileImplementation.pdfch12_fileImplementation.pdf
ch12_fileImplementation.pdf
 
ch3_EN_BK_Process.pdf
ch3_EN_BK_Process.pdfch3_EN_BK_Process.pdf
ch3_EN_BK_Process.pdf
 
ch8_mainMem.pdf
ch8_mainMem.pdfch8_mainMem.pdf
ch8_mainMem.pdf
 
ch1_EN_BK-2.pdf
ch1_EN_BK-2.pdfch1_EN_BK-2.pdf
ch1_EN_BK-2.pdf
 

Recently uploaded

按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
yizxn4sx
 
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
terpt4iu
 
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
snfdnzl7
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
aozcue
 
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
xuqdabu
 
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
1jtj7yul
 
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
6oo02s6l
 
加急办理美国南加州大学毕业证文凭毕业证原版一模一样
加急办理美国南加州大学毕业证文凭毕业证原版一模一样加急办理美国南加州大学毕业证文凭毕业证原版一模一样
加急办理美国南加州大学毕业证文凭毕业证原版一模一样
u0g33km
 
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
eydeofo
 
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
2g3om49r
 
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
nudduv
 
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
yizxn4sx
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
aozcue
 
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
yizxn4sx
 
按照学校原版(QU文凭证书)皇后大学毕业证快速办理
按照学校原版(QU文凭证书)皇后大学毕业证快速办理按照学校原版(QU文凭证书)皇后大学毕业证快速办理
按照学校原版(QU文凭证书)皇后大学毕业证快速办理
8db3cz8x
 
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
xuqdabu
 
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
xuqdabu
 
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
xuqdabu
 
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Peter Gallagher
 
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
uyesp1a
 

Recently uploaded (20)

按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
按照学校原版(UAL文凭证书)伦敦艺术大学毕业证快速办理
 
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
按照学校原版(Adelaide文凭证书)阿德莱德大学毕业证快速办理
 
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
按照学校原版(USD文凭证书)圣地亚哥大学毕业证快速办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
 
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide文凭证书)阿德莱德大学毕业证如何办理
 
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
按照学校原版(SUT文凭证书)斯威本科技大学毕业证快速办理
 
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
按照学校原版(Birmingham文凭证书)伯明翰大学|学院毕业证快速办理
 
加急办理美国南加州大学毕业证文凭毕业证原版一模一样
加急办理美国南加州大学毕业证文凭毕业证原版一模一样加急办理美国南加州大学毕业证文凭毕业证原版一模一样
加急办理美国南加州大学毕业证文凭毕业证原版一模一样
 
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
一比一原版(UOL文凭证书)利物浦大学毕业证如何办理
 
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
1比1复刻澳洲皇家墨尔本理工大学毕业证本科学位原版一模一样
 
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
一比一原版(ANU文凭证书)澳大利亚国立大学毕业证如何办理
 
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
按照学校原版(Greenwich文凭证书)格林威治大学毕业证快速办理
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
 
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
按照学校原版(Westminster文凭证书)威斯敏斯特大学毕业证快速办理
 
按照学校原版(QU文凭证书)皇后大学毕业证快速办理
按照学校原版(QU文凭证书)皇后大学毕业证快速办理按照学校原版(QU文凭证书)皇后大学毕业证快速办理
按照学校原版(QU文凭证书)皇后大学毕业证快速办理
 
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
一比一原版(TheAuckland毕业证书)新西兰奥克兰大学毕业证如何办理
 
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
一比一原版(UQ文凭证书)昆士兰大学毕业证如何办理
 
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
一比一原版(Monash文凭证书)莫纳什大学毕业证如何办理
 
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
 
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
按照学校原版(Columbia文凭证书)哥伦比亚大学毕业证快速办理
 

ch7_EN_BK_sync2.pdf

  • 1. Silberschatz, Galvin and Gagne ©2018 Operating System Concepts Chapter 7: Synchronization Examples
  • 2. 2 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Chapter 7: Synchronization Examples ■ Explain the bounded-buffer, readers-writers, and dining philosophers synchronization problems ■ Describe the tools used by Linux and Windows to solve synchronization problems ■ Illustrate how POSIX and Java can be used to solve process synchronization problems
  • 3. 3 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Classical Problems of Synchronization ■ Classical problems used to test newly-proposed synchronization schemes ● Bounded-Buffer Problem ● Readers and Writers Problem ● Dining-Philosophers Problem
  • 4. 4 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded-Buffer Problem ■ n buffers, each can hold one item ■ Semaphore mutex initialized to the value 1 ■ Semaphore full initialized to the value 0 ■ Semaphore empty initialized to the value n
  • 5. 5 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded Buffer Problem (Cont.) ■ The structure of the producer process while (true) { ... /* produce an item in next_produced */ ... wait(empty); wait(mutex); ... /* add next produced to the buffer */ ... signal(mutex); signal(full); } Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = n
  • 6. 6 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded Buffer Problem (Cont.) ■ The structure of the consumer process while (true) { wait(full); wait(mutex); ... /* remove an item from buffer to next_consumed */ ... signal(mutex); signal(empty); ... /* consume the item in next consumed */ ... } Semaphore mutex = 1 Semaphore full = 0 Semaphore empty = n
  • 7. 7 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Readers-Writers Problem ■ A data set is shared among a number of concurrent processes ● Readers – only read the data set; they do not perform any updates ● Writers – can both read and write ■ Problem – allow multiple readers to read at the same time ● Only one single writer can access the shared data at the same time ■ Several variations of how readers and writers are considered – all involve some form of priorities ■ Shared Data ● Data set ● Semaphore rw_mutex initialized to 1 ● Semaphore mutex initialized to 1 ● Integer read_count initialized to 0
  • 8. 8 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Readers-Writers Problem (Cont.) ■ The structure of a writer process while (true) { wait(rw_mutex); ... /* writing is performed */ ... signal(rw_mutex); } Semaphore rw_mutex = 1 Semaphore mutex = 1 Integer read_count = 0
  • 9. 9 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Readers-Writers Problem (Cont.) ■ The structure of a reader process while (true){ wait(mutex); read_count++; if (read_count == 1) wait(rw_mutex); signal(mutex); ... /* reading is performed * ... wait(mutex); read count--; if (read_count == 0) signal(rw_mutex); signal(mutex); } Semaphore rw_mutex = 1 Semaphore mutex = 1 Integer read_count = 0 while (true) { wait(rw_mutex); ... /* writing is performed */ ... signal(rw_mutex); }
  • 10. 10 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Readers-Writers Problem Variations ■ First variation – no reader kept waiting unless writer has permission to use shared object ■ Second variation – once writer is ready, it performs the write ASAP ■ Both may have starvation leading to even more variations ■ Problem is solved on some systems by kernel providing reader-writer locks
  • 11. 11 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Dining-Philosophers Problem ■ Philosophers spend their lives alternating 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 4 Bowl of rice (data set) 4 Semaphore chopstick [5] initialized to 1
  • 12. 12 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Dining-Philosophers Problem Algorithm ■ Semaphore Solution ■ The structure of Philosopher i: while (true){ wait (chopstick[i] ); wait (chopStick[ (i + 1) % 5] ); /* eat for awhile */ signal (chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); /* think for awhile */ } ■ What is the problem with this algorithm?
  • 13. 13 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum { THINKING, HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } }
  • 14. 14 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Solution to Dining Philosophers (Cont.) void test (int i) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } }
  • 15. 15 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Solution to Dining Philosophers (Cont.) ■ Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophers.pickup(i); /** EAT **/ DiningPhilosophers.putdown(i); ■ No deadlock, but starvation is possible
  • 16. 16 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Kernel Synchronization - Windows ■ Uses interrupt masks to protect access to global resources on uniprocessor systems ■ Uses spinlocks on multiprocessor systems ● Spin locking-thread will never be preempted ■ Also provides dispatcher objects user-land which may act mutexes, semaphores, events, and timers ● Events 4 An event acts much like a condition variable ● Timers notify one or more thread when time expired ● Dispatcher objects either signaled-state (object available) or non-signaled state (thread will block)
  • 17. 17 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Kernel Synchronization - Windows ■ Mutex dispatcher object
  • 18. 18 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Linux Synchronization ■ Linux: ● Prior to kernel Version 2.6, disables interrupts to implement short critical sections ● Version 2.6 and later, fully preemptive ■ Linux provides: ● Semaphores ● atomic integers ● spinlocks ● reader-writer versions of both ■ On single-CPU system, spinlocks replaced by enabling and disabling kernel preemption
  • 19. 19 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Linux Synchronization ■ Atomic variables ● atomic_t is the type for atomic integer ■ Consider the variables atomic_t counter; int value;
  • 20. 20 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Synchronization ■ POSIX API provides ● mutex locks ● semaphores ● condition variable ■ Widely used on UNIX, Linux, and macOS
  • 21. 21 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Mutex Locks ■ Creating and initializing the lock ■ Acquiring and releasing the lock
  • 22. 22 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Semaphores ■ POSIX provides two versions – named and unnamed ■ Named semaphores can be used by unrelated processes, unnamed cannot
  • 23. 23 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Named Semaphores ■ Creating an initializing the semaphore: ■ Another process can access the semaphore by referring to its name SEM. ■ Acquiring and releasing the semaphore:
  • 24. 24 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Unnamed Semaphores ■ Creating an initializing the semaphore: ■ Acquiring and releasing the semaphore:
  • 25. 25 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Condition Variables ■ Since POSIX is typically used in C/C++ and these languages do not provide a monitor, POSIX condition variables are associated with a POSIX mutex lock to provide mutual exclusion: Creating and initializing the condition variable:
  • 26. 26 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 POSIX Condition Variables ■ Thread waiting for the condition a == b to become true: ■ Thread signaling another thread waiting on the condition variable:
  • 27. 27 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Synchronization ■ Java provides rich set of synchronization features: ● Java monitors ● Reentrant locks ● Semaphores ● Condition variables
  • 28. 28 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Monitors ■ Every Java object has associated with it a single lock. ■ If a method is declared as synchronized, a calling thread must own the lock for the object. ■ If the lock is owned by another thread, the calling thread must wait for the lock until it is released. ■ Locks are released when the owning thread exits the synchronized method.
  • 29. 29 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded Buffer – Java Synchronization
  • 30. 30 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Synchronization ■ A thread that tries to acquire an unavailable lock is placed in the object’s entry set:
  • 31. 31 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Synchronization ■ Similarly, each object also has a wait set. ■ When a thread calls wait(): 1. It releases the lock for the object 2. The state of the thread is set to blocked 3. The thread is placed in the wait set for the object
  • 32. 32 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Synchronization ■ A thread typically calls wait() when it is waiting for a condition to become true. ■ How does a thread get notified? ■ When a thread calls notify(): 1. An arbitrary thread T is selected from the wait set 2. T is moved from the wait set to the entry set 3. Set the state of T from blocked to runnable. ■ T can now compete for the lock to check if the condition it was waiting for is now true.
  • 33. 33 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded Buffer – Java Synchronization
  • 34. 34 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Bounded Buffer – Java Synchronization
  • 35. 35 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Reentrant Locks ■ Similar to mutex locks ■ The finally clause ensures the lock will be released in case an exception occurs in the try block.
  • 36. 36 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Semaphores ■ Constructor: ■ Usage:
  • 37. 37 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Condition Variables ■ Condition variables are associated with an ReentrantLock. ■ Creating a condition variable using newCondition() method of ReentrantLock: ■ A thread waits by calling the await() method, and signals by calling the signal() method.
  • 38. 38 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Condition Variables ■ Example: ● Five threads numbered 0 .. 4 ● Shared variable turn indicating which thread’s turn it is. ● Thread calls doWork() when it wishes to do some work. (But it may only do work if it is their turn. ● If not their turn, wait ● If their turn, do some work for awhile …... ● When completed, notify the thread whose turn is next. ■ Necessary data structures:
  • 39. 39 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Java Condition Variables
  • 40. 40 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Alternative Approaches ■ Transactional Memory ■ OpenMP ■ Functional Programming Languages
  • 41. 41 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Transactional Memory ■ Consider a function update() that must be called atomically. One option is to use mutex locks: ■ A memory transaction is a sequence of read-write operations to memory that are performed atomically. A transaction can be completed by adding atomic{S} which ensure statements in S are executed atomically:
  • 42. 42 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 OpenMP ■ OpenMP is a set of compiler directives and API that support parallel programming. void update(int value) { #pragma omp critical { count += value } } ■ The code contained within the #pragma omp critical directive is treated as a critical section and performed atomically.
  • 43. 43 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Functional Programming Languages ■ Functional programming languages offer a different paradigm than procedural languages in that they do not maintain state. ■ Variables are treated as immutable and cannot change state once they have been assigned a value. ■ There is increasing interest in functional languages such as Erlang and Scala for their approach in handling data races.
  • 44. 44 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Summary ■ Classic problems of process synchronization include the bounded- buffer, readers–writers, and dining-philosophers problems. Solutions to these problems can be developed using the tools presented in Chapter 6, including mutex locks, semaphores, monitors, and condition variables. ■ Windows uses dispatcher objects as well as events to implement process synchronization tools. ■ Linux uses a variety of approaches to protect against race conditions, including atomic variables, spinlocks, and mutex locks. ■ The POSIX API provides mutex locks, semaphores, and condition variables. POSIX provides two forms of semaphores: named and unnamed. Several unrelated processes can easily access the same named semaphore by sim- ply referring to its name. Unnamed semaphores cannot be shared as easily, and require placing the semaphore in a region of shared memory.
  • 45. 45 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Summary (Cont.) ■ Java has a rich library and API for synchronization. Available tools include monitors (which are provided at the language level) as well as reentrant locks, semaphores, and condition variables (which are supported by the API). ■ Alternative approaches to solving the critical-section problem include transactional memory, OpenMP, and functional languages. Functional languages are particularly intriguing, as they offer a different programming paradigm from procedural languages. Unlike procedural languages, functional languages do not maintain state and therefore are generally immune from race conditions and critical sections.
  • 46. 46 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Exercise (1/2) ■ #define MAX_RESOURCES 5 ■ int available_resources = MAX_RESOURCES; 46 /* decrease available_resources by count resources return 0 if sufficient resources available, otherwise return -1 */ int decrease_count(int count) { if (available_resources < count) return -1; else { available_resources -=count; return 0; } } /* increase available_resources by count */ int increase_count(int count) { available_resources += count; return 0; } 1.Identify the data involved in the race condition. 2.Identify the location (or locations) in the code where the race condition occurs.
  • 47. 47 Operating System Concepts Silberschatz, Galvin and Gagne ©2018 Exercise (2/2) monitor resources { int available_resources; condition resources_avail; int decrease_count(int count) { WHILE (available_resources < count) resources_avail.wait(); available_resources = available_resources - count; } int increase_count(int count) { available_resources = available_resources + count; resources_avail.signal(); } } 3. Does the above code satisfy the conditions of mutual exclusion problem? 47
  • 48. Silberschatz, Galvin and Gagne ©2018 Operating System Concepts End of Chapter 7