基于POSIX标准库的读者-写者问题的简单实现

文章目录

  • 实验要求
  • 分析
    • 保证读写、写写互斥
    • 保证多个读者同时进行读操作
  • 读者优先
    • 实例代码
    • 分析
  • 写者优先
    • 示例代码
    • 分析

实验要求

  1. 创建一个控制台进程,此进程包含n个线程。用这n个线程来表示n个读者或写者。
  2. 每个线程按相应测试数据文件的要求进行读写操作。
  3. 信号量机制分别实现读者优先写者优先的读者-写者问题。

分析


由于只有一个共享文件, 而有n个读线程, n个写者线程需要互斥地对该文件进行读写操作

读者写者问题需要保证

  • 读读不互斥、允许多个读者同时进行读操作
  • 读写、写写互斥

保证读写、写写互斥


由于临界资源(共享文件)只有一个, 所以创建一个互斥信号量(资源数量只有1份)mutex_file来对进行对文件地互斥操作

保证多个读者同时进行读操作


由于需要保证多个读者不互斥地对文件进行读操作, 所以设置一个进程内的全局变量(线程共享) reading_count, 表示正在对文件进行读操作的线程的数量.

每当有一个读线程进入临界区后, 对该变量的数值+1.

由于有多个读线程, 所以对该全局变量的访问也需要互斥, 因此增加一个互斥信号量mutex_count

如果读线程判断到reading_count != 0, 则不用对信号量mutex_fileP操作, 可以直接进入临界区. 否则, 即该读线程是第一个读线程, 该读线程首先要对信号量mutex_file做P操作.

读者优先

  • 主函数

    • 打开要互斥访问的文件
    • 初始化信号量
    • 创建N个读者线程, N个写者线程mutex_file信号量代表的
  • 读者线程

    • 不断地请求对文件的操作(对信号量mutex_file进行P操作).
    • 打印读者线程id, 用于后续分析.
    • 如果成功的进入临界区, 读取文件的大小, 并打印到标准输出.
  • 写者线程

    • 不断地请求对文件的操作(对信号量mutex_file进行P操作).
    • 打印写者线程id, 用于后续分析.
    • 如果成功的进入临界区, 则对文件写一行文本, 这里为hello world\n.

实例代码

#include <iostream>
#include <vector>
#include <unistd.h>
#include <sys/file.h>
#include <pthread.h>
#include <semaphore.h>
// convient to code
#define P(x) sem_wait(x);
#define V(x) sem_post(x);
sem_t mutex_count;
sem_t mutex_file;
sem_t mutex_print; // make the print info correct
int reading_count = 0; // the amount of the reading thread
int fd; // the shared file descriptor
const int N = 5;

