Week 10: Semaphores Flashcards

1
Q

What is a race condition?

A

A situation where the outcome depends on the sequence or timing of processes accessing shared resources.

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

How can race conditions be prevented?

A

By ensuring only one process accesses a shared resource at a time (mutual exclusion).

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

What is a critical section?

A

A part of a process that accesses shared resources

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

Why does using a critical_region flag fail?

A

A process may be interrupted between checking and setting the flag, allowing another process to enter the critical section.

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

Why does disabling interrupts fail as a solution?

A

It:
- Cannot work in multi-processor
- Risks forgetting to re-enable
- May be abused by selfish users.

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

What does strict alternation enforce?

A

Processes take turns entering their critical sections based on a shared turn variable.

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

What are the drawbacks of strict alternation?

A

It forces processes to wait unnecessarily and prevents a process from entering its critical section twice in a row.

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

What is Peterson’s solution?

A

A mutual exclusion algorithm with the following variables:
Each process has an integer id number
Two shared variables are needed:
- turn: Indicates whose turn it is.
- interested[]: Flags whether a process wants to enter its critical section.
Each process must know the other’s process id

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

Why is Peterson’s solution effective?

A

It combines lock variables with turn-taking, ensuring fairness and preventing simultaneous access.

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

What is busy waiting?

A

A process repeatedly checks a condition in a loop, wasting CPU time.

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

Why is busy waiting problematic?

A

It wastes resources and may prevent lower-priority processes from running in priority scheduling systems.

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

What is the purpose of the sleep and wakeup system calls?

A

To prevent busy waiting by blocking a process (sleep) until it is explicitly awakened (wakeup) by another process.

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

What is the difficulty with using sleep and wakeup?

A

A wakeup call can be lost if it occurs before the sleeping process actually starts sleeping, leading to deadlocks.

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

What is the producer-consumer problem?

A

A synchronization problem where a producer adds data to a shared buffer, and a consumer removes it, requiring coordination to avoid overflows and underflows.

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

How do sleep and wakeup address the producer-consumer problem?

A

The producer sleeps if the buffer is full, and the consumer wakes it up when space is available, and vice versa.

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

What is a semaphore?

A

A primitive introduced by E.W. Dijkstra, used for synchronization and mutual exclusion, with two atomic operations: down and up.

17
Q

How does down work?

A

It decrements the semaphore value if it’s greater than zero; otherwise, the process blocks.

18
Q

How does up work?

A

It increments the semaphore value and wakes up a waiting process if any are blocked.

19
Q

How are semaphores used in the producer-consumer solution?

A
  • (binary) semaphore mutex: Ensures mutual exclusion for buffer access.
  • semaphore empty: Tracks empty slots in the buffer.
  • semaphore full: Tracks filled slots in the buffer.
20
Q

Why is atomicity important for semaphores?

A

To ensure that no process can interrupt another during a semaphore operation, preventing race conditions.

21
Q

How can semaphores be implemented?

A

By disabling interrupts or using hardware instructions like test-and-set-lock (TSL).

22
Q

What is the TSL instruction?

A

A hardware instruction that atomically tests and sets a memory value, ensuring mutual exclusion during semaphore operations.

23
Q

How does TSL work?

A

It reads a memory value and sets it simultaneously, preventing other processes from accessing it until the operation is complete.