Exam 2 (Concurrency and Deadlocks) Flashcards
Why do we need synchronization?
Activities share resources, and it’s important to coordinate their progress.
What type of coordination is this: John and Mary are printing 10 page documents on the same printer. We don’t want their pages to interleave!
Exclusion
What type of coordination is this: John and Mary are sharing a bank account. John deposits $10. Mary should be allowed to withdraw only after this deposit.
Ordering
Types of shared resources
Physical (terminal, disk, network, …) or Logical (files, sockets, memory, …)
What is mutual exclusion?
Think of race conditions… If two threads are accessing the same array, we need to block one from executing while the other is executing to avoid a race condition.
What is a critical section?
The set of instructions where race conditioning could occur, in the event of an array it would be Load, Add, Store.
Implementing Mutex: Disable Interrupts
Stops scheduling other threads. It’s easy to implement, but we don’t want to give that much power to users, it doesn’t work on a multiprocessor, and disables multiprogramming even when there is no critical section access.
Implementing Mutex: Problem with Software Solutions
We can have a variable to indicate a thread accessing the critical section, but this has a race condition too so it doesn’t really work!
Implementing Mutex: What we want
Mutual exclusion (correctness), Granting access to a critical section if not being used (progress), no process should have to wait forever (bounded waiting).
Implementing Mutex: Strict Alternation
Uses a variable to indicate turns. Seems to work, but requires threads to alternate, and doesn’t meet the progress requirement. We can fix this by having an array of “flags”, but both can set their flags to true and wait indefinitely.
Implementing Mutex: Peterson’s Algorithm
Each algorithm has two variables, for flag and turn. If flag is true, a process wants to enter Critical Section. This is allowed to happen when the other process doesn’t want to enter, or if the process has priority.
Implementing Mutex: Multiple Thread Solution
Use the Bakery Algorithm. Every thread has a unique id, like tickets in a bakery. They are serviced in increasing tickets, and need to use some kind of tie breaker.
Implementing Mutex: Specialized Instructions
Functions are still atomic, we can use Bool Test&Set or Swap. Parameters are passed by reference.
Test&Set
Have a boolean indicating TRUE before entering the critical section, and then set it to FALSE when finished. Machine instruction will end execution.
Swap
Swaps a key and the lock. When exiting the CS, lock becomes false.