实验要求
- 创建一个控制台进程,此进程包含
n
个线程。用这n个线程来表示n个读者或写者。- 每个线程按相应
测试数据文件
的要求进行读写操作。- 用信号量机制分别实现
读者优先
和写者优先
的读者-写者问题。
分析
由于只有一个共享文件, 而有n个读线程, n个写者线程需要互斥
地对该文件进行读写操作
读者写者问题需要保证
- 读读不互斥、允许多个读者同时进行读操作
- 读写、写写互斥
保证读写、写写互斥
由于临界资源(共享文件)只有一个, 所以创建一个互斥信号量(资源数量只有1份)mutex_file
来对进行对文件地互斥操作
保证多个读者同时进行读操作
由于需要保证多个读者不互斥地对文件进行读操作, 所以设置一个进程内的全局变量(线程共享) reading_count
, 表示正在对文件进行读操作的线程的数量.
每当有一个读线程进入临界区后, 对该变量的数值+1.
由于有多个读线程, 所以对该全局变量的访问也需要互斥, 因此增加一个互斥信号量mutex_count
如果读线程判断到reading_count != 0
, 则不用对信号量mutex_file
做P
操作, 可以直接进入临界区. 否则, 即该读线程是第一个读线程, 该读线程首先要对信号量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)