ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU-(2020-2021)-1 > online discussion >
online discussion-6 Version 0
👤 Author: by writer 2020-11-03 01:37:51
Discuss classic problems of synchronization : the bounded-buffer problem, the dining-philosophers problem, the readers-writers problem.
What are their similarities and differences.

Please login to reply. Login

kagenzaclaudiengmailcom • 2020-11-03 14:22:17
Let me discuss the following three problems:
<ol class="content">
<li>Bounded Buffer (Producer-Consumer) Problem</li>
<li>Dining Philosophers Problem</li>
<li>The Readers Writers Problem</li>
</ol>
BOUNDED BUFFER PROBLEM
<ul class="content">
<li style="list-style-type: none">
<ul class="content">
<li>This problem is generalised in terms of the <b>Producer Consumer problem</b>, where a <b>finite</b> buffer pool is used to exchange messages between producer and consumer processes.</li>
</ul>
</li>
</ul>
Because the buffer pool has a maximum size, this problem is often called the <b>Bounded buffer problem</b>.
<ul class="content">
<li>Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.</li>
</ul> PostVer 0

Please login to reply. Login

kagenzaclaudiengmailcom • 2020-11-03 14:58:02
PostVer 0

Please login to reply. Login

804824950qqcom • 2020-11-03 14:58:50
1. Producer and consumer issues:
Problem descr iption: Use a buffer to store items. Only when the buffer is not full can the producer put in the item; only if the buffer is not empty can the consumer take the item.

1. Use semaphores to implement producer-consumer problems:

down: If the semaphore is greater than 0, perform the -1 operation; if the semaphore is equal to 0, the process sleeps and waits for the semaphore to be greater than 0;

up: Perform a +1 operation on the semaphore, wake up the sleeping process and let it complete the down operation.

Because the buffer is a critical resource, a mutex must be used to control mutually exclusive access to the buffer.

In order to synchronize the behavior of producers and consumers, it is necessary to record the number of items in the buffer. The number can be counted using semaphores. Two semaphores need to be used here: empty records the number of empty buffers, and full records the number of full buffers. Among them, the empty semaphore is used in the producer process, when empty is not 0, the producer can put in the item; the full semaphore is used in the consumer process, when the full semaphore is not 0, the consumer Only then can the items be taken away.

Note that you cannot lock the buffer first and then test the semaphore. In other words, you cannot execute down(mutex) first and then down(empty). If this is done, then this situation may occur: after the producer locks the buffer, executes the down(empty) operation and finds that empty = 0, and the producer sleeps. Consumers cannot enter the critical area, because the producer locks the buffer, the consumer cannot perform the up(empty) operation, empty is always 0, causing the producer to wait forever without releasing the lock, and therefore the consumer Will wait forever.

2. Reader-writer problem:
Multiple processes are allowed to read data at the same time, but read and write and write and write operations are not allowed to occur at the same time.

An integer variable count records the number of processes that read data, a mutex count_mutex is used to lock count, and a mutex data_mutex is used to lock read and write data.

3. The meal problem of philosophers:
Five philosophers gathered around a round table, and food was placed in front of each philosopher. There are two alternating activities in the life of a philosopher: eating and thinking. When a philosopher eats, he needs to pick up the two chopsticks on his left and right, and he can only pick up one chopstick at a time. PostVer 0

Please login to reply. Login

kagenzaclaudiengmailcom • 2020-11-03 14:59:29
The Readers Writers Problem

In this problem there are some processes(called readers) that only read the shared data, and never change it, and there are other processes(called writers) who may change the data in addition to reading, or instead of reading it.
There are various type of readers-writers problem, most centred on relative priorities of readers and writers. PostVer 0

Please login to reply. Login

hokyeejaufoxmailcom • 2020-11-03 15:58:39
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/dis6.txt">dis6</a> PostVer 0

Please login to reply. Login

1720650158qqcom • 2020-11-03 17:05:42
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/发不上.jpg"><img class="alignnone size-medium wp-image-8772" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/发不上-300x176.jpg" alt="" width="300" height="176" /></a> PostVer 0

