读者-作家访问多个读者 [英] reader-writer accessing multiple readers

查看:64
本文介绍了读者-作家访问多个读者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在UNIX中实现WRITER-READER问题时无法解决的问题. 第一个是我不知道,我该如何修改代码以使其正常工作,就像线程总是在调用进入阅览室一样.例如,当作家在阅览室中时,读者正在等待访问阅览室.当作家逃离阅览室而读者进入阅览室时,他仍在等待机会. 第二个是我不知道如何修改代码以允许一些读者进入阅览室.在我的代码中,阅览室中只能同时有一个线程. 第三个是,如何识别作家或读者是否挨饿?谁在饿死我的代码?

I have a few problems I can't solve in implementing WRITER-READER problem in UNIX. First one is that I have no idea, how can I modify the code to work like threads are always calling to enter reading room. For example, when a writer is in the reading room, readers are waiting to access the reading room. When writer is escaping the reading room and readers are entering the reading room, he still is waiting for his chance. The second one is that I have no idea how to modify the code to allow a few readers to enter the reading room. In my code only one thread can be in the same time in the reading room. The third one is, how to recognize if writer or reader is starving? Which one is starving in my code?

这是代码:

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#define READERS 15
#define WRITERS 10

int bufferw = 0, bufferr = 0, counterw = WRITERS, counterr = READERS;
int i;

pthread_mutex_t mwrite, mread;
pthread_cond_t condw, condr;
pthread_t r[READERS], w[WRITERS];

void *writer(void *ptr) {

    pthread_mutex_lock(&mwrite);

    {
      counterr = READERS;
      counterw = WRITERS;
      ++bufferw;    

     for(i=0; i<READERS; i++) while(bufferr > 0) pthread_cond_wait(&condw, &r[i]);

      printf("WRITER ENTERING!");

      pthread_mutex_unlock(&mwrite);

      bufferw--;
    }

    pthread_cond_signal(&condr);

    pthread_exit(0);
}

void *reader(void *ptr) {

    counterr = READERS;
    counterw = WRITERS;

    {
      ++bufferr;

      for(i=0; i<WRITERS; i++) while(bufferw == 1) pthread_cond_wait(&condr, &w[i]);

      printf("READER ENTERING!");

      bufferr = 0;
    }

    pthread_cond_signal(&condw);
    pthread_exit(0);
}


int main(int argc, char* argv[]) {

    pthread_mutex_init(&mwrite, 0);
    pthread_mutex_init(&mread, 0);

    pthread_cond_init(&condw, 0);
    pthread_cond_init(&condr, 0);

    for(i=0; i<WRITERS; i++) pthread_create(&w[i], NULL, writer, NULL);
    for(i=0; i<READERS; i++) pthread_create(&r[i], NULL, reader, NULL);

    for(i=0; i<WRITERS; i++) pthread_join(w[i], NULL);
    for(i=0; i<READERS; i++) pthread_join(r[i], NULL);

    pthread_cond_destroy(&condw);
    pthread_cond_destroy(&condr);
    pthread_mutex_destroy(&mwrite);
    pthread_mutex_destroy(&mread);

 return 0;
}

提前感谢您的帮助!

//在这种情况下如何避免比赛?

// How can I avoid race in this case?

推荐答案

您可以使用一个互斥锁和两个条件变量来实现此目的.

you could use one mutex and two conditional variables to implement this.

reader( ) {
     pthread_mutex_lock(&m);
     while (!(writers == 0))
     pthread_cond_wait(&readersQ, &m);
     readers++;
     pthread_mutex_unlock(&m);

     /* actual read */

     pthread_mutex_lock(&m);
     if (--readers == 0)
     pthread_cond_signal(&writersQ);
     pthread_mutex_unlock(&m);
    }



writer( ) {
 pthread_mutex_lock(&m);
 writers++;
 while (!((readers == 0) && (active_writers == 0))) {
 pthread_cond_wait(&writersQ, &m);
 }
 active_writers++;
 pthread_mutex_unlock(&m);

 /* actual write */

 pthread_mutex_lock(&m);
 writers--;
 active_writers--;
 if (writers > 0)
   pthread_cond_signal(&writersQ);
 else
   pthread_cond_broadcast(&readersQ);
 pthread_mutex_unlock(&m);
}

此实现对读者不公平,这意味着,如果有作家写作,那么读者将永远无法选择阅读.这是因为写作比阅读更重要.如果您不这样认为,我还可以提供对作家不公平的版本.

This implementation is unfair to the readers, which means if there is a writer writing, readers will never get a choice to read. This is because writing is more important than reading. If you don't think so, I can also provide a version which is unfair to writers.

当作者等于零时,读者可以选择仅阅读.如果只有一个作家,那么作家将不会为零,并且读者将无法阅读.

The reader will get a choice to read only when writers are equal to zero. If there is a single writer, the writer will not be zero and the reader cannot read.

如果有多个作者,则变量active_writers将确保一次只能有一个作者.

If there are multiple writers, the variable active_writers will make sure that only one writer could write at a time.

编辑

是挨饿作家的版本.对于读者来说,这是相同的代码.

below is the version starve writers. For the reader, it is the same code.

writer( ) {
 pthread_mutex_lock(&m);
 while(!((readers == 0) &&(writers == 0)))
     pthread_cond_wait(&writersQ, &m);
 writers++;
 pthread_mutex_unlock(&m);

 /* actual write */

 pthread_mutex_lock(&m);
 writers--;
 pthread_cond_signal(&writersQ);
 pthread_cond_broadcast(&readersQ);
 pthread_mutex_unlock(&m);
}

这篇关于读者-作家访问多个读者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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