经典同步问题--读者和写者问题

读者--写者问题

读者--写者问题是互斥问题的一个概括。一组并发的线程要访问一个共享的对象,例如一个主存中的数据结构,

或者是磁盘上的数据库。有些线程只读对象,其他线程只修改对象。只读对象的线程叫做读者,修改对象的线

程的对象叫做写者。写者必须拥有对对象的独占的访问,而读者可以和其他读者共享对象。

按照读者和写者的优先级分为两类问题:


一、读者优先

读者优先,要求不要读者等待,即使有写者等待时,读者也不需要等待。


代码实现

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

sem_t    mutex,source;
int   read_count = 0;//对象的读者
int count = 0;


void *reader(void *argv)
{
    pthread_detach(pthread_self());
    while(1){
        sem_wait(&mutex);
        if(0 == read_count)
            sem_wait(&source);
        read_count++;
        sem_post(&mutex);

        printf("**%ld--count = %d\n",(long)pthread_self(),count);
        sem_wait(&mutex);
        read_count--;
        if(0 == read_count)
            sem_post(&source);
        sem_post(&mutex);
    }
    return NULL;
}

void *writer(void *argv)
{
    pthread_detach(pthread_self());
    while(1){
        sem_wait(&source);
        count++;
        printf("##%ld--count = %d\n",(long)pthread_self(),count);
        sem_post(&source);
    }

    return NULL;
}


int main(int argc,char **argv)//./a.out readers writers
{
    pthread_t   tid1,tid2;
    int   readers = 2,writers = 2,i = 0;
    
    sem_init(&mutex,0,1);
    sem_init(&source,0,1);

    if(3 == argc){
        readers = atoi(argv[1]);
        writers = atoi(argv[2]);
    }

    for( i = 0; i < writers; ++i)
        pthread_create(&tid1,NULL,writer,NULL);
    for( i = 0; i < readers; ++i)
        pthread_create(&tid2,NULL,reader,NULL);

    while(1)
        sleep(5);

    return 0;

}



二、写者优先

写者优先,要求一旦写着准备好写,它就会尽快的完成它写的操作,一个在写者后达到读者必须等待。


代码实现

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

sem_t    mutex,source,queue;//queue用来排队
int   read_count = 0;//对象的读者
int count = 0;


void *reader(void *argv)
{
    pthread_detach(pthread_self());
    while(1){
        sem_wait(&queue);
        sem_wait(&mutex);
        if(0 == read_count)
            sem_wait(&source);
        read_count++;
        sem_post(&mutex);
        sem_post(&queue);

        printf("**%ld--count = %d\n",(long)pthread_self(),count);
        sem_wait(&mutex);
        read_count--;
        if(0 == read_count)
            sem_post(&source);
        sem_post(&mutex);
    }
    return NULL;
}

void *writer(void *argv)
{
    pthread_detach(pthread_self());
    while(1){
        sem_wait(&queue);
        sem_wait(&source);
        sem_post(&queue);
        count++;
        printf("##%ld--count = %d\n",(long)pthread_self(),count);
        sem_post(&source);
    }

    return NULL;
}


int main(int argc,char **argv)//./a.out readers writers
{
    pthread_t   tid1,tid2;
    int   readers = 2,writers = 2,i = 0;
    
    sem_init(&queue,0,1);
    sem_init(&mutex,0,1);
    sem_init(&source,0,1);

    if(3 == argc){
        readers = atoi(argv[1]);
        writers = atoi(argv[2]);
    }

    for( i = 0; i < writers; ++i)
        pthread_create(&tid1,NULL,writer,NULL);
    for( i = 0; i < readers; ++i)
        pthread_create(&tid2,NULL,reader,NULL);

    while(1)
        sleep(5);

    return 0;

}








  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值