Docsity

Understanding Message Passing and Deadlock in Operating Systems - Prof. Jonathan Walpole, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
1
A common project 2 error
method Lock ()
var
oldIntStat: int
oldIntStat = SetInterruptsTo (DISABLED)
if mutex == 0
mutex = 1
heldBy = currentThread
oldIntStat = SetInterruptsTo (oldIntStat)
else
sleepThreads.AddToEnd(currentThread)
currentThread.Sleep()
oldIntStat = SetInterruptsTo (oldIntStat)
endIf
endMethod
method Unlock ()
var
oldIntStat: int
t: ptr to Thread
oldIntStat = SetInterruptsTo (DISABLED)
mutex = 0
if sleepThreads.IsEmpty() == false
t = sleepThreads.Remove()
t.status = READY
readyList.AddToEnd(t)
endIf
oldIntStat = SetInterruptsTo (oldIntStat)
endMethod
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e

Partial preview of the text

Download Understanding Message Passing and Deadlock in Operating Systems - Prof. Jonathan Walpole and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

A common project 2 error method Lock ()var

oldIntStat: intoldIntStat = SetInterruptsTo (DISABLED)if mutex == 0mutex = 1heldBy = currentThreadoldIntStat = SetInterruptsTo (oldIntStat)elsesleepThreads.AddToEnd(currentThread)currentThread.Sleep()oldIntStat = SetInterruptsTo (oldIntStat)endIf endMethod

method Unlock ()var

oldIntStat: intt: ptr to ThreadoldIntStat = SetInterruptsTo (DISABLED)mutex = 0if sleepThreads.IsEmpty() == false

t = sleepThreads.Remove()t.status = READYreadyList.AddToEnd(t) endIfoldIntStat = SetInterruptsTo (oldIntStat) endMethod

Quiz^ ‰^

What is the difference between a monitor anda semaphore?^ ™^

Why might you prefer one over the other?

‰^

How do the wait/signal methods of a conditionvariable differ from the wait/signal methods ofa semaphore?

‰^

What is the difference between Hoare andMesa semantics for condition variables?^ ™^

What implications does this difference have forcode surrounding a wait() call?

Producer-consumer with message passing^ ‰^

The basic idea:^ ™^

The producer sends the data to consumer in amessage

™^

The system buffers messages^ •^

The producer can out-run the consumer • The messages will be kept in order

™^

But how does the producer avoid overflowing thebuffer?^ •^

After consuming the data, the consumer sendsback an “empty” message

™^

A fixed number of messages (N=100)

™^

The messages circulate back and forth.

Producer-consumer with message passing^ thread consumer

var c, em: charwhile true

Receive(producer, &c)

-- Wait for a char

Send(producer, &em)

-- Send empty message back

// Consume char... const N = 100^ endWhileend

-- Size of message buffer

var em: charfor i = 1 to N

-- Get things started by

Send (producer, &em)

sending N empty messages

endFor

Design choices for message passing^ ‰^

Option 1:

Mailboxes

™^

System maintains a buffer of sent, but not yetreceived, messages

™^

Must specify the size of the mailbox ahead of time

™^

Sender will be blocked if the buffer is full

™^

Receiver will be blocked if the buffer is empty

Design choices for message passing^ ‰^

Option 2:

No buffering

™^

If Send happens first, the sending thread blocks

™^

If Receive happens first, the receiving threadblocks

™^

Sender and receiver must Rendezvous (ie. meet)

™^

Both threads are ready for the transfer

™^

The data is copied / transmitted

™^

Both threads are then allowed to proceed

Resources and deadlocks^ ‰^

Processes need access to resources in order to make progress

‰^

Examples of computer resources^ ™^
printers

™^

tape drives

™^

kernel data structures (process & file table entries …)

™^

locks/semaphores to protect critical sections

‰^

Suppose a process holds resource A and requests resource B^ ™^
at the same time another process holds B and requests A

™^

both are blocked and remain so … this is deadlock

Resource usage model ‰^

Sequence of events required to use a resource ™

request the resource (like acquiring a mutex lock)

™^

use the resource

™^

release the resource (like releasing a mutex lock)

‰^

Must wait if request is denied ™

block

™^

busy wait

™^

fail with error code

Definition of deadlock^ A set of processes is deadlocked if each^ process in the set is waiting for an event that only^ another process in the set can cause^ ‰^

Usually the event is the release of a currently heldresource

‰^

None of the processes can …^ ™^
be awakened

™^

run

™^

release resources

Deadlock conditions^ ‰^

A deadlock situation can occur

if and only if the following

conditions hold simultaneously^ ™^
Mutual exclusion condition – resource assigned to oneprocess

™^

Hold and wait condition – processes can get more thanone resource

™^

No preemption condition

™^

Circular wait condition – chain of two or more processes(must be waiting for resource from next one in chain)

Resource acquisition scenarios^ Thread A:

acquire (resource_1) use resource_1 release (resource_1)

Another Example:^ var

r1_sem: Semaphore

r1_sem.Signal()...r1_sem.Wait() Use resource_1 r1_sem.Signal()

Resource acquisition scenarios

acquire (resource_2) use resource_2 release (resource_2)

Thread A:

Thread B:

acquire (resource_1) use resource_1 release (resource_1)

Resource acquisition scenarios: 2 resources

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

Thread A:

Thread B:

Resource acquisition scenarios: 2 resources

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

acquire (resource_1) acquire (resource_2) use resources 1 & 2 release (resource_2) release (resource_1)

Thread A:

Thread B:

No deadlock can occur here!