// the thread of the writer
char writer_str[] = "hello world\n";
void* writer_thread(void* arg) {
    while (true) {
        // try to operate the file
        P(&mutex_file);

        P(&mutex_print);
        printf("the writer %d is writing\n", arg);
        fflush(stdout);
        V(&mutex_print);
        // write into the file
        write(fd, writer_str, sizeof(writer_str) - 1);
        sleep(1);
        // release the file
        V(&mutex_file);
    }
}
// the thread of the reader
void* reader_thread(void* arg) {
    while (true) {
        // Firstly, we need to check and plus the reading_count
        // so, we try to catch the mutex_count
        P(&mutex_count);
        // if the reader is the first reader
        // if mutex_file = 0, 
        if (reading_count == 0) {
            P(&mutex_file);
        }
        reading_count++;
        V(&mutex_count);

        P(&mutex_print);
        printf("the reader %d is reading  #", arg);
        char buf[1024];
        // move file pointer to left 0, to read all content of file
        lseek(fd, 0, SEEK_SET);
        int len = read(fd, buf, sizeof(buf));
        printf("len = %d\n", len);
        fflush(stdout);
        // printf("str = \n%.*s\n", len, buf);
        // fflush(stdout);
        sleep(1);
        V(&mutex_print);

        // after reading, the reader leave, count--
        P(&mutex_count);
        reading_count--;
        // if the reader is the last reader
        if (reading_count == 0) {
            V(&mutex_file);
        }
        V(&mutex_count);
    }
}
int main(int argc, char const *argv[]) {
    // if use the cmd
    // if (argc < 2) {
    //     printf("usage : %s <n>\n", argv[0]);
    //     exit(1);
    // } 
    // int N = atoi(argv[1]);


    // open a file "data.txt", which can written and read,
    // if not exists, crate it, if already have sth, clear it.
    fd = open("data.txt", O_RDWR | O_CREAT | O_TRUNC);
    if (fd == -1) {
        char msg[] = "error to open the file\n";
        write(2, msg, sizeof(msg) - 1);
        return 1;
    }
    printf("file descriptor = %d\n", fd);

    /**
     * initialize the semaphores
     *  arg1 : the semaphore
     *  arg2: 0 means share in processes
     *  arg3: 1 means initial value of the semaphore, there 1 means mutual-sema
    */
    sem_init(&mutex_count, 0, 1); 
    sem_init(&mutex_file, 0, 1); 
    sem_init(&mutex_print, 0, 1); 


    /**
     * initialize the threads
    */
    std::vector<pthread_t> writer(N), reader(N);
    // create N writer thread, N reader thread
    for (int i = 0; i < N; i++) {
        pthread_create(&writer[i], nullptr, writer_thread, (void*) i + 1);
        pthread_create(&reader[i], nullptr, reader_thread, (void*) (N + i + 1));
    }
    // main thread waiting 2*N threads
    for (int i = 0; i < N; i++) {
        pthread_join(writer[i], nullptr);
        pthread_join(writer[i], nullptr);
    }
    // destory semaphores
    sem_destroy(&mutex_count);
    sem_destroy(&mutex_file);
    sem_destroy(&mutex_print);
    return 0;
}

分析

假设读写线程都有N = 5个, 如果尝试运行一下该程序
在这里插入图片描述
由于第一个创建的线程是写线程, 所以writer1会对文件进行写操作
后续当第一个读线程获取到文件的操作权后, 此时后续的读写线程都已经就绪, 因为此时reading_count=1, 所以其余读线程不会执行对信号量mutex_fileP操作, 而直接进入临界区, 但是写线程执行了对信号量mutex_fileP操作, 从而被阻塞, 加如到了该信号量的阻塞队列中. 接着, 其余读线程也顺势进入临界区, 并且由于一个线程内是持续(while(true))对共享文件做P操作的, 所以一个读线程完成读操作后会立即再次对文件发起读请求. 从而使得可能在后续读线程就绪前, 就准备好的写线程一直被阻塞. 从而引起了这些写线程出现饥饿现象.
读者优先时产生写线程饥饿

当然, 如果线程中不加入死循环, 则每个线程只对文件操作一次, 则所有的线程都有机会操作文件.此时写线程只会饥饿, 但不至于饿死. 而加入死循环, 可能会导致线程饿死.

写者优先


这里的写者优先并不是写者的优先级高于读者, 更不会导致读者出现饥饿情况.
实际上, 这里的读者和写者的优先级是一样的

前面读者优先的实现问题在于: 当读线程在占用文件时, 其它读线程直接进入临界区, 则不被阻塞, 仅有后续的写线程被阻塞

一种解决办法就是设置一个信号量让两类线程都可以因为请求文件被阻塞, 但是同时保证读-读不被阻塞

我们在原先的读者优先的实现中, 在最外层增加一个互斥信号量mutex_equal

为了保证会因请求文件而阻塞, 所以在对mutex_file进行P操作之前, 对mutex_equal执行P操作

由于要保证多个读者同时读取文件, 则当读者进入临界区后, 对mutex_wprivilege进行V操作

表示可以获得写者优先的线程数. 初始值为1表示当前读线程谦让最多1个写线程执行

在写线程执行时, 先对mutex_equal进行P操作, 如果此时mutex_equal= 1, 表示该写线程是第一个就绪且请求写文件的写线程, 则继续对mutex_file信号量进行P操作. 如果mutex_equal= 0, 表明该写线程不是第一个请求文件而被阻塞的写线程, 不应该享有让读线程谦让的资格. 此时被mutex_equal阻塞.

