Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用互斥体和pthread的C读写程序_C_Pthreads_Posix_Mutex - Fatal编程技术网

使用互斥体和pthread的C读写程序

使用互斥体和pthread的C读写程序,c,pthreads,posix,mutex,C,Pthreads,Posix,Mutex,我在C语言中遇到了一个读写器问题。有人能解释一下下面代码中发生了什么吗。我不明白pthread_create(&tid,NULL,writer,NULL)行之后执行是如何进行的 #包括 //#包括 #包括 #包括 pthread_mutex_t x,wsem; pthread_t tid; 整数读取计数; 无效初始化() { pthread_mutex_init(&x,NULL); pthread_mutex_init(&wsem,NULL); 读取计数=0; } void*读卡器(void*参

我在C语言中遇到了一个读写器问题。有人能解释一下下面代码中发生了什么吗。我不明白pthread_create(&tid,NULL,writer,NULL)行之后执行是如何进行的

#包括
//#包括
#包括
#包括
pthread_mutex_t x,wsem;
pthread_t tid;
整数读取计数;
无效初始化()
{
pthread_mutex_init(&x,NULL);
pthread_mutex_init(&wsem,NULL);
读取计数=0;
}
void*读卡器(void*参数)
{
国际等待时间;
waittime=rand()%5;
printf(“\nReader正在尝试输入”);
pthread_mutex_lock(&x);
readcount++;
if(readcount==1)
pthread_mutex_lock(&wsem);
printf(“\n%d个读卡器在里面”,readcount);
pthread_mutex_unlock(&x);
睡眠(等待时间);
pthread_mutex_lock(&x);
读取计数--;
if(readcount==0)
pthread_mutex_unlock(&wsem);
pthread_mutex_unlock(&x);
printf(“\n阅读器正在离开”);
}   
void*writer(void*param)
{
国际等待时间;
waittime=rand()%3;
printf(“\nWriter正在尝试输入”);
pthread_mutex_lock(&wsem);
printf(“\nWrite已输入”);
睡眠(等待时间);
pthread_mutex_unlock(&wsem);
printf(“\n编写者将离开”);
睡眠(30);
出口(0);
}
int main()
{
int n1,n2,i;
printf(“\n输入读取器的数量:”;
scanf(“%d”&n1);
printf(“\n输入写入程序数:”);
扫描频率(“%d”和“n2”);

对于(i=0;i这个好问题,试着用这个。它似乎和读者-作者问题混淆了

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);
   }
}

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

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
}

实际上,我上面粘贴的代码正在工作。您能告诉我在代码中pthread_create(&tid,NULL,writer,NULL)行之后执行是如何进行的吗?我(在输出时)在“writer正在尝试输入”之后得到了“Reader's leaving”行这是怎么发生的……为什么你不能给OP一个问题的答案呢?我认为这里没有请求代码转储。你不需要调用
initialize()
函数来初始化互斥锁。在
main()
中,你不需要调用
initialize()函数
。这是初始化两个互斥体
x
wsem
的函数。您需要首先了解读写器的问题是什么,然后跳转到代码。@DaV抱歉,我忘了在main()中调用initialize()。我在执行上述代码时没有任何问题。您能解释一下睡眠后发生了什么()我关心reader函数中sleep语句之后的执行流help@brokenfoot我知道读写器的问题是什么,但我不知道如何使用信号量和pthread为读写器编写C代码,因此,如果您向我解释线程的概念,并向我解释上面的代码将在sleep语句之后出现在reader函数中。
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
}