pr() while true buff = p() co() while true c(buff) |
data buff main() run_thread(pr) run_thread(co) |
buff
at any time.
There is always at most one thread accessing a resource.
There is at most one thread executing any code in the critical section.
begin_cs()
and end_cs()
system calls.
pr() while true begin_cs() buff = p() end_cs() | co() while true begin_cs() c(buff) end_cs() |
pr()
can be writing buff
or co()
can be reading it.
begin_cs() interrupt_disable() end_cs() interrupt_enable()
interrupt_enable()
and interrupt_disable()
are usually
privilidged assembly instructions.
begin_cs()
call with an end_cs()
call.
pr()
begin_cs()
buff = p()
if error
return
end_cs()
pr()
begin_cs()
buff = disk_read()
end_cs()
pr()
while true
begin_cs()
| pr() while true b = p() begin_cs() buff = b end_cs() |
p1() begin_cs() print(f1) end_cs() p2() begin_cs() print(f2) end_cs() |
s1() begin_cs() f1 = scan() end_cs() s2() begin_cs() f2 = scan() end_cs() |
p
1 can't access the printer when s
2 is
accessing the scanner.
p1() begin_cs() print(f1) end_cs() | s2() begin_cs() f2 = scan() end_cs() |
p1() begin_cs(pid) print(f1) end_cs(pid) p2() begin_cs(pid) print(f2) end_cs(pid) |
s1() begin_cs(sid) f1 = scan() end_cs(sid) s2() begin_cs(sid) f2 = scan() end_cs(sid) |
There is at most one thread executing any code associated with the identified critical section.
data buff bool empty = true pr() while not empty { } begin_cs() buff = p() end_cs() empty = false | co() while empty {} begin_cs() c(buff) end_cs() empty = true |
while not empty { } begin_cs() buff = p() end_cs() empty = false
is known as spin locking.
while not empty { }
semaphore(bool)()
create a semaphore containing a stone when
called with true or not when called with false.
bool grab()()
returns when the stone is in the bowl and the
calling thread has managed to grab it.
put()()
returns the stone to the bowl.
semaphore full(false), empty(true) data buff pr() while true full.grab() buff = p() empty.put() co() while true empty.grab() c(buff) full.put()
begin_cs(sema_id) stone = true deque a waiting thread, if any, and make it ready. end_cs(sema_id)
try()
is a non-blocking grab()
; it returns true if the calling
thread managed to grab the stone; false otherwise.
release()
is similar to put()
: it puts the stone back in the
bowl, but unblocks all waiters rather than just one.
grab()
function has to be re-implemented to use a loop if
the semaphore provides release()
.
notifyAll()
.
waiters()
returns the number of threads waiting to grab for the
stone.
get()
on the associated semaphore.
put()
.
This page last modified on 14 November 2004.