Typical topic of Process Synchronization-producer consumer-philosopher meal-reader writer

Source: Internet
Author: User

1. producer and consumer

The producer thread produces the item and places the item in an empty buffer for consumption by the consumer thread. The consumer thread obtains the item from the buffer and then releases the buffer. When the producer thread produces an item, if no empty buffer zone is available, the producer thread must wait for the consumer thread to release an empty buffer zone. When the consumer thread consumes an item, if the buffer is not full, the consumer thread will be blocked until the new item is produced.

// Assume that the length of the buffer queue is N.
Semaphore produce_count = N;
Semaphore consume_count = 0;
Semaphore buffer_mutex = 1;
Producer (){
While (true ){
Produce a product
P (produce_count );
P (buffer_mutex );
Add the product to the buffer zone
V (buffer_mutex );
V (consume_count );
}
}
Consumer (){
While (true ){
P (consume_count );
P (buffer_mutex );
Extract a product from the buffer zone
V (buffer_mutex );
V (produce_count );
Consume this product
}
}

2. Dining by philosophers

Suppose five philosophers sat around a circular table and did one of the following two things: eating or thinking. When they eat, they stop thinking, and when they think about it, they also stop eating. There is a bowl of pasta in the middle of the dining table, with a dining cross between every two philosophers. It is difficult to use a cross to eat pasta, so it is assumed that philosophers must use two forks to eat. They can only use the two forks on their left and right sides. Philosophers never talk, which is very dangerous and may lead to deadlocks. Every philosopher holds a left-hand dining cross and is always waiting for the right dining cross (or the opposite ).
An article in the reference section provides four implementation methods, which are relatively complete. The methods described here are described in the document. I will further explain that two forks are used only when there are forks on the left and right of the philosopher. The method in this article is to use semaphores to make it an atomic operation to pick up the left and right forks, that is, a mutex semaphores and a semaphores of the Five Forks, one implementation I provided here is to check whether the Left and Right forks can be picked up each time. If you can pick up two, use one mutex semaphore, and five int.

Semaphore fork_mutex = 1;
Int fork_count [5] = {1, 1, 1 };

Void have_dinner (int id) {// philosopher id 0-4
While (true ){
P (fork_mutex );
If (1 = fork_count [id] & 1 = fork_count [(id + 1) % 4]) {
Fork_count [id] = 0; // pick up the toy on the left
Fork_count [(id + 1) % 4] = 0; // pick up the right cross
}
V (fork_mutex );
Use a fork on the right to eat
P (fork_mutex );
Fork_count [id] = 1; // put the left side of the cross
Fork_count [(id + 1) % 4] = 1; // put the right side of the fork
V (fork_mutex );
Think about CS or dota.
}
}

3. Reader

There are three subdivisions: reader first (default), weak writer first (in fact, it can be understood as fair to both readers), and strong writer first. For details, see the references. Now I am not very familiar with this issue. Here we will give readers a priority.
The reader's priority is as follows: You can have multiple readers working at the same time. You cannot have either a reader or a writer working at the same time.
As you can see, as long as more than one reader is working, the writer and the readers who come back later will certainly be suspended, but the reader can work, so the reader is given priority.

Semaphore read_mutex = 1;
Int read_count = 0;
Semaphore write_mutex = 1;

Void reader (){
While (true ){
P (read_mutex );
If (0 = read_count) {// The applied reader is equal to the applied writer only when no current Reader is working.
P (write_mutex)
}
Read_count ++;
V (read_mutex );
Read files
P (read_mutex );
Read_count --;
If (0 = read_count)
V (write_mutex );
V (read_mutex );
}
}
Void writer (){
While (true ){
P (write_mutex );
Write files
V (write_mutex );
}
}

5. Reference

Dining Philosophers http://zh.wikipedia.org/wiki/%E5%93%B2%E5%AD%A6%E5% AE %B6%E5%B0%B1%E9%A4%90%E9%97% AE %E9%A2%98
Concurrency and mutex in operating system: http://www.tangblog.info/2010/03/24/philosopher-eating-problem-operting-system.html of dining problem of philosophers
Reader writer Question and Answer http://wenku.baidu.com/view/67626efafab069dc50220109.html

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.