Lecture 7 - Concurrency and Synchronisation 1 Flashcards
What is concurrency?
Multiple or things being executed at the same time
What is the difference between real and simulated (apparent) concurrency?
Real concurrency: multiple CPUs genuinely executing things at the same time
Apparent concurrence: one CPU using scheduling to give the illusion of concurrency
What is the benefit of concurrency?
Increases performance
What is the downside to concurrency?
Not all resources can be accessed by multiple threads at the same time
When is mutual exclusion needed?
When some resource can only be used by one thread at a time
E.g. shared memory structures, files, …
When must synchronisation be used?
When multiple threads want to access the same object at the same time - critical sections
What are the 4 conditions to provide mutual exclusion?
No two processes can simultaneously be in critical section
No assumptions made about speeds or numbers of cpus
No process running outside its critical sections may block another process
No process must wait forever to enter its critical region
Only critical regions that access __________ need to synchronize with each other
The same resource
Synchronisation for threads has the same pattern. What is it?
Attempt to enter critical section
Do stuff in critical section
Exit critical section
Do all non critical stuff
How does strict alternation work to protect critical sections?
Whats the problem with it?
Use a shared variable to indicate which thread/process is currently in critical section
Avoids race conditions but can just get stuck
How does the Bakery algorithm work to protect critical sections?
Before entering critical section, process receives a number
Numbering scheme always generates numbers in increasing order
Holder of smallest number enters critical section
If two processes receive same number, lower pid is served first
How do locks work?
Create a lock associated with shared resource
Lock can be locked or unlocked:
locked - some thread is accessing the resource
Unlocked - not held by any thread
How do locks prevent race conditions?
Allow only one thread at a time to use the shared item
Whats the problem with locks?
Can reduce concurrency - may have several threads waiting for the one shared resource to become available
What are the expected API operations of a lock?
Create new lock
Lock a lock
Unlock a lock
Declare variable as lock
Possibly:
Test if lock is lockable
Destroy lock to reclaim memory