首页 > 解决方案 > 如何检测读写器问题中的饥饿

问题描述

我对我的这段代码有疑问。这是经典的读者-作者问题。我按照这个维基百科页面上的伪代码来解决第一个让作家挨饿的问题。我想知道我是如何真正注意到作家们正在挨饿的。

我尝试将 shared_variable 的打印语句放在不同的地方,但这并没有给我太多的洞察力。但也许我只是不明白发生了什么。有人可以向我解释我是如何直观地看到饥饿发生的吗?谢谢!读取器或写入器尝试读取或写入的尝试次数作为命令行参数给出。

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


// Compile it like so: gcc assignment2.c -lpthread


// Shared variables (semaphore and integer)
static sem_t rw_mutex;
static sem_t mutex;
static int read_count = 0;

// Shared variable
int shared_variable = 0;

static void *writerAction(void *arg){
    int number_attempt = *((int *) arg);
    int attempt = 0;
    do{
        sem_wait(&rw_mutex);
        shared_variable = shared_variable + 10;
        sem_post(&rw_mutex);
        attempt++;
    }while(attempt < number_attempt);
}

static void *readerAction(void *arg){
    int number_attempt = *((int *) arg);
    int attempt = 0;
   do{
        sem_wait(&mutex);
        read_count++;
        // waiting to be able to read for the possible writer
        if (read_count == 1 ){
            sem_wait(&rw_mutex); // get the lock so that writter can't write!
        }

        // Release the read_count variable
        sem_post(&mutex);
        sem_wait(&mutex);
        read_count--;

        if (read_count == 0){
            sem_post(&rw_mutex); // release the lock so that writter can write
        }
        sem_post(&mutex);
        attempt++;
    } while(attempt < number_attempt);
}




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

    int number_writers = 10;
    int number_readers = 500;
    int reader_repeat_count = atoi(argv[2]);
    int writer_repeat_count = atoi(argv[1]);

    // Instantiating the threads for the writters and readers
    pthread_t writer_threads[number_writers];
    pthread_t reader_threads[number_readers];

    // Initation of semaphores
    sem_init(&rw_mutex, 0, 1);
    sem_init(&mutex, 0, 1);

    printf("Start creation of Readers\n");
    for(int i = 0; i <number_readers; i++){
        pthread_create(&reader_threads[i], NULL, readerAction, &reader_repeat_count);
    }
    printf("Start creation of Writers\n");
    for(int i = 0; i < number_writers; i++){
        pthread_create(&writer_threads[i], NULL, writerAction, &writer_repeat_count);
    }

    // All the actions is hapenning here
    printf("Wait for Readers\n");
    for(int i = 0; i < number_readers; i++){
        printf("Waiting for : %d\n",i);
        pthread_join(reader_threads[i], NULL);
    }

    printf("Wait for Writers\n");
    // Collect all the writers
    for(int i = 0; i < number_writers; i++){
        printf("Waiting for : %d\n",i);
        pthread_join(writer_threads[i], NULL);
    }

    // Results
    printf("The shared variable is : %d\n",shared_variable);
   }

标签: coperating-systemsemaphore

解决方案


首先有语法错误,writerAction和readerAction的返回类型应该是void而不是void*

为了查看 writer starvation,您可以在 writer 尝试获取 rw_mutex(对 sem_wait(&rw_mutex) 的调用)之前打印“writer trying to write”。当作者更新共享变量时,在临界区中添加另一个打印。在阅读器的入口部分之后再添加一个 printf 。这是哪个代码。

// Release the read_count variable
sem_post(&mutex);
printf("reader reading shared value %d\n", shared_variable);

现在,当您运行具有大量重复计数的代码时,您将看到“作者正在尝试写入”,然后您将看到大量阅读器打印输出,而不是作者更新共享变量的打印输出,这将证明阅读器正在挨饿writer 不允许它更新变量。


推荐阅读