有人可以解释生产国和消费PV的形式? [英] Can someone explain producer and consumer in P V form?

查看:166
本文介绍了有人可以解释生产国和消费PV的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://en.wikipedia.org/wiki/Semaphore_%28programming%29#Example%3a_Producer.2Fconsumer_problem"相对=nofollow>维基百科有一个样品code ,每个人都使用了。

我真的不明白的P,V的东西。

I honestly don't get the P,V thing.

据先说

消费者必须等待制作人产生的东西,如果在   队列为空。

The consumer must wait for the producer to produce something if the queue is empty.

但后来它说

为例。单次消费进入临界区。以来   FULLCOUNT是0,则消费者块

Example. A single consumer enters its critical section. Since fullCount is 0, the consumer blocks.

我想阻挡装置还等什么?我的功课,需要我去了解使用这种二进制信号量,然后实施不同种类的生产者 - 消费者问题的解决方案。但我不明白的时候递增(其中共享变量P中设置和V)或递减。所以我希望懂行的人能向我解释?对待我就像一个非CS的学生,如果你可以?

I assume blocking means waiting? My homework requires me to understand the use of such binary semaphore and then implement a solution for a different kind of producer-consumer problem. But I don't get when to increment (which shared variable to set in P and V) or decrement. So I hope someone knowledgeable can explain to me? Treat me like a non-cs student if you may?

推荐答案

想象一下,一对夫妇的家伙搬进房子。

Imagine a couple guys moving into a house.

他们有一个完整的,他们需要卸载进了屋箱子的卡车。

They have a truck full of boxes they need to offload into the house.

于是,他们决定分头行动的工作。

So they decide to split up the work.

  • 制片人保罗说,他会带箱子从卡车和线路起来 在人行道上。

  • Paul Producer says he'll take boxes off the truck and line them up on the sidewalk.

查理消费者说,他将采取箱子掉在人行道上走 他们进入房子。

Charlie Consumer says he'll takes boxes off the sidewalk and take them into house.

它工作得很好了一会儿。但后来彼得显示出来,而且他提供 帮助兄弟保罗。突然,人行道上填补了箱子前 查理可以接他们回家。他被bummed关于这一点,并呼吁兄弟 康拉德和卡尔。但康拉德伤了他的胳膊和卡尔保持与玩 他的电话,所以现在:

It works well for a while. But then Peter shows up, and he offers to help brother Paul. Suddenly, the sidewalk fills up with boxes before Charlie can pick them up. He gets bummed about this and calls brothers Conrad and Carl. But Conrad hurt his arm and Carl keeps playing with his phone, so now:

  • 有时候生产者(保罗,彼得)仍然超过消费者, 人行道已满,球员必须站在一旁抱着箱子

  • Sometimes the producers (paul, peter) still outpace the consumers, the sidewalk is full and the guys have to stand around holding boxes

有时候消费者(查理·康拉德,卡尔)超过了生产商 他们正站在拆包在房子周围的人行道上,而不是

Sometimes the consumers (charlie, conrad, carl) outpace the producers and they're standing around the sidewalk instead of unpacking in house

所以,每个人都做一个规则:检查人行道之前你去那里

So everyone makes a rule: check the sidewalk before you go there!

不幸的是,它并没有帮助。保罗和彼得,排空两端 卡车,都看到了一个几乎完整的人行道上,但显然不够 空间多了一个盒子。因此,他们都摘了下来一箱,走过去, 然后bonked相互转化(竞争条件!)。

Unfortunately, it didn't help. Paul and Peter, emptying opposite ends of the truck, both saw an almost full sidewalk, but clearly enough space for one more box. So they both picked off a box, walked over, then bonked into each other (race condition!).

最后昆西队列出现了。他提出了三个新的规则:

Finally Quincy Queue shows up. He makes three new rules:

  1. 保罗/彼得:你们俩要和我商量一下,以确保有 的空白处,然后空投:

  1. Paul/Peter: you both have to check with me to make sure there's an empty spot before you dropoff:

康拉德/卡尔/查理:你要和我商量一下,使 确保你拿起面前有一个框:

Conrad/Carl/Charlie: you have to check with me to make sure there's a box before you pick up:

最后,因为只有我一个,我不能跟踪 这如果超过一个人是搞乱与line.So连 如果我在步骤1或2给你开了绿灯,你仍然需要 检查,以确保没有其他人就行了。

And finally, because there's only one of me, I can't keep track of this if more than one guy is messing with the line.So even if I gave you a green light in step 1 or 2, you still need to check to make sure no one else is on the line.

所以,彼得/保罗的最终规则变为:

So Peter/Paul's final rules become:

waitFor(spaceOnSideWalk)
waitFor(permissionToUseSideWalk))
dropBoxOnSidewalk(box)
nowSomeoneElseCanUse(permissionToUseSideWalk))
nowSomeoneElseCanUse(boxesOnSideWalk)

(和查理/卡尔/康拉德互补)

(and Charlie/Carl/Conrad complementary)

如果你想

waitFor == decrement == P
nowSomeoneElseCanUse == increment == V

那么你就必须完全维基百科页面上的alogrithm。

Then you'll have exactly the alogrithm on the wikipedia page.

这篇关于有人可以解释生产国和消费PV的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