Reader-writer Problems

Source: Internet
Author: User

Philosophers are very useful for modeling the problem of mutual access to limited resources (such as I/O devices) by multiple competitive processes. Another famous issue is the reader-writer problem, which creates a model for database access. For example, imagine an aircraft ticketing system in which many competing processes attempt to read and write data. It is acceptable to read multiple processes at the same time. However, if a process is updating the database, all other processes cannot access the database, even if the read operation fails. The question here is: how to program readers and writers? Figure 2-19 shows a solution.

 
Typedef int semaphore; semaphore mutex = 1;/* control RC access */semaphore DB = 1;/* control database access */INT rc = 0; /* Number of processes being read or to be read */void reader (void) {While (true) {/* infinite loop */down (& mutex ); /* deny access to rc */rc = RC + 1;/* adds another reader */If (rc = 1) Down (& dB ); /* if this is the first reader, then ...... */up (& mutex);/* restore access to rc */read_data_base ();/* Access Data */down (& mutex ); /* deny access to rc */rc = RC-1;/* the reader has a missing */If (rc = 0) Up (& dB ); /* if this is the last reader, then ...... */use_data_read ();/* non-critical operation */} void writer (void) {While (true) {think_up_data (); /* non-critical operation */down (& dB);/* reject access */write_data_base ();/* modify data */up (& dB ); /* restore access */} Figure 2-19 solutions to a problem between readers and authors

The first reader executes down on the semaphore dB. Then the reader only increments the RC counter. When the reader leaves, they decrease the counter, and the last reader executes up on the DB, so that a blocked writer can access the database.

Imagine that when one reader is using the database, another reader also accesses the database. Because multiple readers are allowed to perform read operations at the same time, the second reader is allowed to access the database, similarly, the third and later readers are allowed to access the website.

Now let's assume that a writer arrives. Because write operations are exclusive, it cannot access the database, but is suspended. Later, other readers will be allowed to access the database as long as one reader is active. The result is that as long as there are readers, they will be allowed to enter, and the writer will be suspended until there is no reader. If a reader comes to the database every two seconds and the operation time is five seconds, the writer will never be able to access the database, or we say the writer is hungry. This is the preferred solution for readers.

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.