在读线程执行时, 先对mutex_wprivilege进行P操作, 如果被阻塞, 则说明此时有写线程正在请求文件(即被信号量mutex_file阻塞), 此时读线程被信号量mutex_wprivilege阻塞

示例代码

#include <iostream>
#include <vector>
#include <sys/file.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
sem_t mutex_count;
sem_t mutex_print;
sem_t mutex_file;
sem_t mutex_equal;
int reading_count = 0;
int fd;
const int N = 5;
#define P(x) sem_wait(x);
#define V(x) sem_post(x);
/**
 * P -> sem_wait -1
 * V -> sem_post +1
*/
// the thread of the writer
char writer_str[] = "hello world\n";
void* writer_thread(void* arg) {
    while (true) {
        // the first thread try to catch the file 
        P(&mutex_equal);
        P(&mutex_file);

        P(&mutex_print);
        printf("the writer %d is writing\n", arg);
        fflush(stdout);
        V(&mutex_print);

        write(fd, writer_str, sizeof(writer_str) - 1);
        sleep(1);
        V(&mutex_file);
        // the first blocking-thread of mutex_file leave the critical zone
        V(&mutex_equal);
    }
}
// the thread of the reader
void* reader_thread(void* arg) {
    while (true) {
        // check if the first thread of blocked-queue of mutex_file is the writer
        // if there is writer blocking, the write can't go into critcal zone 
        P(&mutex_equal);

        P(&mutex_count);
        // if the reader is the first reader
        if (reading_count == 0) {
            P(&mutex_file);
        }
        reading_count++;
        // read_count++;
        // printf("%d %d\n", write_count, read_count);
        V(&mutex_count);

        P(&mutex_print);
        printf("the reader %d is reading  #", arg);
        char buf[1024];
        // move file pointer to left 0 
        lseek(fd, 0, SEEK_SET);
        int len = read(fd, buf, sizeof(buf));
        printf("len = %d\n", len);
        fflush(stdout);
        // fflush(stdout);
        // printf("str = \n%.*s\n", len, buf);
        sleep(1);
        V(&mutex_print);
        
        V(&mutex_equal);

        // after reading, the reader leave, count--
        P(&mutex_count);
        reading_count--;
        // if the reader is the last reader
        if (reading_count == 0) {
            V(&mutex_file);
        }
        V(&mutex_count);
    }    
}
int main(int argc, char const *argv[]) {
    // if (argc < 2) {
    //     printf("usage : %s <n>\n", argv[0]);
    //     exit(1);
    // } 
    // int N = atoi(argv[1]);
    fd = open("data.txt", O_RDWR | O_CREAT | O_TRUNC);
    /**
     * initialize the semaphores
     *  arg1 : the semaphore
     *  arg2: 0 means share in processes
     *  arg3: 1 means initial value of the semaphore
    */
    sem_init(&mutex_count, 0, 1); 
    sem_init(&mutex_file, 0, 1); 
    sem_init(&mutex_print, 0, 1); 
    sem_init(&mutex_equal, 0, 1); 


    /**
     * initialize the threads
     * 
    */
    printf("file descriptor = %d\n", fd);
    vector<pthread_t> writer(N), reader(N);
    for (int i = 0; i < N; i++) {
        pthread_create(&writer[i], nullptr, writer_thread, (void*) i + 1);
        pthread_create(&reader[i], nullptr, reader_thread, (void*) (N + i + 1));
    }
    // main thread waiting 2*N threads
    for (int i = 0; i < N; i++) {
        pthread_join(writer[i], nullptr);
        pthread_join(writer[i], nullptr);
    }
    // destory semaphores
    sem_destroy(&mutex_count);
    sem_destroy(&mutex_file);
    sem_destroy(&mutex_print);
    sem_destroy(&mutex_equal);
    return 0;
}

分析

修改后的程序的输出结果: 读者和写者较为平均的访问了文件.
在这里插入图片描述
对于mutex_equal的阻塞队列的队首.

  • mutex_file的阻塞队列为空(这种情况下mutex_equal= 1), 此时mutex_equal的阻塞队列队首的读者线程, 会直接进入临界区.
  • mutex_file的阻塞队列有一个写者线程时, 此时mutex_equal的阻塞队列队首的读/写线程都不会进入mutex_file的阻塞队列.