Please login to reply. Login

653968106qqcom • 2020-11-04 10:44:09
2018329621008徐天然

<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/发不上去2.jpg"><img class="alignnone size-medium wp-image-8782" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/发不上去2-300x177.jpg" alt="" width="300" height="177" /></a> PostVer 0

Please login to reply. Login

512858048qqcom • 2020-11-04 14:32:10
<ol>
<li><strong>Bounded-buffer Problem:</strong>
Bounded Buffer problem is also called producer consumer problem. This problem is generalized in terms of the Producer-Consumer problem. Solution to this problem is, creating two counting semaphores “full” and “empty” to keep track of the current number of full and empty buffers respectively. Producers produce a product and consumers consume the product, but both use of one of the containers each time.</li>
<li><strong>Dining-Philosphers Problem:</strong>
The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both. This problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation-free manner.</li>
<li><strong>Readers and Writers Problem:</strong>
Suppose that a database is to be shared among several concurrent processes. Some of these processes may want only to read the database, whereas others may want to update (that is, to read and write) the database. We distinguish between these two types of processes by referring to the former as readers and to the latter as writers. Precisely in OS we call this situation as the readers-writers problem. Problem parameters:
<ul>
<li>One set of data is shared among a number of processes.</li>
<li>Once a writer is ready, it performs its write. Only one writer may write at a time.</li>
<li>If a process is writing, no other process can read it.</li>
<li>If at least one reader is reading, no other process can write.</li>
<li>Readers may not write and only read.</li>
</ul>
</li>
<li><strong>readers–writers problems   </strong></li>
</ol>
The readers–writers problems are examples of a common computing problem in concurrency. There are at least three variations of the problems, which deal with situations in which many concurrent threads of execution try to access the same shared resource at one time.Some threads may read and some may write, with the constraint that no thread may access the shared resource for either reading or writing while another thread is in the act of writing to it. (In particular, we want to prevent more than one thread modifying the shared resource simultaneously and allow for two or more readers to access the shared resource at the same time). A readers–writer lock is a data structure that solves one or more of the readers–writers problems.

<strong>Similarities:</strong>All use semaphores for synchronization,

<strong>Differences: </strong>Different problems correspond to different scenarios and needs, and the solutions are also diversified.

&nbsp;

&nbsp; PostVer 0

Please login to reply. Login

1730854984qqcom • 2020-11-04 17:11:37
The bounded-buffer problem: Producers and consumers share a fixed-size buffer and production and consumption operations must be mutually exclusive, stopping production when the buffer is full and consuming only when the product is available. To implement the mutex, we can set a semaphore. Obtain empty cache area units, and use a count semaphore to represent the number of empty units, empty; only a "full" resource can be consumed. Define two synchronization semaphores: empty -- to indicate whether the buffer is empty and the initial value is 1.full -- to indicate whether the buffer is full and the initial value is 0.

The dining-philosophers problem: In this case, the use of each chopstick is mutually exclusive, and each philosopher can only use the chopsticks around him. Deadlock problems can easily occur in solutions to this problem. Several possible solutions to the deadlock problem are listed below. Only four philosophers are allowed to sit at the table at the same time; A philosopher is allowed to pick them up only if both chopsticks are usable; Using an asymmetric solution, the philosopher with an odd number picks up the left chopstick first, then the right chopstick, while the philosopher with an even number picks up the right chopstick first, then the left chopstick.

The readers-writes problem:In this case, the read and write operations are in the same critical section. Simultaneous reading and writing are not allowed. Multiple readers are allowed to read at the same time. Multiple writers are not allowed to write at the same time. When many readers read at the same time, the first reader unlocks, the last reader locks, and a readcount is used to count the number of readers.

Similarities: These problems are concurrency control problems, and the semaphore is used as the solution.
Differences: Each of these problems has a different synchronization scheme.

PostVer 0

Please login to reply. Login

2470994471qqcom • 2020-11-05 17:07:34
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/屏幕截图-2020-11-05-180918.jpg"><img class="alignnone size-medium wp-image-8791" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/屏幕截图-2020-11-05-180918-300x113.jpg" alt="" width="300" height="113" /></a> PostVer 0

