William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码
来源:互联网 发布:java是面向对象的吗 编辑:程序博客网 时间:2024/06/05 06:27
代码能跑,不过实在是观察不出来什么读者优先,或者写者优先。不知道这种优先级的冲突的场景从何而来,也就不知道书上讲得那些情况到底是什么。
特别是在下面代码的writer中,如果引入 sem_wait(&z)的话,writer进程会一直阻塞,从而引起读者也阻塞。去掉后,呈现出读者优先的状况。
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <errno.h>#include <sys/ipc.h>#include <semaphore.h>#include <fcntl.h>void * reader(void *) ;void *writer (void *) ;sem_t x,y,z,wsem,rsem ;int readcount=0 ;int writecount=0 ;main(){ int a=1,b=1; system("clear"); sem_init(&wsem,0,1) ; sem_init(&x,0,1) ; sem_init(&rsem,0,1) ; sem_init(&y,0,1) ; sem_init(&z,0,1) ; pthread_t r1,r2,r3; pthread_t w1,w2,w3,w4,w5 ; pthread_create(&r1,NULL,reader,(void *)a); a++; pthread_create(&r2,NULL,reader,(void *)a); a++; pthread_create(&w1,NULL,writer,(void *)b); b++; pthread_create(&w2,NULL,writer,(void *)b); b++; pthread_create(&r3,NULL,reader,(void *)a); a++; pthread_create(&w3,NULL,writer,(void *)b); b++; pthread_create(&w4,NULL,writer,(void *)b); b++; pthread_create(&w5,NULL,writer,(void *)b); //add a reader b++; printf("main begin join \n"); pthread_join(r1,NULL); pthread_join(r2,NULL); pthread_join(w1,NULL); pthread_join(w2,NULL); pthread_join(r3,NULL); pthread_join(w3,NULL) ; pthread_join(w4,NULL); pthread_join(w5,NULL); printf("main end join\n"); sleep(30); printf("main terminated\n");}void * reader(void * arg){ int c=(int)arg ; printf("\nreader %d is created",c); //sleep(1); //sem_wait(&z) ; { sem_wait(&rsem) ; { sem_wait(&x) ; readcount++; if( readcount == 1){ sem_wait(&wsem) ; } sem_post(&x) ; } sem_post(&rsem) ; } //sem_wait(&z) ; //sleep(1); /*Critcal Section */ printf("\nreader %d is reading\n ",c); sleep(10) ; /* critical section completd */ sem_wait(&x) ; readcount-- ; if(readcount==0) sem_post(&wsem) ; printf("\nreader %d finished reading,readCount=%d\n",c,readcount); sem_post(&x) ;}void * writer(void * arg){ int c=(int)arg ; printf("\n--------------------writer %d is created",c); sleep(5); sem_wait(&y) ; writecount++; if( writecount == 1){ sem_wait(&rsem) ; } sem_post(&y) ; sem_wait(&wsem) ; printf("\n--------------------writer %d is writing\n",c) ; sleep(1); sem_post(&wsem) ; sem_wait(&y) ; writecount--; printf("\n--------------------writer %d finished writing,writecount=%d\n",c,writecount); if( writecount == 0){ sem_post(&rsem) ; } sem_post(&y) ; }
编译方法 : gcc -o pvrww pv-posix-reader-writer.c -lpthread
这里采用posix的信号量机制。
下面修改代码,能达到写者优先的目的:只要有产生了一个写者线程,那么正在运行的读者进程会在执行结束后,立刻被终止调度,让写者
先运行。
#include <stdio.h>先#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <errno.h>#include <sys/ipc.h>#include <semaphore.h>#include <fcntl.h>sem_t x,y,z,wsem,rsem ;int readcount=0 ;int writecount=0 ;void * reader(void * arg){ int c= *(int *)arg; printf("\nreader %d is created",c); sem_wait(&z) ; sem_wait(&rsem) ; sem_wait(&x) ; readcount++; printf("\n-------------readcount=%d----- ",readcount); if( readcount == 1){ sem_wait(&wsem) ; } sem_post(&x) ; sem_post(&rsem) ; sem_wait(&z) ; /*Critcal Section */ printf("\nreader %d is reading\n ",c); sleep(10); /* critical section completd */ sem_wait(&x) ; readcount-- ; if(readcount==0) sem_post(&wsem) ; printf("\nreader %d finished reading,readCount=%d\n",c,readcount); sem_post(&x) ; pthread_exit(0);}void * writer(void * arg){ int c= *(int *)arg; printf("\n--------------------writer %d is created",c); //sleep(5); sem_wait(&y) ; writecount++; printf("\n--------------------writer %d is created,and writecount ==%d",writecount); if( writecount == 1){ if( readcount <= 1 ){ sem_post(&z) ; sem_post(&rsem) ; }else{ sem_wait(&rsem) ; } } sem_post(&y) ; sem_wait(&wsem) ; printf("\n--------------------writer %d is writing\n",c) ; sleep(2); sem_post(&wsem) ; sem_wait(&y) ; writecount--; printf("\n--------------------writer %d finished writing,writecount=%d\n",c,writecount); if( writecount == 0){ sem_post(&rsem) ; } sem_post(&y) ; pthread_exit(0); }main(){ int a=1,b=1; sem_init(&wsem,0,1) ; sem_init(&x,0,1) ; sem_init(&rsem,0,1) ; sem_init(&y,0,1) ; sem_init(&z,0,1) ; pthread_t PReader[3]; pthread_t PWriter[5]; int k =0; for(k = 1; k <= 3; k++) //创建顾客线程 { pthread_create(&(PReader[k-1]),NULL,(void *)reader,&k); srand(time(0)); sleep(rand() % 2 + 1); //1到3的随机数 } for(k = 1; k <= 5; k++) //创建顾客线程 { pthread_create(&(PWriter[k-1]),NULL,(void *)writer,&k); srand(time(0)); sleep(rand() % 2 + 1); //1到3的随机数 } for(k = 0; k < 3; k++){ pthread_join(PReader[k],NULL); } sleep(30); for(k = 1; k <= 5; k++){ pthread_kill(PWriter[k-1],0); } //sleep(30); printf("\nmain terminated\n");}
阅读全文
0 0
- William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码
- C 优先读者 读者/写者问题
- 优先读者/写者问题—操作系统
- 优先读者的读者/写者问题的算法设计
- 有名信号量实现读者-写者问题(读者优先)
- linux 读者写者问题 读者优先c++
- 读者写者问题之读者优先
- “读者-写者问题”的写者优先算法实现
- 读者-写者同步问题的写者优先实现
- OS: 读者写者问题(写者优先+LINUX+多线程+互斥量+代码)
- 操作系统的优先读者和优先写着的读者/写着问题
- 操作系统的优先读者和优先写着的读者/写着问题
- 读者写者 问题C线程实现 linux平台
- 操作系统内核实验之 读者-写者实现
- 读者-写者优先
- (操作系统原理·第三章)读者-写者问题
- 操作系统中,生产者消费者问题&读者写者问题
- linux读者写者问题
- 为什么初始化远程仓库要使用git init --bare
- 多态
- 设计模式概述
- 正则表达式
- 习惯养成记
- William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码
- Java串口编程学习1-环境配置(64位Win7)
- 连接池简介
- HDU5444-二叉树模拟-Elven Postman
- Android-单点触摸-多点触摸
- 循环保存n张图像 Matlab版
- 把exe文件设置为开机启动(win7)
- matlab调用CVX工具包
- JSP EL 表达式 常用