Locks Flashcards
(43 cards)
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.
What happens if we leave a note before checking if there is milk or a note?
Starvation. Then there is no point in checking the note because it will always be there. So no one will ever buy milk.
Describe using labelled notes.
Before A does anything, it leaves a note A (so if at any point it gets switched to B, B will see that A is on it, so B will not go buy milk). Then A checks for B’s note and check if there is milk. If there is not, A goes to buy. B does the same thing.
Will using labelled notes still lead to starvation?
It is possible. If A left its note and before it could check for B’s note, it gets context switched to B. B also leaves its note and gets switched back to A. Then they both see each other’s note and remove their own.
What is a solution that works? Explain.
To use a while loop on oner person to keep checking the other person’s note. The person with the while loop has to keep waiting. For B, if there is no note from A, it is safe for B to buy. If there is a note, B just removes its note. For A, it keeps waiting for B to quit so there is no note. Then A can check if there is milk and decide what to do.
What is the while loop named?
Busy waiting loop
Why is the solution with a busy waiting loop not ideal?
A’s code is different from B’s. This becomes difficult when there are lots of threads and each of them has a slightly different code. It wastes CPU time since A is just waiting. It is also very complex and hard to convince one that it works without loopholes.
What is the best solution for the milk problem?
Use “lock.Acquire()” to acquire a task and use “lock.Release()” to release once done.