Please login to reply. Login

cfeshete97gmailcom • 2020-11-06 02:03:24
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/mmexport1604599336382.jpg"><img class="alignnone size-medium wp-image-8797" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/mmexport1604599336382-244x300.jpg" alt="" width="244" height="300" /></a> PostVer 0

Please login to reply. Login

244766935qqcom • 2020-11-06 11:29:16
<strong>2018329621184 边凯昂</strong>

<strong>The Bounded-Buffer Problem</strong>

The bounded-buffer problem is commonly used to illustrate the power of synchronization primitives. Here, we present a general structure of this scheme without committing ourselves to any particular implementation.

We assume that the pool consists of n buffers, each capable of holding one item.

The mutex binary semaphore provides mutual exclusion for accesses to the

buffer pool and is initialized to the value 1. The empty and full semaphores

count the number of empty and full buffers. The semaphore empty is initialized

to the value n ; the semaphore full is initialized to the value 0.

<strong>The Dining-Philosophers Problem</strong>

Consider five philosophers who spend their lives thinking and eating. The philosophers share a circular table surrounded by five chairs, each belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five single chopsticks (Figure 7.5). When a philosopher thinks, she does not interact with her colleagues. From time to time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the chopsticks  that are between her and her left and right neighbors). A philosopher may pick up only one chopstick at a time. Obviously, she cannot pick up a chopstick that is already in the hand of a neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats without releasing the chopsticks. When she Is finished eating, she puts down both  chopsticks and starts thinking again.

The dining-philosophers problem is considered a classic synchronization problem neither because of its practical importance nor because computer scientists dislike philosophers but because it is an example of a large class of concurrency-control problems. It is a simple representation of the need to allocate several resources among several processes in a deadlock-free and starvation-free manner.

<strong>The Readers–Writers Problem</strong>

Suppose that a database is to be shared among several concurrent processes. Some of these processes may want only to read the database, whereas others may want to update (that is, read and write) the database. We distinguish between these two types of processes by referring to the former as readers and to the latter as writers. Obviously, if two readers access the shared data simultaneously, no adverse effects will result. However, if a writer and some other process (either a reader or a writer) access the database simultaneously, chaos may ensue.

The readers–writers problem has several variations, all involving priori-ties. The simplest one, referred to as the first readers–writers problem, requires that no reader be kept waiting unless a writer has already obtained permission to use the shared object. In other words, no reader should wait for other readers to finish simply because a writer is waiting. The second readers–writers problem requires that, once a writer is ready, that writer perform its write as soon as possible. In other words, if a writer is waiting to access the object, no new readers may start reading.

<strong>Similarities:</strong> Their main task is to coordinate the execution order of multiple related processes, so that the processes executing concurrently can share system resources according to certain rules (or timing sequence), and can cooperate with each other well, so that the execution of the program can be reproducible.

<strong>Difference: </strong>the complexity of semaphore mechanism is different and the utilization rate of resources is different PostVer 0

Please login to reply. Login

997371991qqcom • 2020-11-06 16:21:19
2018329621069 张时敏<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/捕获.png"><img class="alignnone size-medium wp-image-8809" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/捕获-300x202.png" alt="" width="300" height="202" /></a> PostVer 0

Please login to reply. Login

454234624qqcom • 2020-11-07 14:54:48
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/Online_Disscussion06.jpg"><img class="alignnone size-medium wp-image-8816" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/Online_Disscussion06-217x300.jpg" alt="" width="217" height="300" /></a> PostVer 0

Please login to reply. Login

1210775967qqcom • 2020-11-09 16:07:29
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/无标题.bmp"><img class="alignnone size-full wp-image-8850" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/无标题.bmp" alt="" width="809" height="633" /></a> PostVer 0

Please login to reply. Login

1285795916qqcom • 2020-11-09 20:13:58
My answer is in the following text file: <a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/邱锐鹏_2018329621199_discussion6.txt">邱锐鹏_2018329621199_discussion6</a> PostVer 0

