Problem:
Some readers and writers read and write the same blackboard. Multiple readers can read the blackboard at the same time, but only one writer can at a time. Readers cannot use the blackboard at the same time. Different requirements on blackboard priority enable readers-to-writers to fall into several categories. The first type of problem specifies that the reader has a high priority and only allows the writer to use the blackboard when there are no readers.
P, V primitive implementation:
Process: writer process reader Process
Define variables:
Mutex = 1; // blackboard Resource
Reader_mutex = 1; // controls the mutex between readers
R_count = 0; // count the number of readers
// Reader-reader process {while (true) {p (reader_mutex); // controls the reader's mutex to prevent two readers from executing reader_count ++ at the same time, while reader_count only adds 1; reader_count ++; // Add 1 to the number of readers who want to use the blackboard; if (reader_count = 1) p (mutex); // if it is the first reader, apply for a blackboard; v (reader_mutex); // release the variable so that the next reader can read the blackboard; read (); // read the blackboard; p (reader_mutex); reader_count --; // after reading the blackboard, the number of readers should be reduced by 1, and mutual exclusion should also be considered; if (reader_count = 0) v (mutex ); // if it is the last reader, release the resources on the blackboard; v (reader_mutex );}
// Writer process {while (true) {p (mutex); // apply for Blackboard resources; write (); // write; v (mutex ); // release blackboard resources ;}}
This article from the "technology lies in persistence" blog, please be sure to keep this source http://minilinux.blog.51cto.com/4499123/1275337