Created
July 23, 2016 04:50
-
-
Save Shengliang/644268245e0ec60cf8d5301da8d6bca8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ref: https://arxiv.org/pdf/1309.4507.pdf | |
Sempahore(n) | |
Decrement opeartion => wait | |
Increment operatorn => signal | |
mutex = sempaphore(1) | |
Classical Solution | |
init: | |
mx = sempaphore(1) | |
wrt = sempahore(1) | |
ctr = integer(0) | |
reader: | |
begin | |
wait mx | |
if (++ctr) == 1, then wait wrt | |
singal mx | |
[critical section of reader] | |
wait mux | |
if (--ctr)==0, then signal wrt | |
signal mx | |
end | |
writer: | |
begin | |
wait wrt | |
[critical section | |
signal wrt | |
Note: downsite: startvation of the writer. | |
A writer read does not have a chance to execute while any num readers continuously entering and and leaving the working area. | |
2nd Solution | |
init: | |
in = Semaphore(1) | |
mx = sempaphore(1) | |
wrt = sempahore(1) | |
ctr = integer(0) | |
reader: | |
begin | |
wait in | |
wait mx | |
if (++ctr) == 1, then wait wrt | |
singal mx | |
signal in | |
[critical section of reader] | |
wait mux | |
if (--ctr)==0, then signal wrt | |
signal mx | |
end | |
writer: | |
begin | |
wait in | |
wait wrt | |
[critical section | |
signal wrt | |
singal in | |
------------ | |
3rd Solution | |
------------- | |
init: | |
in = Semaphore(1) | |
out = semaphore(1) | |
wrt = sempahore(1) | |
ctrin = integer(0) | |
ctrout = integer(0) | |
wait = boolean(0) | |
reader: | |
begin | |
wait in | |
ctrin++ | |
singal in | |
[critical section of reader] | |
wait mux | |
ctrout++ | |
// last reader signals writer that is is safe to proceed | |
if (wait==1 && ctrin==ctrout) then signal wrt | |
signal out | |
end | |
writer: | |
begin | |
wait in | |
wait out | |
if (ctrin == ctrout) { | |
signal out | |
} else { | |
wait = 1 | |
signal out | |
wait wrt | |
wait=0 | |
} | |
critical section | |
singal in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment