William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码

本文主要是介绍William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码能跑,不过实在是观察不出来什么读者优先,或者写者优先。不知道这种优先级的冲突的场景从何而来,也就不知道书上讲得那些情况到底是什么。

特别是在下面代码的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 readerb++;    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");
}



这篇关于William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/796736

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环