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

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T