经典同步问题--读者和写者问题
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; }
相关文章推荐
- 各种分类算法比较
- 指针之函数的参数是数组的时候的本质
- 2015 ACM多校训练第三场
- 跨域调用webapi
- 偶遇问题 - - 空格导致行内元素水平间隔问题
- linux命令进阶小记
- 最大公约数Stein算法之verilog实现
- LCS (最长公共子序列)
- Cocos2d-x xml解析
- 关于ListView的优化!
- 【extern】如何使用关键字extern "C"?
- ViewPager+Fragment实现滑动显示,且Fragment里面又放Fragment+viewPager
- Delphi考虑sql注入 QuotedStr
- Block Formatting Contexts
- 思维值惰性
- hdoj 1385 Minimum Transport Cost
- Maven系列2--pom.xml 配置详解
- js数据结构与算法存储结构
- Redis安装
- 【英语】Bingo口语笔记(62) - 生气道歉场景的表达