Week 5: Semaphores and Deadlocks Flashcards

1
Q

Semaphore

A

Semaphore is a system where the processes get “trapped” in a while loop until the condition becomes false.

The element changing the condition is a “key”.

The first process to enter the system will be able to pass through the while loop and will “lock” it by making the condition true for the following processes.

When process one finished using the resources, the “key” changes and sets the condition to false, allowing another process to pass and blocking again the while loop.

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

Name 3 practical uses of semaphores

A
  • Buffers
  • Shared files/variables
  • Limited resources with many processes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Notice:

Semaphores are also a great solution for Process Manager design

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

Describe how you would implement a file read/write system using semaphores.

A

When a process used fopen(), a semaphore would let the process pass, copy part of the file into a buffer and then let the process out and another one in. This keeps only one process while ensuring that no one is writing.

Finally, fscan() would act locally, while pointing to the buffer. Once the buffer is empty, the semaphore would kick in again, to copy the rest of the file in the buffer.

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

Bounded-Buffer Problem

A

When only one process at the time can use a resource. Also, no other process can access the resource until the first process is done.

When there is synchronization in one direction.

Ex: Sending data to a printer.

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

In your words, how would you solve this problem with semaphores?

A

Producer

Here we would need 3 semaphores: mutex, full, empty where mutex is the one that guards the critical section, empty is the one that keeps track of how empty the buffer is, and full the one that keeps track of how full the buffer is. Then, the producer would first check if there is space in the buffer and signal empty, then the first process would enter the Critical Section and finally would signal mutex and full.

Consumer

Same semaphores. First, it will check if there is something in the buffer and signal full with full-1 since the consumer is taking from the buffer, then wait(mutex) since we can’t read and write to the buffer at the same time.

Notice: mutes, empty and full are share between consumer and producer.

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

Readers-writers Problem

A
  • The resource is shared
  • We can have many processes reading at the same time
  • We cannot have many processes writing at the same time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you solve the Readers-writers problem with semaphores?

A

In this case we have 2 semaphores: wrt and mutex.

Mutex: allows access to Critical Section

wrt: keeps track is someone is writing

Writer

wait(wrt) ensures that only ONE process can write at the time

Reader

wait(mutex) protects the Critical Section which is the increase/decrease of readersCount (keeps track of the number of readers). Then, wait(wrt) checks if someone is writing and if not, keeps other processes from writing as the process reads.

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

How would you solve the Dining-Philosophers Problem with semaphores (not with monitors)?

A

TODO

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

Explain how the following image is a deadlock based on resources

A

Notice that the arrows are important.

An arrow coming from a process means “I want” whereas an arrow pointing to a process means “I am using”.

Notice that Process A wants resource 2 but Process B is using resource 2 and it will not finish using it until it gets resource 1. BUT Process A is using resource 1. So Process A and Process B will never end.

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

Request of resources

A

If a process needs a given resource it has to wait until it is available.

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

Use of resources

A

A process has dedicated access to a resource until it finishes.

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

True or False

In a deadlock, it is possible that a process in a set of processes will finish executing.

A

False

By definition, deadlock means that all the processes in a given set of processes can’t finish executing.

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

Name 4 necessary conditions to get a deadlock

A
  • Mutual exclusion
  • Hold and wait
  • No pre-emption
  • Circular wait
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

1) Is there a deadlock?

A

No,

P3 finishes, then P2, then P1

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

2) Is there a deadlock?

A

Yes

P3, P2 make a cycle

17
Q

3) Is there a deadlock?

A

No,

P2 finishes, then P1 in R1 and finishes, P3 in R2 and finishes and P4 finishes

18
Q

Kernel Design

slide 31

A