Producer Consumer Issues
The problem description is: There is a group of producer processes in the production of products, this product is provided to consumers to consume. To enable producer and consumer processes to execute concurrently, set up a pool of n buffers between them, the producer process can put the products it produces into a buffer pool, and the consumer process can get a product consumption from one buffer.
Use of recorded signal volume
Semaphore mutex=1, empty=n,full=0; item Buffer[n];//Bufferint inch= out=0;//inputs, output pointersvoidProducer () { while(1) { ... Production of a product nextp; ... wait (empty);//First Judge empty--whether he is less than 0, less than 0 is self-blockingWait (mutex);//Get the lock, or you'll block yourself.buffer[inch]= NEXTP;//go to buffer [in] to put the product inch= (inch+1)% n; Signal (mutex);//Release lockSignal (full);//full++ to determine whether <=0, if it is the wake-up process}}voidConsumer () { while(1{... wait (full);//First Judge full--whether he is less than 0, less than 0 is self-blockingWait (mutex);//Get the lock, or you'll block yourself.NEXTC = buffer[ out];//from buffer [out] take product out= ( out+1)MoDN Signal (mutex);//Release lockSignal (empty);//empty++ to determine whether <=0, if it is the wake-up processConsumer NEXTC Products; }}main () {cobegin{producer (); Consumer ();}
Using an and type semaphore
Semaphore mutex=1, empty=n,full=0; item Buffer[n];//Bufferint inch= out=0;//inputs, output pointersvoidProducer () { while(1) { ... Production of a product nextp; ... swait (empty, mutex);//When empty and mutex are greater than 0, otherwise self-blockingbuffer[inch]= NEXTP;//go to buffer [in] to put the product inch= (inch+1)MoDN Ssignal (mutex, full);//Set the lock back, then full+1}}voidConsumer () { while(1) {... swait (full, mutex); When both full and mutex are greater than0, otherwise self-blocking NEXTC = buffer[ out];//from buffer [out] take product out= ( out+1)MoDN Signal (mutex, empty); Set the lock back, then empty+1Consumer NEXTC Products; }}
Use of pipe process
//Establish a tube structureMonitor Produceconsumer {item buffer[n];//Buffer int inch, out; Condition Notfull, Notempty;intCount Public:void put(Item x) {if(cout >= N) cwait (notfull);//If the amount inside is already equal to N, let the process blockbuffer[inch] = x;//or put something on it inch= (inch+1)% N; count++; Csignal (Notempty);//Wake up the consumer process if not empty}void Get(Item x) {if(Count <=0) cwait (Notempty);//If it is empty, let this process blockx = buffer[ out]; out= ( out+1)% N; count--; Csignal (Notfull);//If you are dissatisfied, wake up the producer process} {inch=0; out=0; Count =0; }}PC;voidProducer () {Item x; while(1) {put something pc.put (x) in the buffer; }}VOIF consumer () {item x; while(1) {PC.Get(x); Fetch something from the buffer}}voidMain () {cobegin; Proceducer (); Consumer (); Conend;}
"Operating System Summary" classic process synchronization issues-producer consumer issues