建设网站报告书,dede手机网站模板下载,网页设计的交流网站,俄罗斯军事基地文章目录 实验要求分析保证读写、写写互斥保证多个读者同时进行读操作读者优先实例代码分析写者优先读写公平法示例代码分析实验要求 创建一个控制台进程,此进程包含n个线程。用这n个线程来表示n个读者或写者。每个线程按相应测试数据文件的要求进行读写操作。用信号量机制分别… 文章目录 实验要求分析保证读写、写写互斥保证多个读者同时进行读操作 读者优先实例代码分析 写者优先读写公平法示例代码分析 实验要求 创建一个控制台进程,此进程包含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 fileP(mutex_file);P(mutex_print);printf("the writer %d is writing\n", arg);fflush(stdout);V(mutex_print);// write into the filewrite(fd, writer_str, sizeof(writer_str) - 1);sleep(1);// release the fileV(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_countP(mutex_count)