第二种算法解决方案,以飨读者,作家 [英] Second Algorithm Solution to Readers-Writer

查看:145
本文介绍了第二种算法解决方案,以飨读者,作家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个真的很难理解第二算法的读者,作家的问题。我理解的一般概念,认为作家将获得优先的读者(读者可以饿死)。我甚至知道了条件变量来实现这一算法读卡器/ C语言写锁++ 的。然而,信号量和放大器;互斥的实现是没有意义的我。这是维基百科的一个例子:

I'm having a really hard time understanding the Second Algorithm to the Readers-Writers problem. I understand the general concept, that the writers will get priority over the readers (readers can starve). I even understand the conditional variable implementation of this algorithm Reader/Writer Locks in C++. However, the semaphore & mutex implementation makes no sense to me. This is an example from Wikipedia:

int readcount, writecount; (initial value = 0)
semaphore mutex 1, mutex 2, mutex 3, w, r ; (initial value = 1)

READER
  P(mutex 3);
    P(r);
      P(mutex 1);
        readcount := readcount + 1;
        if readcount = 1 then P(w);
      V(mutex 1);
    V(r);
  V(mutex 3);

  reading is done

  P(mutex 1);
    readcount := readcount - 1;
    if readcount = 0 then V(w);
  V(mutex 1);


WRITER
    P(mutex 2);
      writecount := writecount + 1;
      if writecount = 1 then P(r);
    V(mutex 2);

  P(w);
    writing is performed
  V(w);

  P(mutex 2);
    writecount := writecount - 1;
    if writecount = 0 then V(r);
  V(mutex 2);

[http://en.wikipedia.org/wiki/Readers-writers_problem][2]

我不明白这三个信号量(互斥3,R和互斥1)在读锁。是不是一信号灯不够的readcount?

I don't understand what the three semaphores (mutex 3, r, and mutex 1) are for in the reader lock. Isn't one semaphore enough for the readcount?

推荐答案

互斥1 保护 readcount 变量; mutext 2 保护 writecount 变量;互斥研究保护读取操作和mutext 是W 保护写操作。

mutex 1 protects the readcount variable; mutext 2 protects writecount variable; mutex r protects the reading operations and mutext w protects the writing operations.

1)让我们假设一个作家进来:

1) Let's suppose a writer comes in:

信号互斥2 和增量 writercount 占额外的作家(本身) 因为它是可以改变的唯一方法 writercount (因为它是控股互斥2 ),它可以安全地测试是否它是唯一的作家( writercount == 1 ),如果是真的,它标志着互斥研究,以保护读者来自何处在 - 其他作家( writercount> 1 ),可享受互斥研究被暗示已

Signals mutex 2 and increments writercount to account for the extra writer (itself) Since it is the only process that can change writercount (as it is holding mutex 2), it can safely test whether it is the only writer (writercount==1), if true, it signals mutex r to protect readers from coming in -- other writers (writercount > 1) can enjoy the mutex rbeing signaled already.

笔者随后指示互斥是W 来保护它的变化来自其他(并行)的作家。

The writer then signals mutex w to protect its changes from other (concurrent) writers.

最近作家( writecount == 1 )释放互斥研究来让读者执行任务。

Last writer (writecount==1) releases mutex r to let readers perform their tasks.

2)让我们假设读者进来:

2) Let's suppose a reader comes in:

信号互斥3 来保护其他读者的阅读者设置的逻辑;然后发信号互斥研究从其他作家(记住,研究,而作家的工作是信号)来保护;然后信号互斥1 来保护readcount(其他读者可能被退出),如果它是第一个读者( readercount == 1 ),信号互斥是W 从作家(现在不包括作家从执行它们的操作)。

Signals mutex 3 to protect the readers' setup logic from other readers; then signals mutex r to protect from other writers (remember, r is signaled while writers are operating); then signals mutex 1 to protect readcount (from other readers that might be exiting) and if it is the first reader (readercount == 1), signals mutex w to protect from writers (now excludes writers from performing their operations).

读可以并行完成,所以没有保护其他读者的需要,而阅读(记住,互斥是W 被关押在这一点上,从编剧所以没有intereference)

Reading can be done parallel, so no protection is needed from other readers while reading (remember, mutex w is being held at this point, so no intereference from writers)

然后最后一个读者复位写互斥(是W ),允许作家。

Then the last reader resets the write mutex (w) to allow writers.

这prevents作家饥饿的技巧是,作家伪装成读者(信令互斥 P 时),所以有越来越定,即使有一个很好的机会许多读者。此外,互斥3 $ P $从等待互斥pvents太多的读者研究,这样的作家有很好的机会信号研究当他们来了。

The trick that prevents writer starvation is that writers pose as readers (when signaling mutex p), so have a good chance of getting scheduled even when there are many readers. Also, mutex 3 prevents too many readers from waiting on mutex r, so writers have a good chance to signal r when they come.

这篇关于第二种算法解决方案,以飨读者,作家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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