Mod 3: Semaphores Flashcards

1
Q

What is “busy waiting”?

A

(1) A thread is spinning in a loop waiting for a resource to become available, instead of waiting for it to be signaled
(2) Synchronization technique that can be quite wasteful of resources

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

What is a semaphore?

A

a Synchronization method

(1) Counter
(2) Abstract data type, not integer
(3) Shared between threads

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

What is a semaphore used for?

A

Semaphores are used to synchronize access to shared resources, and can solve a wide range of synchronization issues

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

Which to atomic operations does semaphores use?

A

Wait( ) (or P, Down)
Signal( ) (or V, Up)

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

How does a semaphore work?

A

It’s a counter that keeps tabs on available resources, when a thread requests access to resource the counter is decremented, and when the threads release the semaphore the counter is incremented. If the counter is negative, the thread has to wait

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

What are the attributes of a semaphore?

A

(1) Value, a integer
(2) A queue, holding blocked threads, not accessible for the programmer

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

What are the methods/operations of a semaphore?

A

(1) Initialize(s), where s is number of available resources
(2) P or Wait, called by threat who want to enter the CS
(3) V or Signal, called by thread who want to exit the CS, if the in a blocked thread, it is unblocked once the counter is incremented

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

Does a semaphore have history?

A

Yes, a semaphore remember which thread has released the ressource

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

Does the thread owns the semaphore if it uses it?

A

No, a semaphore is not owned by a thread

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

What are some real-life examples of semaphore uses?

A
  • Traffic light,
  • Parking slots in a parking lot,
  • Counters in an bank office (nbr of service ppl available)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the rule for number of semaphore required?

A

One for each constraint

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

What are the two types of semaphore?

A

(1) Counting semaphore - used for a resource with many units or when several threat are allowed to access the resource (eg. for reading)
(2) Binary semaphore - can only have value 0 (wait) or 1 (signal), used as lock mechanism or where only on thread can access a resource

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

Which values can a counting semaphore have?

A

0, and positive or negative value (-1 means no resources are available).

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

What is a binary semaphore used for?

A

Mutual Exclusion, ME

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

What are the main differences between a lock and a binary semaphore?

A

(1) Locking usually disables interrupts inside CS, Semaphore does not

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

What is the difference between a semaphore and mutex?

A

Mutex is a locking mechanism, while a semaphore is a signaling mechanism

17
Q

What is the difference between a lock and mutex?

A

Mutex can be shared by different threads, a lock cannot (a lockable object can be)

18
Q

What are the main advantages of using semaphores?

A

(1) Efficiently synchronization technique,
(2) Use CPU more efficiently

19
Q

What are the main disadvantages of using semaphores?

A

(1) Not structured
(2) Difficult to develop and maintain
(3) Programmer must have good control over calls to P( ) and V( )
(4) No connection between the semaphore and the data it controls

20
Q

What can be used instead of semaphores?

A

(1) Built-in language support
(2) Monitor