【问题标题】:Reader Writer Problem With Writer Priority Problem读写器优先级问题的读写器问题
【发布时间】:2021-04-09 10:12:40
【问题描述】:

我在学习有关操作系统的更多信息时遇到了这个问题。在我的代码中,我尝试让阅读器具有优先权并且它起作用了,所以接下来我对其进行了一些修改以使作者具有优先权。当我运行代码时,输​​出完全一样,似乎作者没有优先权。这是带有 cmets 的代码。我不确定我做错了什么,因为我修改了很多代码,但如果我根本不更改它,输出仍然是一样的。

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

/*
This program provides a possible solution for first readers writers problem using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You can always play with these values.
*/

// Semaphore initialization for writer and reader 
sem_t wrt;
sem_t rd;

// Mutex 1 blocks other readers, mutex 2 blocks other writers
pthread_mutex_t mutex1;
pthread_mutex_t mutex2;

// Value the writer is changing, we are simply multiplying this value by 2
int cnt = 2;

int numreader = 0;
int numwriter = 0;

void *writer(void *wno)
{   
    pthread_mutex_lock(&mutex2);
    numwriter++;
    if(numwriter == 1){
        sem_wait(&rd);      
    }
    pthread_mutex_unlock(&mutex2);
    sem_wait(&wrt); 

    // Writing Section
    cnt = cnt*2;
    printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);

    sem_post(&wrt);
    pthread_mutex_lock(&mutex2);
    numwriter--;
    if(numwriter == 0){
        sem_post(&rd);
    }
    pthread_mutex_unlock(&mutex2); 

}
void *reader(void *rno)
{   
    sem_wait(&rd);
    pthread_mutex_lock(&mutex1);
    numreader++;
    if(numreader == 1){
        sem_wait(&wrt);
    }
    pthread_mutex_unlock(&mutex1);
    sem_post(&rd); 

    // Reading Section
    printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);

    
    pthread_mutex_lock(&mutex1);
    numreader--;
    if(numreader == 0){
        sem_post(&wrt);
    }
    pthread_mutex_unlock(&mutex1); 
}

int main()
{   

    pthread_t read[10],write[5];
    pthread_mutex_init(&mutex1, NULL);
    pthread_mutex_init(&mutex2, NULL);
    sem_init(&wrt,0,1);
    sem_init(&rd,0,1);

    int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the writer and reader

    for(int i = 0; i < 5; i++) {
        pthread_create(&write[i], NULL, (void *)writer, (void *)&a[i]);
    }

    for(int i = 0; i < 10; i++) {
        pthread_create(&read[i], NULL, (void *)reader, (void *)&a[i]);
    }

    for(int i = 0; i < 5; i++) {
        pthread_join(write[i], NULL);
    }

    for(int i = 0; i < 10; i++) {
        pthread_join(read[i], NULL);
    }

    pthread_mutex_destroy(&mutex1);
    pthread_mutex_destroy(&mutex2);
    sem_destroy(&wrt);
    sem_destroy(&rd);

    return 0;
    
}

输出(两者都是一样的。我认为如果作家有优先权,它会首先改变,然后会被读取):

【问题讨论】:

  • 请提供输出并解释您认为错误的原因。
  • @kaylum 添加到主帖中。
  • 互斥量和信号量与“优先级”之间没有链接。当你的线程可以得到他们的手时,就是这样。
  • @kaylum 有道理,我该如何进行链接?
  • 首先你的目标是什么真正?其中生产者和消费者之间交换数据的好方法是使用队列,而主动等待不是一个好方法

标签: c linux operating-system


【解决方案1】:

替代语义

您想做的大部分事情都可以用更少的开销来完成。例如,在经典的读写器问题中,读者不需要阻塞其他读者。

您也许可以将读写器模式替换为发布者-消费者模式,该模式通过获取-消耗内存排序来管理指向数据块的指针。如果一个线程在最初写入后需要更新相同的内存块,则您只需要锁定。

POSIX 和 Linux 在系统库中实现了读写器锁,旨在避免饥饿。这很可能是您想要的高级构造。

如果您仍想实现自己的,一种实现将使用当前读取器的计数、待处理写入器的计数和指示写入是否正在进行的标志。它将所有这些值打包到一个原子位域中,并通过比较和交换进行更新。

读取器线程将检索该值,检查是否有饥饿的写入器在等待,如果没有,则增加读取器的计数。如果有写入器,它会后退(可能会旋转并让出 CPU,可能会在条件变量上休眠)。如果正在进行写入,它会等待完成。如果它只看到正在进行的其他读取,则继续。

Writer 线程会检查是否有任何读取或写入正在进行。如果是这样,它们会增加等待写入者的数量,然后等待。如果没有,他们会设置 write-in-progress 位并继续。

将所有这些字段打包到同一个原子位域中可以保证没有线程认为使用缓冲区是安全的,而另一个线程认为写入是安全的:如果两个线程同时尝试更新状态,一个总是会失败.

如果你坚持使用信号量

您仍然可以让阅读器线程检查写入器信号量上的sem_getvalue(),如果他们看到任何饥饿的写入器正在等待,请退出。一种方法是等待条件变量,线程在完成缓冲区时发出信号。读取器在写入器等待时看到它持有互斥锁,可以尝试唤醒一个写入器线程并重新进入睡眠状态,而仅看到其他读取器正在等待的读取器可以唤醒读取器,这将唤醒下一个读取器,等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多