Signal Volume (iii) classic examples (II.)

Source: Internet
Author: User
Tags mutex

The classic example of the signal volume, including "Reader writer", "over the bridge", "Bus", "quartet Love" and so on

A. The reader is writing questions (there are many situations here):

(1) Readers first

Topic Description: There is a book, there are many readers and write, read and write mutually exclusive, write mutual exclusion. When multiple readers can read at the same time, when a reader is reading the book, other readers can come in to read, but the writer cannot come in to write.

Answer:

Signal Volume:

W: Can write No: 1

Nreader: Number of readers: 0

Mutex: "Mutual exclusion" between readers, the order of entry and exit: 1

Written by: Reader

P (W) p (mutex)

Write Nreader + +

V (W) if (Nreader = 1)//First Reader

P (W)//plugging writer

V (Mutex)

Read

P (Mutex)

Nreader--

if (nreader==0)//Last Reader

V (W)//Wake writer

V (Mutex)

(2) Writing is preferred

Topic Description: There is a book, there are many readers and write, read and write mutually exclusive, write mutual exclusion, the difference is as long as there are writing, the follow-up reader must wait behind the writer

W: Can write No: 1

Nreader: Number of readers: 0

Mutex: "Mutual exclusion" between readers, the order of entry and exit: 1

S: For blocking follow-up readers: 1

Written by: Reader

P (s)

P (s) p (mutex)

P (W) Nreader + +

Write if (Nreader = 1)//First Reader

V (W) P (W)//plugging writer

V (s) v (mutex)

V (s)

Read

P (Mutex)

Nreader--

if (nreader==0)//Last Reader

V (W)//Wake writer

V (Mutex)

Two. Over the log bridge

Topic Description:

The bridge is one-way, from west to east and from east to west.

Answer:

Signal Volume:

S: Indicates that the bridge is available. : 1

NW: East to West number: 0

NE: West to East number: 0

M1: Mutex modification to NW: 1

M2: Mutually exclusive to NE modification: 1

Towest Toeast

P (M1) p (m2)

nw++ NE + +

if (1==NW) if (1==ne)

P (s) p (s)

V (M1) v (m2)

Cross the bridge to West Cross

P (M1) p (m2)

nw--NE--

if (0==NW) if (0==ne)

V (s) v (s)

V (M1) v (m2)

Three. Quartet Love

Topic Description: There are two men and two women, Men send women roses, ladies send Men's watches. One by one corresponds, each can only connect one

Answer:

(1) Teacher answer

Signal Volume:

M1: Collection of flowers between mutual exclusion women behavior: 1

M2: collecting and sending flowers between mutually exclusive men: 1

S1: there are flowers. 0

S2: There are tables. 0

Ladies and gentlemen

P (S1) p (m2)

P (m1) Send flowers

Flower V (S1)

Send table P (s2)

V (S2) Watch

V (M1) v (m2)

Love Love

(2) own answer

Four are dick (male), John (male), Xi Hee (female), haha (female)

Signal Volume:

G1: Do you have a watch: 0

G2: Does the Rose Collect: 0

S1: Can I send table: 1

S2: Can I send rose: 1

Note: Here only run once, not be able to cycle

Dick                                 John                                  hehe                                   haha

P (S2) p (S2) p (S1) p (S1)

Sendrose () sendrose Sendwatch Sendwath

V (G2)                               V (G2)                               V (G1)                              V (G1)

P (G1) p (G1) p (G2) p (G2)

Getwatch Getwatch Getrose Getrose

V (S1) v (S1) v (S2) v (S2)

PS: This solution I think logically, no mistake, but I a classmate said actually carry the machine running when there seems to be a problem, I do not want to understand, please advise the master

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.