Classic operating system problems-read/write problems (Reader preferred)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.