Please login to reply. Login

1079513613qqcom • 2020-11-10 14:50:27
1. Producer and consumer issues: Problem descr iption: Use a buffer to store items. Only when the buffer is not full can the producer put in the item; only if the buffer is not empty can the consumer take the item. 1. Use semaphores to implement producer-consumer problems: down: If the semaphore is greater than 0, perform the -1 operation; if the semaphore is equal to 0, the process sleeps and waits for the semaphore to be greater than 0; up: Perform a +1 operation on the semaphore, wake up the sleeping process and let it complete the down operation. Because the buffer is a critical resource, a mutex must be used to control mutually exclusive access to the buffer. In order to synchronize the behavior of producers and consumers, it is necessary to record the number of items in the buffer. The number can be counted using semaphores. Two semaphores need to be used here: empty records the number of empty buffers, and full records the number of full buffers. Among them, the empty semaphore is used in the producer process, when empty is not 0, the producer can put in the item; the full semaphore is used in the consumer process, when the full semaphore is not 0, the consumer Only then can the items be taken away. Note that you cannot lock the buffer first and then test the semaphore. In other words, you cannot execute down(mutex) first and then down(empty). If this is done, then this situation may occur: after the producer locks the buffer, executes the down(empty) operation and finds that empty = 0, and the producer sleeps. Consumers cannot enter the critical area, because the producer locks the buffer, the consumer cannot perform the up(empty) operation, empty is always 0, causing the producer to wait forever without releasing the lock, and therefore the consumer Will wait forever. 2. Reader-writer problem: Multiple processes are allowed to read data at the same time, but read and write and write and write operations are not allowed to occur at the same time. An integer variable count records the number of processes that read data, a mutex count_mutex is used to lock count, and a mutex data_mutex is used to lock read and write data. 3. The meal problem of philosophers: Five philosophers gathered around a round table, and food was placed in front of each philosopher. There are two alternating activities in the life of a philosopher: eating and thinking. When a philosopher eats, he needs to pick up the two chopsticks on his left and right, and he can only pick up one chopstick at a time. PostVer 0

Please login to reply. Login

1224532347qqcom • 2020-11-10 15:13:06
hese three problems belong to the classic problem of process synchronization. To put it simply, a process is a task in the system. There are multiple tasks in the operating system that need to be performed, so how can they be performed in sync? How do you make tasks execute concurrently without affecting each other? These three questions are classics in the discussion of process synchronization.

1. Philosopher eating problem is a typical multi-threaded deadlock problem 2. Producer and consumer problem belong to buffer zone problem PostVer 0

Please login to reply. Login

1119833189qqcom • 2020-11-10 19:03:38
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/阿巴阿巴.jpg"><img src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/11/阿巴阿巴-300x79.jpg" alt="" width="300" height="79" class="alignnone size-medium wp-image-8874" /></a> PostVer 0

Please login to reply. Login

2031115154qqcom • 2020-11-17 09:11:27
The bounded-buffer problem is something that if the buffer area is full, the producer can not put the product into buffer area and if the buffer area is empty,  the consumer can not take the product from buffer area until that  is not empty. Furthermore, more complicated condition: there are more than one kind of producer and more than one kind of consumer,  and at the same time, one kind of producer only produce specific product and one kind of consumer only consume specific product, too. The mutual exclusion of bounded-buffer problem: producer and producer, consumer and consumer, producer and consumer.

The readers-writers problem have a different mutual exclusion from the bounded-buffer problem: if there is not writer in the buffer area, more than one readers can read buffer area at the same time.

The dining-philosophers problem  may cause a deadlock, even if there is no deadlock, resource exhaustion may occur. PostVer 0

Please login to reply. Login

wx287_oz26ft1wt2_isbggtha4lgepaasa • 2020-12-08 14:00:51
Bounded-buffer Problem: Bounded Buffer problem is also called producer consumer problem. This problem is generalized in terms of the Producer-Consumer problem. Solution to this problem is, creating two counting semaphores “full” and “empty” to keep track of the current number of full and empty buffers respectively. Producers produce a product and consumers consume the product, but both use of one of the containers each time.

