例说读者写者模型

来源:互联网 发布:orcal数据库设计工具 编辑:程序博客网 时间:2024/05/13 13:21

前面我们学习了生产者和消费者模型,想了解一下的可以去看看这篇客:
http://blog.csdn.net/bit_clearoff/article/details/55805884

什么是读者写者模型

读者和写者模型是操作系统中的一种同步与互斥机制,它与消费者和生产者模型类似,但也有不同的地方,最明显的一个特点是在读者写者模型中,多个多者之间可以共享“仓库”,读者与读者之间采用了并行机制;而在消费者和生产者模型中,消费者只能有一个独占仓库,消费者与消费者是竞争关系。下图展示了读者与写者模型:
(读者写者模型)


读者写者模型的要具有的条件

  1. 写者是排它性的,即在有多个写者的情况下,只能有一个写者占有“仓库”;
  2. 读者的并行机制:可以运行多个读者去访问仓库;
  3. 如果读者占有了仓库,那么写者则不能占有;

读者写者模型的关系

  1. 读者优先:读者先来读取数据,此时写者处于阻塞状态,当读者都读取完数据后且没有读者了时写者才能访问仓库;
  2. 写者优先:类似与读者优先的情况;
  3. 公平情况:写者与读者访问“仓库”优先级相等,谁先进入优先级队列谁先访问;

读写锁

什么是读写锁

读写锁与互斥量类似,不过读写锁允许更高的并行性。它有三种状态:读模式下加锁状态、写模式下加锁状态、不加锁状态,另外,一次只有一个线程可以占用写模式下的读写锁,但是可以有多个线程可以占有读模式下的读写锁。

读写锁状态

  1. 写加锁状态:当处于写加锁状态时,在被解锁之前,所有试图对锁进行访问的进行都会被阻塞;
  2. 读加锁状态:当处于读加锁状态时,所有试图以读模式对这个锁进行访问的线程都可以获得访问权,但是任何以写模式对此锁进行访问的线程都会被阻塞,直到所有的线程都释放他们的读状态锁为止。

注意:当读写锁处于读模式加锁的状态时,这时有一个线程试图以写模式来获取锁,读写锁通常会阻塞随后的读模式锁请求,这样做事为了避免读模式锁长期占用资源,导致写模式饥饿。


读写锁的接口

1. 读写锁的类型

在Linux系统下,读写锁被定义为pthread_rwlock_t类型,但是其本质上还是一个内存计数器。

2. 读写锁的初始化

使用pthread_rwlock_init函数进行初始化;

 int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr); //return val: success return zero

参数attr表示的是初始化读写锁的属性,如果想使用默认属性,直接将该参数设置为NULL即可。
另外在single UNIX Specification中,在XSI还扩展了用PTHREAD_RWLOCK_INITIALIZER宏来进行初始化。

3. 读写锁的销毁

在释放读写锁的内存前,需要调用pthread_rwlock_destroy函数来对读写锁进行清理工作,在清理函数中一般是释放初始化函数对读写锁分配的资源。

4. 读模式下锁定读写锁

由于读加锁和写加锁这两种状态的实现机制不同,所以他们的加锁函数也就不相同,下面是读加锁函数

       int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);       int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);//成功返回0,失败返回错误码

使用时的注意事项:因为操作系统对读者数量有限制,所以当我们使用读加锁函数时最好能判断一下返回值。

5. 写加锁函数

       int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);       int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//返回值:成功返回0,失败返回错误码

6. 解锁函数

对于读加锁写加锁而言,他们所使用的解锁函数都是相同的。

       int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);       //返回值:成功返回0,失败返回错误码。

相关案例

为了演示本文中提到的读者和写者模型,我做了下面的测试案例,使用线程来模拟读者和写者,使用链表来模拟读者和写着交换数据的仓库,写者负责向链表中添加数据,而读者负责从链表中读取数据,我们来看看使用读写锁后的测试结果:
这里写图片描述

下面给出案例代码:

//读者和写者场景模拟代码

