"Operating System Summary" classic process synchronization issues-producer consumer issues

Source: Internet
Author: User
Tags mutex semaphore

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.