With the synchronization and mutual exclusion between the previous processes, producer-consumer issues are often used to resolve synchronization and mutex issues during multi-process concurrent execution.
The principle is as follows:
associate a bounded buffer of length n (n>0) with a group of producer processes P1,p2,..., pm and a group of consumer processes c1,c2,..., CK, as long as the buffer is not full, the producer can put the product into the buffer, as long as the buffer is not empty, Consumers will be able to take away product consumption from it.
(1) Synchronization conditions: Producers only at least one critical section of the unit is empty, in order to produce products, consumers only at least one critical area is filled in the product, in order to consume the product, so set two synchronization variables, avail for the producer of private variables, the initial value of N, Full is the consumer's private variable, with an initial value of 0.
(2) Mutually exclusive condition: the producer and the consumer cannot access the critical resource at the same time, so set a mutex variable to the initial value of 1.
Producer Process: Consumer processes:
P (Avail) p (full)
P (Mutex) p (mutex)
Generate a product to consume a product
V (full) v (avail)
V (Mutex) v (mutex)
Producer-Consumer issues