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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部