很明显, mutex_file的阻塞队列最多只会有一个线程(只可能是写线程)
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/598809.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

FileLink跨网文件交换,推动企业高效协作|半导体行业解决方案

随着信息技术的迅猛发展&#xff0c;全球信息产业已经迎来了前所未有的繁荣与变革。在这场科技革命中&#xff0c;半导体作为信息产业的基础与核心&#xff0c;其重要性日益凸显&#xff0c;半导体的应用场景和市场需求将进一步扩大。 然而&#xff0c;在这一繁荣的背后&#x…

解决 SyntaxError: Unexpected token ‘.‘ 报错问题

这个报错一般是编译问题&#xff0c;浏览器的版本过低没通过代码 解决办法&#xff1a; 在package.json文件中加上这个 "browserslist": ["> 1%","last 2 versions","not dead","not ie < 6","Android > 4&…

源代码防泄露可以通过哪些方法实现?七种有效方法分享

在当今数字化时代&#xff0c;访问安全和数据安全成为企业面临的重要挑战。传统的边界防御已经无法满足日益复杂的内网办公环境&#xff0c;层出不穷的攻击手段已经让市场单一的防御手段黔驴技穷。当企业面临越来越复杂的网络威胁和数据泄密风险时&#xff0c;更需要一种综合的…

stable-diffusion-webui配置

源码地址 https://github.com/AUTOMATIC1111/stable-diffusion-webui.git报错Fresh install fail to load AttributeError: NoneType object has no attribute _id pydantic降级 pip uninstall pydantic pip install pydantic1.10.11记得要把clip-vit-large-patch14放在opena…

Java集合 总结篇(全)

Java集合 集合底层框架总结 List 代表的有序&#xff0c;可重复的集合。 ArrayList -- 数组 -- 把他想象成C中的Vector就可以&#xff0c;当数组空间不够的时候&#xff0c;会自动扩容。 -- 线程不安全 LinkedList -- 双向链表 -- 可以将他理解成一个链表&#xff0c;不支持…

C语言猜数字游戏

用C语言实现猜数字游戏&#xff0c;电脑随机给出一个范围内的数字&#xff0c;用户在终端输入数字&#xff0c;去猜大小&#xff1b;对比数字&#xff0c;电脑给出提示偏大还是偏小&#xff1b;不断循环&#xff0c;直到正确 #include <stdio.h> #include <time.h>…

【系统架构师】-选择题(十一)

1、紧耦合多机系统一般通过&#xff08;共享内存&#xff09;实现多机间的通信。对称多处理器结构&#xff08;SMP&#xff09;属于&#xff08; 紧耦合&#xff09;系统。 松耦合多机系统又称间接耦合系统,—般是通过通道或通信线路实现计算机间的互连。 2、采用微内核的OS结构…

从互联网医院源码到搭建:开发视频问诊小程序的技术解析

如今&#xff0c;视频问诊小程序作为医疗服务的一种新形式&#xff0c;正逐渐受到人们的关注和青睐。今天&#xff0c;小编将为您详解视频问诊小程序的开发流程。 一、背景介绍 互联网医院源码是视频问诊小程序开发的基础&#xff0c;它提供了一套完整的医疗服务系统框架&…

【vue-echarts】 报错问题解决 “Error: Component series.pie not exists. Load it first.“

目录 问题描述解决【解决1】【解决2】 问题描述 使用 vue-echarts 时导入的文件 import VChart from vue-echarts/components/ECharts import echarts/lib/chart/line import echarts/lib/chart/bar import echarts/lib/chart/pie import echarts/lib/component/legend impor…

MySQL 报错: “Host ‘xxx‘ is not allowed to connect to this MySQL server“

MySQL 报错 “Host ‘xxx’ is not allowed to connect to this MySQL server” 通常是因为数据库服务器上的权限设置不允许来自特定主机&#xff08;‘xxx’&#xff09;的连接。解决这个问题通常涉及修改 MySQL 的访问控制设置。 以下是一些可能的解决步骤&#xff1a; 使用…

高效工作之:开源工具kettle实战

