基于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)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 23:12:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 23:12:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 23:12:07       87 阅读
  4. Python语言-面向对象

    2024-05-10 23:12:07       96 阅读

热门阅读

  1. 蓝桥杯 算法提高 ADV-1163 网格贪吃蛇 python AC

    2024-05-10 23:12:07       36 阅读
  2. 哪里可以获得正规的行政区底图?

    2024-05-10 23:12:07       32 阅读
  3. MySQL VARCHAR 最佳长度评估实践

    2024-05-10 23:12:07       29 阅读
  4. XXL-Job

    2024-05-10 23:12:07       30 阅读
  5. ArrayList线程不安全的情况

    2024-05-10 23:12:07       36 阅读