(5) Synchronization Flashcards

1
Q

What are ‘Critical sections’?

A

Sequences of instructions that may get incorrect results if executed simultaneously

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

When do Critical Sections arise?

A

Read-modify-write pattern. Two threads trying to access a global/heap allocated variable (not local/stack).

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

What is a race condition?

A

If the result of the program executing depends on the timing, i.e it is non-deterministic.

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

What are the requirements for a correct critical section?

A

Mutual exclusion
(at most one thread in the critical section)

Progress
(If T outside critical section then T can’t prevent S from entering it)

Bounded Waiting
(If T is waiting for critical section it will eventually enter it)

Performance
(The overhead of entering and exiting is small)

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

How do we implement Mutual Exclusion?

A

Via hardware - special machine instructions
Via OS support - primitives via syscall
Via Software - entirely by user code

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

Give Peterson’s Algorithm

A
(Let ˆı mean 1-i)
flag[i] = true;
turn = ˆı;
while ( flag[ˆı] && turn == ˆı ){}
/* critical section */
flag[i] = false;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the different mechanisms for building critical sections?

A

Spinlocks
Semaphores
Monitors
Messages

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

What is a lock?

A

A memory object with two operations:
Acquire(): obtain right to enter CritSec
Release(): Give up right to be in CritSec

Acquire() prevents progress of thread until lock can be acquired.

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

How do we implement spinlocks?

A

‘Busy wait’ / spin in the acquire method until the lock is released. When a thread loops waiting for a mutex it is called a Spinlock. Acquire/release must be ATOMIC (executes as though it could not be interrupted)

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