(postgraduate) Black electricity eating Apple synchronization mutex problem (with code)

Source: Internet
Author: User

More complex producer-consumer issues:

Problem description

There is a plate on the table and only one fruit can be put into it at a time. Dad specificity Plate in the apple, mother specificity plate in the orange, son special to eat dishes in the orange, daughter special to eat the apples on the plate. Only when the plate is empty, the father or mother can put a fruit on the plate, and the son or daughter only when there is a fruit of its own in the plate.
Can be removed from the plate.

Problem analysis

1) Relationship analysis. The relationship here is slightly more complicated, first by putting only one fruit at a time to show that mom and dad are mutually exclusive . Father and daughter, mother and son are synchronous, and the two pairs of processes must be connected, there is no mutex and synchronization between the son and the daughter, because they are the choice of conditions to execute, it is not possible to concurrency, 2-8 is shown.
2) Arrange ideas. There are 4 processes that can actually be abstracted to two producers and two consumers connected to a buffer of size 1.

3) Semaphore setting. First, set the semaphore plate to be a mutex, indicating whether to allow the fruit to be placed into the plate, the initial value is 1, is allowed to put in, and only allowed into one. Signal volume Apple Indicates whether there is an apple on the plate, the initial value is 0, indicating that the plate is empty, not allowed, if apple=l can be taken. The semaphore orange Indicates whether there are oranges on the plate, the initial value is 0, indicating that the plate is empty, not allowed, if orange=l can be taken. The code to resolve the problem is as follows:

Semaphore Plate=l, apple=0, orange=0;d AD ()//parent process 
{ while(1) {Prepare an apple; P (plate); //mutually exclusive to the disk to take, put fruitPut the apple on the plate;//to put an apple in a dishV (Apple);//allow apples to be taken}}
Mom () {//Mother Process while(1) {Prepare an orange; P (plate); //mutually exclusive to the disk to take, put fruitPut the orange on the plate;//to put an orange in a dishV (orange);//allow to take oranges }}
Son () {//son process while(1) {P (orange); //mutually exclusive to the dish to fetch orangesTake an orange fromThe plate; V (plate); //allow fruit to be taken and placed in the disheat the orange; }}
Daughter () {//daughter Process while(1) {P (Apple); //mutually exclusive to fetch apples from the diskTake an apple fromThe plate; V (plate); //run the fruit on the plate.eat the apple; }}

The relationship between processes is shown in 2-9. Dad () and daughter (), Mam (), and Son () must be executed continuously, because of this, only after the daughter has taken the Apple, or the son took the orange to release the plate, that is, V (plate) operation.

(postgraduate) Black electricity eating Apple synchronization mutex problem (with code)

Related Article

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.