Locks Flashcards
What is multi-programming?
Doing multiple tasks at the same time using multiple processes.
How does multi-programming differ from multi-threading in terms of data transfer?
Multiprogramming uses IPC for processes to communicate with each other. Multithreading shares the memory space.
What are the pros and cons of multiprogramming?
Pros: Memory safety protection provided by the OS: since each process runs independently without interfering with the memory space of other processes, there is no risk of accidentally modifying or overwriting someone else’s data. There is also no risk of race condition.
Cons: IPC is difficult to set up
What are the pros and cons of multithreading?
Pros: Easy communication
Cons: Racing conditions leading to hard to debug errors. There are also errors like atomicity violation, deadlocks, starvation, etc.
When we use multithreading, when does the order of scheduling cause an issue?
When the threads work on shared data
Define race condition.
The output of concurrent program depends on the order of operations between threads
What is an approach we can use to combat race condition?
Using atomic operations to ensure cooperation between concurrent threads. This is also called synchronization.
What is an atomic operation?
An operation that either does not run or runs to completion
What does indivisible mean for an atomic operation?
It means the operation cannot be stopped in the middle of running.
If there are no atomic operations, can threads still work together?
No
Give two examples of atomic operations.
Load and store
Describe mutual exclusion.
It ensures that only one thread works on a particular task at a time.
What is critical section?
It is a piece of code that only one thread can execute at a time. If we have mutual exclusion, we have a critical section. It is the shared resources such that the OS cannot interrupt when we execute this section.
What is a lock?
A lock prevents someone from doing something. Before a thread enters a critical section (accessing shared data), it should lock the task. Once it is done, unlock before leaving. If a task is locked, wait.
Does synchronization involve waiting?
Yes
What are the two correctness properties? In terms of “too much milk” and in general.
- Someone buys if needed
- Never more than one person buys
- Someone works on the task if needed
- Never more than one worker executing the same task
Explain why the basic strategy fails: if there is no milk and no note, leave a note and go buy milk. Remove note after buying.
After person 1 checked there is no milk, get context switched to person 2, who also checked there is no milk and no notes. Then switch back to person 1, who sees no note, leaves note and goes to buy milk. Then person 2 also leaves note and goes to buy milk.