Locks Flashcards

1
Q

What is multi-programming?

A

Doing multiple tasks at the same time using multiple processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does multi-programming differ from multi-threading in terms of data transfer?

A

Multiprogramming uses IPC for processes to communicate with each other. Multithreading shares the memory space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the pros and cons of multiprogramming?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the pros and cons of multithreading?

A

Pros: Easy communication

Cons: Racing conditions leading to hard to debug errors. There are also errors like atomicity violation, deadlocks, starvation, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When we use multithreading, when does the order of scheduling cause an issue?

A

When the threads work on shared data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define race condition.

A

The output of concurrent program depends on the order of operations between threads

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an approach we can use to combat race condition?

A

Using atomic operations to ensure cooperation between concurrent threads. This is also called synchronization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an atomic operation?

A

An operation that either does not run or runs to completion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does indivisible mean for an atomic operation?

A

It means the operation cannot be stopped in the middle of running.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If there are no atomic operations, can threads still work together?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give two examples of atomic operations.

A

Load and store

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe mutual exclusion.

A

It ensures that only one thread works on a particular task at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is critical section?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a lock?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Does synchronization involve waiting?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two correctness properties? In terms of “too much milk” and in general.

A
  1. Someone buys if needed
  2. Never more than one person buys
  3. Someone works on the task if needed
  4. Never more than one worker executing the same task
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens if we leave a note before checking if there is milk or a note?

A

Starvation. Then there is no point in checking the note because it will always be there. So no one will ever buy milk.

19
Q

Describe using labelled notes.

A

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.

20
Q

Will using labelled notes still lead to starvation?

A

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.

21
Q

What is a solution that works? Explain.

A

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.

22
Q

What is the while loop named?

A

Busy waiting loop

23
Q

Why is the solution with a busy waiting loop not ideal?

A

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.

24
Q

What is the best solution for the milk problem?

A

Use “lock.Acquire()” to acquire a task and use “lock.Release()” to release once done.

25
Q

What is the code between lock.Acquire() and lock.Release() called?

A

Critical section

26
Q

What is wrong with putting threads to sleep?

A

Wasting CPU resources

27
Q

What is a naive implementation of locks?

A

When the lock is acquired, disable interrupts. When the lock is released, enable interrupts.

28
Q

What is the issue with the naive implementation of locks?

A

If we get into an infinite loop after acquiring a lock, the OS cannot interrupt to get us out. The system will stay hanging and unresponsive until it restarts.

29
Q

What are some problems of implementing locks with interrupts?

A

It does not work well on multiprocessors. On multiprocessor systems, this approach does not provide synchronization because disabling interrupts on one core does not affect others. It does nothing to stop other processors from accessing shared resources. Disabling interrupts also makes it time consuming to communicate between processors.

30
Q

What is an approach that can be used in both uniprocessor and multiprocessors?

A

Atomic read-modify-write instructions

31
Q

Who offers doing these instructions atomically?

A

The hardware

32
Q

test&set is an example of read-modify-write instructions. Describe how it works.

A

1) Set a variable lock to 0 if the lock is free, 1 if the lock is locked.
2) Given the mem address of the lock, read the value at the address and save it somewhere, e.g. result.
3) Rewrite the value with 1 at the address. (It sets to 1 no matter what the state of the lock is)
4) Return the value we read initially
So, if we get 0 back, we acquired the lock (the lock was available when we got to it). If we get 1 back, we failed to lock the lock.

33
Q

When we use test&set, what does test&set read and set if we have a free lock?

A

It reads 0 and sets the value to 1.

34
Q

What about when we have a busy lock?

A

It reads 1 and sets the value to 1.

35
Q

How do we determine if we succeeded in acquiring the lock with test&set?

A

We look at the value it returns.

36
Q

If we implement locks with test&set, within the acquire step, we would have a while(test&set(&my_lock)) loop. What is wrong with this implementation?

A

All threads will run this code and only one thread will succeed in acquiring the lock. Meanwhile, all the other threads are kept busy waiting, consuming resources.

37
Q

What are the pros of this implementation of busy-waiting?

A

1) No blocking interrupts
2) User code can use this lock through system calls
3) Works on multiprocessors

38
Q

What are the cons of busy-waiting?

A

1) Wasting resources when threads consume cycles waiting
2) Priority inversion

39
Q

What is priority inversion?

A

Each thread has a priority and the OS expects a high priority thread to run first. If a low priority thread gets access to the resources first, the OS gives it the highest priority and waits until it finishes while no one can interrupt. Then a high priority task will be waiting for the lock to be released, but the low priority one holding the lock cannot release the resources because a medium-priority task preempts it and runs instead.

40
Q

Why is priority inversion bad?

A

1) Priority inversion disrupts the expected behavior of priority scheduling. High-priority tasks are expected to preempt lower-priority tasks, but inversion violates this principle, leading to erratic and hard-to-debug system behavior.

2) High priority tasks often have a tight deadline so busy-waiting would cause them to miss their deadlines.

41
Q

What are the other two rules of using locks? 1) Locks should be initially free. 2) Always acquire a lock before accessing shared data

A

3) Always release the lock after finishing with shared data
4) Never access shared data without a lock

42
Q

Who can release a lock?

A

The lock owner

43
Q

Should the lock owner throw the lock for others to release?

A

No, never.