Talk C chestnuts together (105th back: C language instance-producer and consumer question 1)
Hello, everyone. In the previous session, we talked about the process knowledge system diagram and drew a knowledge system diagram. In this case, the following is an example:Producer and consumer problems. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
The producer-consumer problem is a classic example of inter-process communication, which is often used to explain inter-process communication. Next we will introduce what is a question about producers and consumers:
1. Overview of producer and consumer issues
There is a C pool used to store products.
Producer A constantly stores the products produced in the pool, and determines whether the product has reached the pool capacity C. If the number of products reaches the pool capacity C, the producer stops production, and wait for the consumer to remove the product from the pool.
Consumer B constantly removes the product from the pool and determines whether there is any product in the pool. If there is no product in the pool, the consumer stops taking the product and waits for the producer to produce the product.
2. producer and consumer problems
Let's introduce how to implement production and consumer problems:
Both producers and consumers are processes. Therefore, two processes are required. The producer has the action of producing the product, and the consumer has the action of removing the product. The two actions need to be performed in an orderly manner, so process synchronization is required. Both producers and consumers store and remove products from the pool. It can be seen that the pool is a critical zone. Therefore, inter-process communication is required and process synchronization is required to control inter-process communication.
Let's make a conclusion: In order to achieve producer and consumer problems, we need to create/end processes. We also need inter-process communication. Process Communication can share the memory in the pipelines described in the previous chapter, select a communication method in the message queue, and use the semaphore to synchronize the process communication.
We can see that the producer and consumer problems use all process-related knowledge and can be seen as a comprehensive application of process knowledge. In the previous chapter, we summarized and sorted out the knowledge system diagram of the process. If you forget it, you can view it.
The following is the pseudocode for producer and consumer issues. For details, refer:
Produce () {while (true) {p (sem); // use the P/V Operation for Process Synchronization and mutual exclusion if (count <C) // determine whether the number of products is smaller than the pool capacity {produce (); // production product, and store it in the pool count ++;} else break; // when the number of products is equal to the pool capacity, production is stopped, waiting for the consumer to take the product v (sem );}}
Customer () {while (true) {p (sem); // use the P/V Operation for Process Synchronization and mutual exclusion if (count> 0) // determine whether there are products in the pool {custome (); // take the product count from the pool --;} else break; // stop taking the product when the number of products in the pool is equal to zero, wait for the producer to produce the product v (sem );}}
Let's talk about the example of producers and consumers. I want to know what examples will be provided later, and I will try again.