【问题标题】:Reader Writer program in C using mutexes and pthreads使用互斥锁和 pthread 的 C 语言中的 Reader Writer 程序
【发布时间】:2014-05-30 11:05:54
【问题描述】:

我在 C 中遇到了读写器问题。谁能解释一下下面代码中发生了什么。我不明白 pthread_create(&tid,NULL,writer,NULL) 行之后的执行流程。

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

pthread_mutex_t x,wsem;
pthread_t tid;
int readcount;

void intialize()
{
    pthread_mutex_init(&x,NULL);
    pthread_mutex_init(&wsem,NULL);
    readcount=0;
}

void * reader (void * param)
{
    int waittime;
    waittime = rand() % 5;
    printf("\nReader is trying to enter");
    pthread_mutex_lock(&x);
    readcount++;
    if(readcount==1)
        pthread_mutex_lock(&wsem);
    printf("\n%d Reader is inside ",readcount);
    pthread_mutex_unlock(&x);
    sleep(waittime);
    pthread_mutex_lock(&x);
    readcount--;
    if(readcount==0)
        pthread_mutex_unlock(&wsem);
    pthread_mutex_unlock(&x);
    printf("\nReader is Leaving");
}   

void * writer (void * param)
{
    int waittime;
    waittime=rand() % 3;
    printf("\nWriter is trying to enter");
    pthread_mutex_lock(&wsem);
    printf("\nWrite has entered");
    sleep(waittime);
    pthread_mutex_unlock(&wsem);    
    printf("\nWriter is leaving");
    sleep(30);
    exit(0);
}

int main()
{
    int n1,n2,i;    
    printf("\nEnter the no of readers: ");
    scanf("%d",&n1);
    printf("\nEnter the no of writers: ");
    scanf("%d",&n2);
    for(i=0;i<n1;i++)
        pthread_create(&tid,NULL,reader,NULL);  
    for(i=0;i<n2;i++)
        pthread_create(&tid,NULL,writer,NULL);
    sleep(30);
    exit(0);
}

【问题讨论】:

  • 您不会调用 initialize() 函数来初始化互斥锁。
  • main() 内,您不调用函数initialize()。这是初始化两个互斥锁 xwsem 的函数。
  • 你需要先了解什么是读写器问题,然后跳转到代码。
  • @DaV 抱歉,我忘了在 main() 中调用 initialize()。我在执行上述代码时没有任何问题,你能解释一下 sleep() 之后发生了什么我关心阅读器函数中 sleep 语句后的执行流程,请帮助
  • @brokenfoot 我知道读写器问题是什么,但我不知道如何使用信号量和 pthread 为读写器编写 C 代码,所以如果你向我解释一下线程并解释一下上面代码在 reader 函数中的 sleep 语句之后的执行流程。

标签: c pthreads posix mutex


【解决方案1】:

如果您没有得到答案,那么您可以尝试以下代码。尝试与上面给出的代码进行比较。

semaphore mutex = 1;                 // Controls access to the reader count
semaphore db = 1;                    // Controls access to the database
int reader_count;                    // The number of reading processes accessing the data

Reader()
{
  while (TRUE) {                     // loop forever
     down(&mutex);                          // gain access to reader_count
     reader_count = reader_count + 1;       // increment the reader_count
     if (reader_count == 1)
         down(&db);                         // if this is the first process to read the database,
                                            // a down on db is executed to prevent access to the 
                                            // database by a writing process
     up(&mutex);                            // allow other processes to access reader_count
     read_db();                             // read the database
     down(&mutex);                          // gain access to reader_count
     reader_count = reader_count - 1;       // decrement reader_count
     if (reader_count == 0)
         up(&db);                           // if there are no more processes reading from the 
                                            // database, allow writing process to access the data
     up(&mutex);                            // allow other processes to access reader_countuse_data();
                                            // use the data read from the database (non-critical)
}

Writer()
{
  while (TRUE) {                     // loop forever
     create_data();                         // create data to enter into database (non-critical)
     down(&db);                             // gain access to the database
     write_db();                            // write information to the database
     up(&db);                               // release exclusive access to the database
}

【讨论】:

    【解决方案2】:

    很好的问题尝试使用它。您似乎对读写器问题感到困惑。

    void reader(){
       while(1){
          wait(x);
            readcount++;
            if (readcount==1) 
                 wait(wsem);
          signal(x);
          doReading();
          wait(x);
            readcount--;
            if (readcount==0)
                 signal(wsem);
          signal(x);
       }
    }
    

    【讨论】:

    • 实际上我上面粘贴的代码是有效的。你能告诉我代码中 pthread_create(&tid,NULL,writer,NULL) 行之后的执行流程吗?在“作家试图进入”之后,我得到(在输出)“读者离开”行这是怎么回事......
    • 你为什么不能直接给 OP 回答他的问题?我认为这里没有请求代码转储。
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2016-09-05
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多