Lecture 12: Synchronization 3 - Semaphores Flashcards

1
Q

Goals for Solving Critical Sections

A
  1. Mutual Exclusion
    no more than 1 process in its critical section at a time
  2. Progress
    cannot keep postponing scheduling process into its critical section if no other is in its critical section
  3. Bounded wait
    once a process requests entering critical section, there must be a bound on # of other processes allowed to enter their critical section before granted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Clever Algorithms

A
  1. Strict Alternation
  2. After You
  3. Peterson’s Algorithm
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Strict Alternation

A

Threads take turns entering C/S

Mutex? Yes
Progress? No
Bounded-wait? Yes

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

After You

A

Threads enter C/S when no others want to

Mutex? Yes
Progress? No
Bounded-wait? No

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

Peterson’s Algorithm

A

Combines Strict Alternation and After You algorithms. Thread enters C/S when its turn, or thread whose turn it is not interested

Mutex? Yes
Progress? Yes
Bounded-wait? Yes

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

Disabling Interrupts

A

+: Easy to see that it works

–: Overkill

  1. Disables all interrupts (e.g.: I/O)
  2. Disallows concurrency with threads in non- critical sections

–: Doesn’t work for multicore systems
(Each CPU has own interrupts: threads on different
CPU’s can access their critical sections at same time)

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

Test and Set

A
  • New AL instruction (∴ Atomic)

- Can be used to implement “locking”

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

Semaphores

A

Data structure consisting of lock with wait queue

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

Semaphore variations

A

Binary Semaphore: one thread can hold lock at a time (n = 1) good for critical sections

Counting Semaphore:n threads can hold lock at a time (good for other synchronization problems)

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

Binary Semaphores and Critical Sections: Why so much simpler than Peterson’s algorithm?

A

• devil is in the details
must ensure that no two threads can execute acquire, release on
same semaphore at the same time (shared counter, cnt)

• C/S Problem moved to semaphore implementation! Will Revisit After Talking About Monitors

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

Bounded-Buffer Problem

A
  1. Shared variable
    buffer [N]: shared by all
    in: shared by all producers
    out: shared by all consumers
  2. Full buffer:
    ensure that producer doesn’t try and insert into (instead wait until not full)
  3. Empty buffer:
    ensure that consumer doesn’t try to consume from (instead wait until not empty)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Bounded Buffer Problem: Use of Semaphores

A
  1. mutex
    • binary (initialized to 1)
    • ensures exclusive access to code reading, writing in, out, buffer
  2. empty
    • counting (initialized to buffer size, N)
    • when = 0, no empty slots. Begins queueing producers
  3. full
    • counting (initialized to 0)
    • when = 0, no full slots. Begins queueing consumers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly