Title: Process Synchronization
1Process Synchronization
- For concurrent or parallel execution
- Concurrent execution might be called virtual
parallelism - OS may arbitrarily interleave execution of
processes - For example, with preemptive or RR scheduling
- Time-out and priority scheduling cause arbitrary
interruptions - Page faults cause process suspension
- Parallel processing is more complex than
uni-processing -- interleaved access to memory bus
2Concurrency environments
- Multiprogramming
- Multiple processes share single processor
- Multiprocessing
- Multiple processes scheduled to multiple
processors and using shared memory - Distributed processing
- processes executing on different processors
communicating through message passing
3Process Synchronization
- Processes synchronize either for competition or
cooperation - Competition independent processes that
occasionally (and asynchronously) synchronize to
ensure that shared resources are used correctly - Cooperation dependent processes that
synchronize for data communication or for
purposes such as timing, dependencies, etc.
4The mutual exclusion problem
- Processes access a shared device that must be
used in mutual exclusion - Example printer
- Interleaved access can yield incorrect results
- Ex lost update problem
- Ex inconsistent retrieval
- If order of code execution of concurrent
processes can yield different results causing
inconsistency of data or resources, this is
called a race condition. Such code is called a
critical section - To ensure that a critical section is not
interrupted, execution of its code must be
atomic
5Example of Lost update problem
- Two agents try to purchase the last seat
- Communicate over a network (involving a
propagation delay) - Assume Seat 1 in the following scenario
- Agent 1 get (Seat) Agent 2 get (Seat)
- if (Seat gt0) if (Seatgt0)
- Seat Seat -1 Seat Seat -1
store Seat store
Seat - Try interleaving execution, so that both get the
same seat
6Example of Inconsistent Retrieval
- Transaction 1 transfer 100 from S to T
- Transaction 2 read and print value of (S T)
- Note that process 2 is only a reader to database
- If reads and writes are interleaved, process 2
can obtain a wrong result, for example if process
2 reads S and T after S was updated but before T
was updated. - Initial state S T 500
- Process 1 S 500 100 600
- Process 2 S 600 T 500 S T 1100
(incorrect) - Process 1 T 500 - 100 400 (S T is 1000
correct)
7Possible solution lock critical section
- Common solution to the critical section problem
is to lock some agreed upon flag for shared
resource - Problems
- Overhead
- This will cut down on concurrency
- Implementation of locking has overhead
- Misuse of flag
- Failure to lock
- Failure to unlock
- Starvation
- Process might be locked out of critical section
indefinitely - Resource Deadlock can occur
8Mutual Exclusion mechanism requirements
- Only one process is allowed in the critical
section for a shared resource at a time /prevent
inconsistencies/ - Bounded waits /no starvation or resource
deadlock/ - Process execution outside a critical section
should not delay other processes in terms of
using the shared resource - If no process is in critical section to shared
resource, any access to the critical section
should not be delayed by mutual exclusion
mechanism - All mechanisms should also apply to multiple
processors - Critical section should be finite in execution
9Software or hardware locks
- Test and set must be atomic.
- Suppose 2 processes execute the following code
- while (lock is set) loop set
lock - critical section
- clear lock
- If not atomic process1 tests, finds the lock is
clear, and then is interrupted and process2
obtains processor. process2 also tests the lock.
Both find the lock is clear, both set the lock
and work within the critical section at the same
or at interleaved times.
10Solution 1 process disables interrupts
- We leave it up to the programmer to ensure
consistency here. This mechanism is used by
kernel processes. - while (true)
- disable interrupts, lock memory bus for
- multiprocessing environment
- enter and execute critical section
- enable interrupts unlock memory bus
- other code
- Issues Noncompeting processes will be delayed
- No protection from programmer errors
- users should not be able to disable interrupts
11Compare Swap
- int compare_and_swap (int word, int testval, int
newval) /atomic instruction / - int oldval
- oldval word /oldval is a temp for
swap/ - if oldval testval) word newval
- return oldval
- / word is only updated to newval (providing a
lock) if the test value is equal to oldval /
12compare_and_swap is a hardware instruction for
generalized atomic test and set (text p. 211)
13Comments on test and set instructions
- Each shared resource has its own bolt (lock)
- Appropriate for multiprocessing
- Processes spin on the lock (bolt) if resource is
in use - A busy wait
- Neither fairness nor bounded execution (nor
freedom from deadlock) are handled
14Semaphore (E. Dijkstra)
- Value integer
- Binary semaphores have values 0 and 1 (mutual
exclusion) - Counting semaphores may be nonnegative or general
(cooperation) - Assume there is a queue associated with each
semaphore - Most general usage of semaphores
- Operations Initialize, wait, signal
- wait (Semaphore S) /atomic test and set/
- if (S.count is equal to 0) suspend on S.queue
/resource is locked/ - else decrement S.count
-
- signal (Semaphore S) / unlock /
- if (S.queue not empty) awaken first process
on queue - else increment S.count by 1
-
15Semaphores
- Advantages
- Provides consistent locks for critical section if
coded correctly and memory bus is locked - Solves busy waiting for user processes if
suspension is supplied - Provides bounded service to critical section if
queue is supplied - Applicable as a cooperation mechanism
- Implementation as mutex lets mutex be freed by
locker - Disadvantages
- If programmer forgets to lock, inconsistent
resource state can occur - If programmer forgets to unlock, dead states can
occur - Programmer may be able to unlock resource causing
inconsistency - If two or more resource locks are in the wrong
order and interleaved, resource deadlock can
occur - Low level structure not user friendly
16Producer/ consumer problem
- Producer()
- produce (item) place_in_queue (item)
- Consumer()
- retrieve_from_queue (item) use_up(item)
- Mutual Exclusion producer and consumer are
placing or retrieving item in a shared buffer.
Ptrs, counts of item must be consistently
updated - Cooperation consumer should be suspended if
buffer is empty and awakened by producer when
items are inserted. - If buffer is bounded, producer is suspended when
buffer is full and awakened by consumer when
there is room to store more items. - Well look at unbounded buffer problem.
17Producer-Consumer p. 224
18Readers-Writers problem
- We assume that multiple readers can be in the
critical section at the same time but that only
one writer at a time can be in the critical
section concurrently. - We want to allow multiple readers in their
critical section (obviously we could use binary
semaphores so that either a single reader or
writer is in the critical section) - Three different solutions
- 1) readers have higher priority than writers
- 2) writers have higher priority than readers
- 3) bounded waits
- see http//alpha.fdu.edu/levine/conflict.txt
- Note that VMS Locking Manager and Solaris
(rwlock) provide readers/writers (and other kinds
of) locks
19Readers/writers readers have priority
p.240check the increment of readcount in your
text
20Writers have priority
21 Dining Philosophers
- do
- wait (chopstick i) /if chopstick is in use,
wait else take (lock) it / - wait (chopstick i1 5)
- eat
- signal (chopsticki)
- signal (chopstick i1 5 )
- think
- while (true)
- Resource Deadlock will occur if each philosopher
picks up left chopstick at same time. - Prevent deadlock by having any one philosopher
request and obtain chopstick i1 before it can
request chopstick i - Or allow only 4 philosophers to compete at a
time. - Or require picking up 2 chopsticks or none
(enables indefinite waits) - http//www.cs.mtu.edu/shene/NSF-3/e-Book/MUTEX/DI
AGRAM-philosopher.jpg
22Monitors
- Higher level construct
- Directly leads to server concept/ object concept
- Resource is encapsulated in monitor
- Data and all operations on data are inside of
monitor - Guarantees lock and unlock
- Consistent use of resource
- Correct use of mutual exclusion construct
- If queue is used for access to monitor, bounded
wait is guaranteed (assuming no errors, no
deadlocks) - Note that only one process is allowed in monitor
at a time - Monitor programmer can limit the critical section
to minimum size - Resource deadlock is still possible with nested
monitor calls - Correct use is guaranteed if monitor controls all
access to resource
23Producer/consumer (bounded buffer) using
monitors p. 229
24Message-Passing Systems
- Necessary in distributed environments
- We do not want most network users to have direct
access to our local memory - There is overhead in message passing, but it is
safer (there would also be overhead for a network
user to access our local memory) - Services provided to user
- Send
- Receive
- Possibly connection establishment, disconnect
25Issues in message passing systems
- Send by copy data are copied into receivers
area - Variable size messages (or fixed size)
- Overhead in delimiting message size
- Danger of overflowing buffer
- But fixed size typically requires fragmenting and
reassembly - Blocking or non-blocking send or receive
- Can be used for synchronization
- Buffering
- Size 0 (equivalent to blocking)
- Bounded or unbounded buffer
- Addressing
- Direct or indirect
26Message-Passing Systems
- Direct communication link between 2 processes
- Q Send (P, message)
- P Receive (Q, message) or
- Receive (id, message)
- Indirect communication
- Mailboxes (called ports in Windows)
- Dedicated, buffer with ID
- Message copied into mailbox for receiver when
ready - Mailbox can be shared by multiple processes or
only for single process - Possible competition over receipt of message
27Mailboxes (continued)
- OS provides services to be able to create,
delete, send and receive messages - Many to one(excalls to server)
- One to many (ex broadcasting)
- Many to many (ex general mail groups)
28Question
- What is an advantage of message passing?
- It is more efficient than shared memory.
- It is safer than shared memory.
- It is easier to use reference parameters.
- There is no support for shared memory.
- It is safer than shared memory.
29Synchronization in Solaris 2
- Adaptive mutex used similar to hardware (compare
and swap) - Implemented by spin on a lock wait is expected
to be short - Some threads cannot be suspended
- The process that wakes up the suspended
processes, for example - Kernel processes typically spin on a lock
- Semaphores and condition variables for suspension
- readers/writers locks to allow multiple readers
- Turnstiles to support locks provided to lock
owners - Queue maintained by thread that owns lock
kernel chooses next thread to run - Priority inversion is prevented for high priority
process waiting for lock lock holders priority
is increased to waiting threads priority (if it
is higher) until holding thread releases lock
(this is priority inheritance)
30Synchronization in Windows
- Multiple handles to same event are possible
- Spinlocks, critical regions are used for short
code, kernel processes - Interrupts are masked if spinlocks are used
- Dispatcher objects used for user applications
- Events for synchronization (condition variables)
- Thread is suspended waiting for condition to
occur - Mutex used for locks
- Thread is suspended if a call to mutex is blocked
- Thread is blocked while waiting for the mutex
lock to be released
31Atomic transactions in databases
- Serializability is the standard
- Entire transaction must complete
- abortion or failure in the middle must be handled
- Ex transfer money from one account to another
- Logs kept in nonvolatile, stable (independent,
redundant) storage - start logs
- update logs
- Update data
- commit to log
- Cost of double writes
- Checkpoints
- Periodic backups
- Synchronization to allow recovery to proceed from
checkpoint - Efficiency considerations for all of the above
32Question
- Which of the following processes should be
allowed to use a spinlock? - a) windows explorer
- b) vi editor
- c) a dispatcher
- d) grep
- e) a keyboard ISR
- Answers c and e
33Question
- Which type of process should be suspended while
waiting for an event or for a lock to be released
user or kernel process? - Answer user process
34Question
- Binary semaphores are primarily used as
- Cooperation mechanisms
- Competition mechanisms
- Answer competition mechanisms.
- The binary value serves as a lock typically 0
is the value for the locked state.