Dining-Philosphers Problem: The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both. This problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation-free manner.

Readers and Writers Problem: Suppose that a database is to be shared among several concurrent processes. Some of these processes may want only to read the database, whereas others may want to update (that is, to read and write) the database. We distinguish between these two types of processes by referring to the former as readers and to the latter as writers. Precisely in OS we call this situation as the readers-writers problem. Problem parameters:

One set of data is shared among a number of processes.

Once a writer is ready, it performs its write. Only one writer may write at a time.

If a process is writing, no other process can read it.

If at least one reader is reading, no other process can write.

Readers may not write and only read.

readers–writers problems

The readers–writers problems are examples of a common computing problem in concurrency. There are at least three variations of the problems, which deal with situations in which many concurrent threads of execution try to access the same shared resource at one time.Some threads may read and some may write, with the constraint that no thread may access the shared resource for either reading or writing while another thread is in the act of writing to it. (In particular, we want to prevent more than one thread modifying the shared resource simultaneously and allow for two or more readers to access the shared resource at the same time). A readers–writer lock is a data structure that solves one or more of the readers–writers problems.

Similarities:All use semaphores for synchronization, Differences: Different problems correspond to different scenarios and needs, and the solutions are also diversified. PostVer 0

Please login to reply. Login

1079513613qqcom • 2020-12-10 12:15:37
1. Producer and consumer issues: Problem descr iption: Use a buffer to store items. Only when the buffer is not full can the producer put in the item; only if the buffer is not empty can the consumer take the item. 1. Use semaphores to implement producer-consumer problems: down: If the semaphore is greater than 0, perform the -1 operation; if the semaphore is equal to 0, the process sleeps and waits for the semaphore to be greater than 0; up: Perform a +1 operation on the semaphore, wake up the sleeping process and let it complete the down operation. Because the buffer is a critical resource, a mutex must be used to control mutually exclusive access to the buffer. In order to synchronize the behavior of producers and consumers, it is necessary to record the number of items in the buffer. The number can be counted using semaphores. Two semaphores need to be used here: empty records the number of empty buffers, and full records the number of full buffers. Among them, the empty semaphore is used in the producer process, when empty is not 0, the producer can put in the item; the full semaphore is used in the consumer process, when the full semaphore is not 0, the consumer Only then can the items be taken away. Note that you cannot lock the buffer first and then test the semaphore. In other words, you cannot execute down(mutex) first and then down(empty). If this is done, then this situation may occur: after the producer locks the buffer, executes the down(empty) operation and finds that empty = 0, and the producer sleeps. Consumers cannot enter the critical area, because the producer locks the buffer, the consumer cannot perform the up(empty) operation, empty is always 0, causing the producer to wait forever without releasing the lock, and therefore the consumer Will wait forever. 2. Reader-writer problem: Multiple processes are allowed to read data at the same time, but read and write and write and write operations are not allowed to occur at the same time. An integer variable count records the number of processes that read data, a mutex count_mutex is used to lock count, and a mutex data_mutex is used to lock read and write data. 3. The meal problem of philosophers: Five philosophers gathered around a round table, and food was placed in front of each philosopher. There are two alternating activities in the life of a philosopher: eating and thinking. When a philosopher eats, he needs to pick up the two chopsticks on his left and right, and he can only pick up one chopstick at a time. PostVer 0

Please login to reply. Login

1285795916qqcom • 2020-12-12 15:37:37
邱锐鹏 2018329621199

<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/12/屏幕快照-2020-12-12-下午3.41.14.png"><img class="alignnone size-medium wp-image-9132" src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/12/屏幕快照-2020-12-12-下午3.41.14-300x77.png" alt="" width="300" height="77" /></a> PostVer 0

Please login to reply. Login

450766287qqcom • 2020-12-22 13:43:33
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2020/12/os6.png"><img src="http://www.yvsou.com/wp-content/uploads/sites/30/2020/12/os6-150x150.png" alt="" width="150" height="150" class="alignnone size-thumbnail wp-image-9194" /></a> PostVer 0

