Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Lecture 9 - Process Synchronization.pptx
1. University of Liberal Arts Bangladesh 1
OPERATING SYSTEMS
CSE 204
LECTURE 9
PROCESS SYNCHRONIZATION
2. Chapter 6: Process Synchronization
Operating System Concepts - by Gagne, Silberschatz, and Galvin
3. Synchronization
� Co-operating process – one that can affect / be affected by
other processes
� Co-operating processes may either directly share a logical
address space (i.e. code & data), or share data through files
or messages through threads
� Concurrent access to shared data can result in inconsistencies
P1 P2 P3
Data
Delete
data
update
data
Read
data
3
4. 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
4
5. Background
● Concurrent access to shared data may result in data
inconsistency 🡪 Multiple threads in a single process
� Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating
processes
5
6. Background
� Disjoint threads use disjoint sets of variables, and do not
use shared resources.
� The progress of one thread is independent of all
other threads, to which it is disjoint.
� Non-disjoint threads influence each other by using
shared data and/or resources.
� Two possibilities:
� Competing threads: compete for access to the data
resources
� Cooperating threads: e.g. producer / consumer
model
� Without synchronization, the effect of non-disjoint
parallel threads influencing each other is not
predictable and not reproducible.
6
7. Race Conditions
� A race condition is where multiple processes/threads
concurrently read and write to a shared memory
location and the result depends on the order of the
execution.
� The part of the program, in which race conditions can
occur, is called critical section.
7
8. Critical Sections
� A critical section is a piece of code that accesses a
shared resource (data structure or device) that must not
be concurrently accessed by more than one thread of
execution.
� The goal is to provide a mechanism by which only one
instance of a critical section is executing for a particular
shared resource.
� Unfortunately, it is often very difficult to detect critical
section bugs
8
9. Critical Sections
� A Critical Section Environment contains:
� Entry Section Code requesting entry into the critical
section.
� Critical Section Code in which only one process can
execute at any one time.
� Exit Section The end of the critical section, releasing or
allowing others in.
� Remainder Section Rest of the code AFTER the critical
section.
do {
critical section;
remainder sections;
} while (true);
Entry Section;
Exit Section;
9
10. Solution to Critical-Section Problem
A solution to the critical-section problem must satisfy the
following requirements:
1. Mutual Exclusion – Only one process can be in its critical
section
e.g. 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
10
11. Solution to Critical-Section Problem (Cont.)
3. Bounded Waiting - A bound, or limit 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
11
12. Synchronization Hardware
� Hardware can be used to solve the critical section
problem
� Many systems provide hardware support for critical
section code
� Uniprocessors – could disable interrupts
� Currently running code would execute without
preemption
� No unexpected modifications would be made to
shared variables
� Disabling interrupts in a multi-processor environment is
not feasible
13. Synchronization Hardware
� Instead, we can generally state that any solution to the
critical-section problem requires a simple tool, a LOCK
� Race conditions are prevented by requiring that critical
regions be protected by locks
while (true) {
acquire lock
critical section
release lock
reminder
section
}
14. Synchronization Hardware
� Modern machines provide special atomic hardware instructions
� Atomic = non-interruptable
� Either test memory word and set value (TestAndSet())
� Or swap contents of two memory words(Swap())
� These instructions allow us either to test & modify the content of a
word, or to swap the contents of two words, atomically
TestAndSet
boolean TestAndSet(boolean *target) {
boolean rv = *target;
*target = true;
return rv;
}
15. Synchronization Hardware
� This instruction is executing atomically
� if two TestAndSet instructions are executed
simultaneously (on different CPUs), they will be
executed sequentially
TestAndSet with mutual exclusion
lock=false
do{
while
(TestAndSet(&lock));
// do
nothing
// critical section
lock = false;
// reminder
section
} while(true);
P1 I1
P2 I2
Mutual-exclusion: while P1
is executing, all other
interrupts are disabled
16. Synchronization Hardware
Swap with mutual exclusion
do{
key = true;
while (key==true)
swap(&lock,
&key);
critical
section
lock = false;
reminder
section
} while(true);
Swap
void swap (boolean *a, boolean *b){
boolean temp = *a;
*a=*b;
*b=temp;
}
Pi L K
F T
swap T F
Pj L K
T T
swap T T
Pj L K
F T
swap T F
17. Semaphore
� Semaphore: a synchronization tool, A flag used to
indicate that a routine cannot proceed if a shared
resource is already in use by another routine.
� Semaphore S – integer variable
� Two standard operations modify S: wait() and signal()
� Semaphore is signaling mechanism – no ownership
� Can only be accessed via two indivisible (atomic)
operations
� wait (S) {
while S <= 0
; // no-op
S--;
}
� signal (S) {
S++;
}
Resource
S = 2
Thread 1
Thread 2
Thread 3
Thread 3: Try to access, but not
allowed, because S <=0 (Thread
1 and Thread 2 occupy S)
18. � Two types:
� Counting semaphore – integer value can range over an
unrestricted domain
�can allow n processes to access (e.g. database) by
initializing the semaphore to n
� Binary semaphore – integer value can range only
between 0 and 1
�N processes share a semaphore - mutex locks
(provide mutual exclusion)
Semaphore as General Synchronization Tool
Resource
Thread 1
Thread 2
Thread 3
If Thread 2 try to access resource by
accessing mutex object then attempts
to take mutex will return error
Thread 1 accessed resource:
mutex object held by Thread 1:
LOCK(mutex)
Do work
UNLOCK (mutex)
19. Semaphores Usage
� Mutex is an object owned by thread, so
there is a ownership in mutex. Mutex allows
only one thread to access resource
� Semaphore is a signaling mechanism. It
allows a number of thread to access to the
resources.