Write the code about Readers-Writer Problem Implementation in C++.1、 A data set is shared among several concurrent processes.Problem – allow multiple readers (at most 8) to read at the same time. Only one single writer can access the shared data at the same time.

时间: 2024-02-01 22:02:45 浏览: 35
Here is an implementation of Readers-Writer Problem in C++ using Semaphores: ```cpp #include <iostream> #include <thread> #include <mutex> #include <semaphore.h> using namespace std; const int MAX_READERS = 8; int data = 0; int active_readers = 0; bool writer_active = false; sem_t mutex_read, mutex_write, reader_turnstile, writer_turnstile; void reader() { sem_wait(&reader_turnstile); sem_wait(&mutex_read); active_readers++; if (active_readers == 1) { sem_wait(&writer_turnstile); } sem_post(&mutex_read); sem_post(&reader_turnstile); cout << "Reader " << this_thread::get_id() << " read data: " << data << endl; sem_wait(&mutex_read); active_readers--; if (active_readers == 0) { sem_post(&writer_turnstile); } sem_post(&mutex_read); } void writer() { sem_wait(&writer_turnstile); writer_active = true; sem_post(&writer_turnstile); sem_wait(&mutex_write); cout << "Writer " << this_thread::get_id() << " is writing data" << endl; data++; cout << "Writer " << this_thread::get_id() << " finished writing data" << endl; sem_post(&mutex_write); sem_wait(&writer_turnstile); writer_active = false; sem_post(&writer_turnstile); } int main() { sem_init(&mutex_read, 0, 1); sem_init(&mutex_write, 0, 1); sem_init(&reader_turnstile, 0, MAX_READERS); sem_init(&writer_turnstile, 0, 1); thread readers[MAX_READERS]; thread writers[3]; for (int i = 0; i < MAX_READERS; i++) { readers[i] = thread(reader); } for (int i = 0; i < 3; i++) { writers[i] = thread(writer); } for (int i = 0; i < MAX_READERS; i++) { readers[i].join(); } for (int i = 0; i < 3; i++) { writers[i].join(); } sem_destroy(&mutex_read); sem_destroy(&mutex_write); sem_destroy(&reader_turnstile); sem_destroy(&writer_turnstile); return 0; } ``` In this implementation, we have two semaphores for mutex (one for readers and one for writers) and two turnstile semaphores (one for readers and one for writers). When a reader wants to read, it first waits on the reader turnstile to ensure that there are not too many readers. Then, it waits on the reader mutex to ensure exclusive access to the shared data's active readers counter. It increments the active readers counter and, if it is the first reader, waits on the writer turnstile to block any writers from accessing the shared data. It then releases the reader mutex and signals the reader turnstile. It reads the shared data and then waits on the reader mutex again to decrement the active readers counter. If it is the last reader, it signals the writer turnstile to allow any waiting writers to access the shared data. When a writer wants to write, it waits on the writer turnstile to ensure exclusive access to the shared data. It sets the writer_active flag to true, waits on the writer mutex to ensure exclusive access to the shared data, writes to the data, and then sets the writer_active flag to false. It then signals the writer turnstile to allow other writers or readers to access the shared data.

相关推荐

最新推荐

recommend-type

概率论与数理统计试卷三套(含答案)

2020-2021年概率论与数理统计试卷
recommend-type

“人力资源+大数据+薪酬报告+涨薪调薪”

人力资源+大数据+薪酬报告+涨薪调薪,在学习、工作生活中,越来越多的事务都会使用到报告,通常情况下,报告的内容含量大、篇幅较长。那么什么样的薪酬报告才是有效的呢?以下是小编精心整理的调薪申请报告,欢迎大家分享。相信老板看到这样的报告,一定会考虑涨薪的哦。
recommend-type

伊坂幸太郎21册合集.mobi

伊坂幸太郎21册合集.mobi
recommend-type

dsdy-b4-v30003-1h.apk

dsdy-b4-v30003-1h.apk
recommend-type

Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)

Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)开发软件: Pycharm+ Python3.6数据库:mysql8.0 本软件基于python gui图形库pyqt5编写的仿qq,采用mysql数据库存储,socket通信(tcp协议)实现,支持多账号登录,注册,单人私聊,群聊,添加好友分组等功能。 Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)客户端界面目录文件:pyqt5-qq,服务端目录文件:Tcpserver Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)服务端目录结构: common:存放公共的工具类代码文件目录,主要是配置文件解析工具类,数据库操作工具类,本软件主要使用的是sqlalchemy orm数据库框架。 configfile:存放配置文件目录 dto:存放数据库表模型类代码文件 Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)客户端目录结构: image,res:存放资源文件,其他文件:界面及逻辑实现源码
recommend-type

STC89C51 简单时钟

STC89C51 简单时钟,叫你从基础开始学习单片机,
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?

![MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?](https://www.finebi.com/wp-content/uploads/2019/11/FineBI%E8%A1%8C%E4%B8%9A%E9%A9%BE%E9%A9%B6%E8%88%B1-1024x510.png) # 1. MATLAB归一化概述 归一化是一种数据预处理技术,用于将数据缩放到特定范围内,从而消除不同特征之间的尺度差异。在MATLAB中,有各种归一化方法可用于不同类型的数据和应用程序。 归一化的主要目的是: - 提高模型的训练效率和准确性,通过消除特征之间的尺度差异,使模型能够更有效地学习
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

Linux系统常用操作命令大全手册

附件是Linux系统常用操作命令大全手册,是 markdown格式,其中覆盖了Linux系统管理、文件操作、网络配置等多个方面,都是日常工作中非常常用的命令,欢迎大家下载学习使用!