Please login to reply. Login

shatino94163com • 2020-12-22 14:34:39
Let me discuss the following three problems:
<ol class="content">
<li>Bounded Buffer (Producer-Consumer) Problem</li>
<li>Dining Philosophers Problem</li>
<li>The Readers Writers Problem</li>
</ol>
BOUNDED BUFFER PROBLEM
<ul class="content">
<li>
<ul class="content">
<li>This problem is generalised in terms of the <b>Producer Consumer problem</b>, where a <b>finite</b> buffer pool is used to exchange messages between producer and consumer processes.</li>
</ul>
</li>
</ul>
Because the buffer pool has a maximum size, this problem is often called the <b>Bounded buffer problem</b>.
<ul class="content">
<li>Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.</li>
</ul> PostVer 0

Please login to reply. Login

439731491qqcom • 2021-01-05 16:23:54
<a href="http://www.yvsou.com/wp-content/uploads/sites/30/2021/01/捕获-1.png"><img class="alignnone size-medium wp-image-9292" src="http://www.yvsou.com/wp-content/uploads/sites/30/2021/01/捕获-1-300x161.png" alt="" width="300" height="161" /></a> PostVer 0

Please login to reply. Login

1403861656qqcom • 2021-01-06 22:16:28
This problem is generalised in terms of the Producer Consumer problem, where a finite buffer pool is used to exchange messages between producer and consumer processes.
This problem is summarized in terms of the producer-consumer problem, where a limited buffer pool is used to exchange messages between producer and consumer processes.



Because the buffer pool has a maximum size, this problem is often called the Bounded buffer problem.



Because the buffer pool has a maximum size, this problem is often referred to as the bounded buffer problem.



Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.

The solution to this problem is to create two count semaphores, "full" and "empty," to keep track of the current number of buffers, respectively PostVer 0

Please login to reply. Login

962305148qqcom • 2021-01-07 22:23:15
<h2><strong>2018329621064_邢杰焰</strong></h2>
1. Producer and consumer issues: Problem descr iption: Use a buffer to store items. Only when the buffer is not full can the producer put in the item; only if the buffer is not empty can the consumer take the item. 1. Use semaphores to implement producer-consumer problems: down: If the semaphore is greater than 0, perform the -1 operation; if the semaphore is equal to 0, the process sleeps and waits for the semaphore to be greater than 0; up: Perform a +1 operation on the semaphore, wake up the sleeping process and let it complete the down operation. Because the buffer is a critical resource, a mutex must be used to control mutually exclusive access to the buffer. In order to synchronize the behavior of producers and consumers, it is necessary to record the number of items in the buffer. The number can be counted using semaphores. Two semaphores need to be used here: empty records the number of empty buffers, and full records the number of full buffers. Among them, the empty semaphore is used in the producer process, when empty is not 0, the producer can put in the item; the full semaphore is used in the consumer process, when the full semaphore is not 0, the consumer Only then can the items be taken away. Note that you cannot lock the buffer first and then test the semaphore. In other words, you cannot execute down(mutex) first and then down(empty). If this is done, then this situation may occur: after the producer locks the buffer, executes the down(empty) operation and finds that empty = 0, and the producer sleeps. Consumers cannot enter the critical area, because the producer locks the buffer, the consumer cannot perform the up(empty) operation, empty is always 0, causing the producer to wait forever without releasing the lock, and therefore the consumer Will wait forever. 2. Reader-writer problem: Multiple processes are allowed to read data at the same time, but read and write and write and write operations are not allowed to occur at the same time. An integer variable count records the number of processes that read data, a mutex count_mutex is used to lock count, and a mutex data_mutex is used to lock read and write data. 3. The meal problem of philosophers: Five philosophers gathered around a round table, and food was placed in front of each philosopher. There are two alternating activities in the life of a philosopher: eating and thinking. When a philosopher eats, he needs to pick up the two chopsticks on his left and right, and he can only pick up one chopstick at a time. PostVer 0

Please login to reply. Login

Reversion History

Loading...
No reversions found.