Operating System-problem of mutual exclusion between processes-Introduction of producers and consumers, operatingsystem-
Several solutions to mutual exclusion between processes, whether it is the Peterson solution or the TSL command method, have a feature: when a process is blocked outside the critical zone, the blocked process will remain in the waiting state, which will not only waste CPU resources, but also have a bad side effect. Assume that two processes, H, L, and H, have a high priority, and L processes have a low priority. The CPU scheduling rule is that as long as H is in Ready state, H is started to run. The problem arises:
- H is in the BLock status and may be waiting for external resources
- L enters the critical section
- H is in Ready state (external resources are satisfied), and L has not left the critical section
- According to the scheduling rules, the CPU switches to H and L suspends.
- H.
- L it will never be able to leave the critical section
I. Introduction of 1.1 by producer and consumer to solve the infinite waiting Problem
If a process is blocked when it is not allowed to enter the critical section, the wireless waiting problem is solved, and the problem raised at the beginning of the article is also solved. Block processes.
:
When the producer consumer inserts data into the Buffer and finds that the buffer is full, the producer sleep instead of infinite loop wait.
When no data is found in the buffer, let the consumer sleep, instead of infinite loop wait.
When there is data in the buffer, but the consumer is found to be in the sleep state, the consumer is allowed to wake up.
When there is no data in the buffer, but the producer is found to be in sleep state, the producer is allowed to wake up.
1.2 code demonstration
# Definne N 100 // buffer Capacity int count = 0; // number of items in buffer void producer (void) {int item; while (true) // always run {item = produce_item (); // generate the next item if (count = N) // if the buffer is full, sleep {sleep ();} insert_item (item); // put the item into buffer count = count + 1; // number + 1 if (count = 1) // buffer is empty before, so the consumer is in sleep state, and now there is an item, wake up consumer {wakeup (consumer) ;}} void consumer (void) {int item; while (true) {if (count = 0) // if the buffer is empty, sleep {sleep ();} item = remove_item (); // remove the item from the buffer count = count-1; // quantity-1 if (count = N-1) // the buffer is full before, so the producer is in sleep state. Now an item is removed and the producer {wakeup (producer) ;}consume_item (item); // consume item} is awakened }}Ii. Conditional competition of producers and consumers
The above code still has Race Conditions. Consider the following situation:
After the preceding steps, the wakeup does not work for the consumer because the cpu scheduling has not reached the sleep state. Therefore, the wakeup signal is lost.
Continue:
Final result:
The key to the above problem is that the wakeup command sent to a process without sleep, And the wakeup does not work, and the model of the wakeup is eventually lost.
A quick solution is to add a Wakeup waiting Bit to the process. When a wakeup request is sent to a process in the wakeup State, set the Wakeup waiting Bit Flag of the process to true. When sleep is given to this process, judge that if the Wakeup waiting Bit is true, the sleep will not be performed.
This method can solve the problem temporarily, but it is very bad. If there are multiple processes, a lot of signs are needed. In the following articles, semaphores and mutex are used to solve the competition conditions of producers and consumers.