在运营商数据处理领域&#xff0c;Oracle存储过程一直是数据处理的核心工具&#xff0c;但随着技术的发展&#xff0c;寻找替代方案变得迫切。Kettle&#xff0c;作为Oracle存储过程的替代品&#xff0c;以其强大的功能和易用性&#xff0c;正逐渐受到运营商的青睐。本文将介绍…

C++基础——深拷贝和浅拷贝

C中类的拷贝有两种&#xff1a;深拷贝&#xff0c;浅拷贝&#xff1a;当出现类的等号赋值时&#xff0c;即会调用拷贝函数 一、概念 浅拷贝&#xff1a;同一类型的对象之间可以赋值&#xff0c;使得两个对象的成员变量的值相同&#xff0c;两个对象仍然是独立的两个对象&#…

【全网首发】Typecho文章采集器火车头插件去授权版

内容目录 一、详细介绍二、效果展示1.部分代码2.效果图展示 三、学习资料下载 一、详细介绍 目前市面上基本没有typecho火车头采集器 而分享的这一款采集器&#xff0c;牛的一批 内置使用方法与教程&#xff01; 二、效果展示 1.部分代码 代码如下&#xff08;示例&#…

嘎嘎好用的虚拟键盘第二弹之中文输入法

之前还在为不用研究输入中文而暗自窃喜 这不新需求就来了&#xff08;新需求不会迟到 它只是在路上飞一会儿&#xff09; 找到了个博主分享的代码 是好使的 前端-xyq 已经和原作者申请转载了 感谢~~ 原作者地址&#xff1a;https://www.cnblogs.com/linjiangxian/p/16223681.h…

Amazon Q Business现已正式上市!利用生成式人工智能协助提高员工生产力

在 2023 年度 AWS re:Invent 大会上&#xff0c;我们预览了 Amazon Q Business&#xff0c;这是一款基于生成式人工智能的助手&#xff0c;可以根据企业系统中的数据和信息回答问题、提供摘要、生成内容额安全地完成任务。 借助 Amazon Q Business&#xff0c;您可以部署安全、…

Java多线程编程之synchronizaed和锁分类

并发编程第三周 1 锁的分类 1.1 可重入锁&#xff0c;不可重入锁 Java提供的synchronized&#xff0c;ReentrantLock,ReentrantReadWriteLock都是可重入锁 可重入&#xff1a;当前线程获取到A锁&#xff0c;在获取之后尝试再次获取A锁是可以直接拿到的。 不可重入:当前线程…

python使用mongo操作

目前有个需求&#xff0c;就是把所有sql转为mongo管道查询 知识点 在 MongoDB 中&#xff0c;allowDiskUse 选项应该作为聚合命令的一个选项&#xff0c;而不是聚合管道的一个阶段。allowDiskUse 选项用于允许聚合操作使用磁盘空间来临时存储数据&#xff08;当聚合操作的数据…

[leetcode] 67. 二进制求和

文章目录 题目描述解题方法模拟java代码复杂度分析 相似题目 题目描述 给你两个二进制字符串 a 和 b &#xff0c;以二进制字符串的形式返回它们的和。 示例 1&#xff1a; 输入:a "11", b "1" 输出&#xff1a;"100"示例 2&#xff1a; 输…

串的模式匹配之KMP算法实现

概述 函数刻画 主串位置不变&#xff0c;next值就是模式串(子串)比较后应跳转的位置 不同位置 next[j]函数 next由模式串决定&#xff0c;看模式串当前比较位的前串中前后缀相同的个数来得k-1的值&#xff0c;next[当前位]k1 小补充 PM值&#xff1a;也称部分匹配值&#xf…

产品推荐 | 基于Intel (Altera) Cyclone V打造的水星Mercury SA1核心板

01 产品概述 水星Mercury SA1片上系统&#xff08;SoC&#xff09;核心板通过结合基于ARM处理器的SoC FPGA、快速DDR3L SDRAM、eMMC flash、QSPI flash、Gigabit Ethernet PHY和RTC形成了一个高性能嵌入式处理方案&#xff0c;结合了CPU系统的灵活性和FPGA原始的、实时的并行处…
最新文章