Part 9 - Semaphores

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:26 AM on 5/20/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

21 Terms

1
New cards

What is a semaphore?

Semaphores control the number of user transactions that can be run in parallel. It makes sure there aren't too many things running at once. It does this by keeping an internal integer value that can be incremented or decremented and based on its value it may wake up a waiting thread.

2
New cards

What two main functions exists for interacting with semaphores?

- sem_wait()

- sem_post()

3
New cards

What does sem_wait() do?

It decrements the semaphore's integer value. If the value now is greater than 0, then it resumes. If not then it is placed in a waiting queue.

4
New cards

What does sem_post() do?

It increments the semaphore's integer value. If the integer value now is greater than 0, wake up a thread in the queue. If not, do nothing.

5
New cards

What do we call it whenever we make a semaphore only be able to have one executing thread at a time?

A binary semaphore

6
New cards

What should the initial value of a binary semaphore be?

1, because then a wait call will serve as the lock function

7
New cards

What other synchronization primitive does a binary semaphore resemble?

A lock

8
New cards

What differentiates a binary semaphore and a lock?

A lock can only be released by the owner while a semaphore might be posted to by any thread.

9
New cards

What should the initial value of a semaphore be if we want to use it like a condition variable?

0, because then the parent can wait before it is posted to

10
New cards

What is a deadlock?

A set of theads that are all waiting for an event that can only be caused by another thread in the set. Therefore it will never fire.

11
New cards

When using semaphores to solve the producer/consumer problem (bounded-buffer), what is important to keep in mind to avoid deadlocks?

To only wrap the critical section with a semaphore that functions as a mutex lock. That means that the empty/full semaphore wait needs to come before the mutex semaphore.

12
New cards

What types of locks are based on semaphores?

- Binary semaphores

- Reader-Writer locks

13
New cards

What is the purpose of a reader-writer lock?

Only allow one thread to write at a time, but allow many readers to read at a time. Also don't allow write if any readers are active.

14
New cards

How does a reader-writer lock work?

Whenever a thread wants to write, acquire the write lock. Then release when done.

Whenever a thread wants to read, acquire the read lock and increment the semaphore value. The first reader must also acquire the write lock and the last reader to finish must call sem_post() to release it.

15
New cards

What are the four needed conditions for a deadlock?

- Mutual exclusion

- Hold-and-wait

- No preemption

- Circular wait

16
New cards

What is the definition of mutual exclusion?

Theads claim exclusive control of resources

17
New cards

What is the definition of hold-and-wait?

Theads hold resources allocated to them while waiting for additional resources

18
New cards

What is the definition of preemption?

Resources cannot be forcibly removed from threads that are holding them

19
New cards

What is the definition of circular wait?

There exists a circular chain of threads such that each holds one or more resources that are being requested by the next thread in the chain

20
New cards

How can semaphores be used to throttle a thread pool?

Set the initial value of the semaphore to the max threads that should be able to work at the same time.

21
New cards

What is special about the integer value of a semaphore that some operating systems like Linux use?

The integer can never be less than 0. It doesn't care how many threads are waiting, it only cares about not too many being active at the same time.