您的位置:首页 > 其它

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

2015-09-03 20:23 489 查看
读者--写者问题

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

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

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

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

一、读者优先

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

代码实现

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: