4. Synchronization Flashcards
What do threads do?
Threads abstract and multiplex the CPU
How does the OS create the illusion of concurrency?
The OS quickly switches the processors between multiple threads of execution
Why is the illusion of concurrency both powerful and useful?
It helps us to think about how to structure our applications, and it hides latencies caused by slow hardware devices (like waiting for keyboard input).
Briefly define the coordination problem that multi-threaded concurrency creates.
How do we enable efficient communication between the multiple threads involved in coordinating a single task?
Briefly define the correctness problem that multi-threaded concurrency creates.
How do we ensure that shared state (memory, files) remains consistent when being accessed by multiple threads concurrently?
What is the difference between concurrency and parallelism?
Concurrency is about dealing with lots of things at once (independently executing processes).
Parallelism is about doing lots of things at once (simultaneous execution of possibly related computations).
For an OS to achieve concurrency, what must be by default true about threads?
They must be able to be run in any order. They must be allowed to be stopped and restarted at any time. They must be able to remain stopped for any amount of time.
This is all assuming that no explicit synchronization techniques have been employed. Example: protecting a critical region with a lock, making it impossible for another thread to execute that section while another thread is within it.
What is a race condition?
When the output of a process is unexpectedly dependent on timing or other events
Ex: A condition that occurs when two or more threads can access shared data and try to change it at the same time. Threads can be started and stopped at any moment, so it is possible that one thread checks a piece of memory for a condition, but before executing its desired operation to/with that data, it is stopped and another thread changes that data first. Now the initial check that the first thread made is incorrect, but the thread does not know that. In short, a race condition exists when the unknowable order of access that threads make to a shared resource impacts the outcome of execution.
What is concurrency?
The illusion that multiple things are happening at once. (Requires stopping or starting any thread at any time)
What is atomicity?
The illusion that a set of separate actions occurred all at once.
How is atomicity achieved?
This requires not stopping certain threads at certain times or not starting certain threads at certain times.
i.e. providing some limited control to thread over their scheduling
What is a critical section?
A critical section contains a series of instructions that only one thread can be executing at any given time
This set of instructions will look atomic with respect to other threads executing code within the critical section
What is mutual exclusion?
Only one thread should be executing in the critical section at one time.
What is progress?
All threads should eventually be able to proceed through the critical section
What is performance (as it relates to a critical section)?
We want to keep critical sections as small as possible (to improve performance) without sacrificing correctness