Analysis of P and V operation examples-reader-Writer Problem-sleep barber Problem

Source: Internet
Author: User

Question 1: readers

Level 1: Only readers are allowed. A maximum of K readers are allowed to write programs using P and V Operations.

int main(){    int rspace = k;    cobegin        read_1();        read_2();        ……        read_n();    coend}read_i(){    p(rspace);    ……    v(rspace);}

In this operation, you need to set the signal rspace to determine whether the number of people has reached K. When the number of people reaches K, rspace = 0. In this case, you need to suspend.

Level 2: One reader and one writer.

int main(){    int mutex = 1;    cobegin        read_i();        write_i();    coend}read_i()//i = 1, 2, ……{    p(mutex);    ……    v(mutex);}write()//i = 1, 2,  ……{    p(mutex);    ……    v(mutex);}

This is a critical resource. For critical resources, you need to set the signal mutex to control only one process for access.

Level 3: There are multiple readers and multiple writers.

Int main () {int mutex = 1; // This is the mutex between read and write int mutex1 = 1; // This is to protect variables such as CNT cobegin read_ I (); write_ I (); coend} read_ I () // I = 1, 2 ,...... {P (mutex1); CNT ++; If (CNT = 1) {P (mutex) ;}v (mutex1 );...... P (mutex1); CNT --; If (CNT = 0) {v (mutex);} V (mutex1);} write () // I = 1, 2, ...... {P (mutex );...... V (mutex );}

This resource, mutex for read and write, uses mutex1 to protect CNT critical resources. // Question: Does mutex itself need no protection? How can I ensure that the mutex value is not modified at the same time?

Level 4: there are multiple readers, up to k readers, and multiple writers.

Int main () {int mutex = 1; // This is the mutex between read and write int mutex1 = 1; // This is the protection of CNT and other variables int CNT = 0; int rspace = K; cobegin read_ I (); write_ I (); coend} read_ I () // I = 1, 2 ,...... {P (mutex1); P (rspace); CNT ++; If (CNT = 1) {P (mutex) ;}v (mutex1 );...... P (mutex1); CNT --; If (CNT = 0) {v (mutex);} V (rspace); V (mutex1);} write () // I = 1, 2 ,...... {P (mutex );...... V (mutex );}

This must be ensured by rspace = K, that is, when the critical value is rspace = 0, it is the critical value.

Question 2: token copy

Problem:

Due to the slow input speed between the printer and the card player (the previous input device), if one input and one output speed is slow, two buffers are set up to shorten the time.

Set the original time: enter a character T1 and print a character T2. Then, the total time for a character from input to output is t1 + T2, and the optimized time is max (t1 + T2 );

// Input F, output Gint main () {Get (S, T); While (incomplete copying) {T = s; cobegin get (S, f); Put (t, g); coend }}

The key to this question is:

1. First, get (S, T) should ensure that the content in the S buffer needs to be used at the beginning, and the content in the T buffer is not used.

2. t = s is a value assignment operation, which is extremely fast and negligible. Therefore, the time depends on max (t1 + T2 );

Question requirements:

N there is a barber in the barber shop, a barber chair, and N chairs for the rest of the customers waiting for a haircut; if there are no customers, the barber will go to bed in the barber chair. When a customer arrives, he wakes up the barber; if a new customer arrives while the barber is in the haircut, then if there is still an empty chair, the customer will sit down and wait; otherwise, he will leave the barber shop. Step 1: Barber: if a customer has a haircut, the customer does not have a haircut. The critical condition is whether there are any customers. Therefore, the number of customers CNT = 0; custermer: If there is no empty location, the customer will go away. If there is no empty location, there will be no such empty Location status, therefore, there are only judgment statements without critical values.
Int main () {int CNT = 0; // number of customers cobegin Barber (); customer (); coend} Barber () {While (1) {P (CNT ); ...... } Customer_ I () // 1, 2 ,...... {If (CNT <n + 1) V (CNT );}

Problem: At this time, CNT exists in the combination of customer and Barber, so it may happen that custermer and barber change CNT together, therefore, you need to set a variable mutex to protect the mutex operation of the variable CNT. Step 2:

Int main () {int CNT = 0; // number of customers int mutex = 0; cobegin Barber (); customer (); coend} Barber () {While (1) {P (mutex); P (CNT); V (mutex );...... } Customer_ I () // 1, 2 ,...... {P (mutex); If (CNT <n + 1) V (CNT); V (mutex );}
Question 2:/* for each custemer, the entire program should be a process of communication between each other. custermer-> barber-> custermer. After a haircut, custermer can kill the process, instead of applying to immediately kill the process. Therefore, there is a communication variable waiting for Barber. */For this question, let's take a look at the working principles of P and V Operations. P (k) operation: IF k> 1, the request is successful. If K <1, the request fails and the value of K is reduced by 1, link the process to the wait queue of K. V (k) operation: release a resource represented by K. If K is <0, (assuming K =-M, M indicates the number of processes in the waiting queue) at the same time, retrieve the wait and wait for a process in the queue to continue. In short, the P operation applies for resources, and the V Operation releases resources. To be continued.

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.