semaphore counts resources
reads usually dominate writes, eg. database
rarely, writes are way more than reads, eg. log server
so one policy is to let the writer to wait until there's no reader, and reader has high priority.
two mutexes!
'mutex' ~> for reader counting
'w' ~> for readers dominating writers
-
Question:
why these two lines should be in the critical section?
-
Answer:
the shared var "readcnt" should be protected
how does the first reader excludes writers?
symmetric!
in a stream of readers, when a writer comes in, the writer will require a 'r', to prevent further readers from running.
and 'wmutex' is a mutex to protect the 'writecnt'
circular wait
deadlock -> threads acquire resource not in the same sequence
what if 2 threads using rand
and one of them called srand
remove the static next
each srand
malloc a new next
so the programmer has to maintain the seed, and need to free the variable
没看懂???
网友评论