Linux uses ring BUF and POSIX version semaphores to solve producer consumer problems

Source: Internet
Author: User

First, Semaphore (semaphore)

The mutex variable is 0 or 1, which can be considered as the available quantity of a resource, and the mutex is 1 when initialized, indicating that there is an available resource, the resource is locking, the mutex is reduced to 0, the resource is no longer available, the resource is freed when unlocked, and the mutex is added to 1, indicating that there is another available resource.

Semaphores (Semaphore) are similar to mutexes, which indicate the number of available resources, unlike mutexes, which can be greater than 1.

That is, if the semaphore describes the number of resources is 1 o'clock, at this time the semaphore and the same mutex lock!

This is a POSIX semaphore library function that can be used to synchronize between threads in the same process and between different processes.

Use the following:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/7F/5F/wKioL1cceRvBmhyAAAAZQGewxwU501.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiol1ccervbmhyaaaazqgewxwu501.jpg "/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/7F/62/wKiom1cceFWg_zmrAAAitfUO9f4274.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiom1ccefwg_zmraaaitfuo9f4274.jpg "/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/7F/5F/wKioL1cceRvTkNJ9AAAJA9mAj2w968.jpg "style=" float: none; "Title=" L3. JPG "alt=" Wkiol1ccervtknj9aaaja9maj2w968.jpg "/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/62/wKiom1cceFWyE3nMAAAKWIpsHgI023.jpg "style=" float: none; "Title=" L4. JPG "alt=" Wkiom1ccefwye3nmaaakwipshgi023.jpg "/>

The type of the semaphore variable is sem_t,sem_init () Initializes a semaphore variable, the value parameter represents the number of available resources, and a pshared parameter of 0 indicates that the semaphore applies to thread synchronization between the same process. You should call Sem_destroy () to release resources related to semaphore after you have finished using the semaphore variable.

Call Sem_wait () to get the resource (P operation), the value of Semaphore minus 1, and if the value of semaphore is called when Sem_wait () is already 0, then the wait is suspended. If you do not want to suspend the wait, you can call Sem_trywait (). The Sem_post () can free up resources (v operations), increase the value of semaphore by 1, and wake up the pending thread.

II. realization of producer-consumer issues

(1). This example mainly uses the annular buf and the signal quantity to realize the single consumer, the single producer , its ring BUF realization mainly uses the array subscript ring BUF the size 20.

For producers: The consumer cannot be looped at the production speed.

For consumers: they cannot consume faster than producers.

In this example, the main realization: When the number of space required by the producer is 0, it hangs waiting for consumer consumption. When consumer data is 0, it hangs waiting for producers to produce. Its producer space initial value is 20, and its consumer data initial value is 0. The code is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7F/61/wKiom1cccKuACBHyAACbjUcnL9U442.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiom1ccckuacbhyaacbjucnl9u442.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7F/5F/wKioL1cccXKjLs3uAACMMy2hZr4931.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiol1cccxkjls3uaacmmy2hzr4931.jpg "/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/7F/61/wKiom1cccK2iIIdVAAAu-dpmQc0269.jpg "style=" float: none; "Title=" L3. JPG "alt=" Wkiom1ccck2iiidvaaau-dpmqc0269.jpg "/>

Operation Result:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/7F/61/wKiom1cccK2TGThJAACzvfEJmvc520.jpg "style=" float: none; "Title=" L4. JPG "alt=" Wkiom1ccck2tgthjaaczvfejmvc520.jpg "/>

(2). Multi-producer, multi-consumer , modify the code as follows, the need to separate the producers, consumers plus lock.

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/62/wKiom1ccdTGxrstcAABX9SVd9LY290.jpg "style=" float: none; "Title=" L6. JPG "alt=" Wkiom1ccdtgxrstcaabx9svd9ly290.jpg "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7F/5F/wKioL1ccdfiC3YctAABSNVxblyE024.jpg "style=" float: none; "Title=" L7. JPG "alt=" Wkiol1ccdfic3yctaabsnvxblye024.jpg "/>

The results of the operation are as follows:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7F/62/wKiom1ccdTLCx_ViAACmMJwzN8k692.jpg "style=" float: none; "Title=" L5. JPG "alt=" Wkiom1ccdtlcx_viaacmmjwzn8k692.jpg "/>

Summary: For multi-producer multi-consumer, in its semaphore operation Nega Lock, not outside, because the operation of the semaphore is atomic. and more efficient.

The semaphore is not locked and is mainly used for synchronization. However, you can also use semaphore operation to implement lock.

This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1767252

Linux uses ring BUF and POSIX version semaphores to solve producer consumer problems

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.