/*************************************************************************    > File Name: Read_Write.c    > Author: LZH    > Mail: 597995302@qq.com     > Created Time: Sun 19 Feb 2017 10:56:59 PM PST ************************************************************************/#include "myList.h"#include<pthread.h>//int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);pthread_rwlock_t lock;void* Pthread_Write(void* arg){    Node_p head=(Node_p)arg;    while(1)    {           sleep(1);        int ret=pthread_rwlock_wrlock(&lock);        if(ret!=0){            perror("wrlock error..\n");            return (void*) -1;        }        int data1=rand()%1000;        int data2=rand()%1000;        PushHead(head,data1);        PushHead(head,data2);        sleep(1);        printf("Writer write data:%d,%d to list.\n",data1,data2);        pthread_rwlock_unlock(&lock);    }    return (void*) 0;}void* Pthread_Read1(void* arg){    Node_p head=(Node_p)arg;    while(1)    {           sleep(1);        int ret=pthread_rwlock_rdlock(&lock);        if(ret!=0){            perror("rdlock error..\n");            return (void*) -1;        }        int data=0;        PopHead(head,&data);        sleep(1);        printf("Reader1 receive data:%d from list.\n",data);        pthread_rwlock_unlock(&lock);    }    return (void*) 0;}void* Pthread_Read2(void* arg){    Node_p head=(Node_p)arg;    while(1)    {           sleep(1);        int ret=pthread_rwlock_rdlock(&lock);        if(ret!=0){            perror("rdlock error..\n");            return (void*) -1;        }        int data=0;        PopHead(head,&data);        sleep(1);        printf("Reader2 receive data:%d from list.\n",data);        pthread_rwlock_unlock(&lock);    }    return (void*) 0;}int main(){    printf("Read and Write model...\n");    Node_p head;    ListInit(&head);    printf("lock:%d\n",lock);    pthread_t tid1,tid2,tid3;    pthread_rwlock_init(&lock,NULL);    pthread_create(&tid1,NULL,Pthread_Write,(void*)head);    pthread_create(&tid2,NULL,Pthread_Read1,(void*)head);    pthread_create(&tid3,NULL,Pthread_Read2,(void*)head);    pthread_join(tid1,NULL);    pthread_join(tid2,NULL);    pthread_join(tid3,NULL);    pthread_rwlock_destroy(&lock);    return 0;}

//链表源文件

#include "myList.h"Node_p AllocNode(int data){    Node_p NewNode=(Node_p)malloc(sizeof(Node));    if(NewNode==NULL)    {        perror("malloc..\n");        return ;    }    NewNode->data=data;    NewNode->next=NULL;    return NewNode;}int IsEmpty(Node_p list){    assert(list);    if(list->next!=NULL)    {        return 0;    }    else    {        return 1;    }}void ListInit(Node_pp head){    *head=AllocNode(0); }void PushHead(Node_p list,int data){    assert(list);    Node_p NewNode=AllocNode(data);    NewNode->next=list->next;    list->next=NewNode;}void DelNode(Node_p node){    assert(node);    free(node);    node=NULL;}void PopHead(Node_p list,int *data){    assert(data);    if(IsEmpty(list))    {        printf("the list empty..\n");        return;    }    Node_p dNode=list->next;    list->next=dNode->next;    *data=dNode->data;    DelNode(dNode);}void ShowList(Node_p list){       assert(list);    Node_p cur=list->next;    while(cur)    {        printf("%d ",cur->data);        cur=cur->next;    }    printf("\n");}void DestroyList(Node_p list){    assert(list);    int data=0;    while(list->next)    {        PopHead(list,&data);    }    free(list);    list=NULL;    printf("list is destroy...\n");}

下面我给出整个案例的源码链接(包含Makefile),大家可以自己下去运行:
https://github.com/clearoff/C-Test/tree/master/%E8%AF%BB%E8%80%85%E4%B8%8E%E5%86%99%E8%80%85%E6%A8%A1%E5%9E%8B

2 0