读者-写者(多线程)
题目
1 描述操作系统中“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料
2 利用多线程完成reader 和writer
3 在main中测试若干个reader 和writer的测试,提交截图说明代码的正确性
问题理解
需要满足的条件:
1.写进程与写进程之间必须互斥的写入数据(因为如果两个写进程同时对共享数据中的区域A中的数据进行写操作的话,会导致数据错误覆盖的问题)
2.写进程与读进程之间必须互斥的访问共享数据(因为写进程与读进程如果同时访问共享数据,可能会导致数据不一致的问题。比如:读进程A想要访问共享数据中的B数据,但是写进程C在读进程A访问B数据之前将B数据进行了更新,这就会导致读进程A读不到它想要读到的数据,从而出现数据不一致问题)
3.读进程与读进程之间可以同时访问数据,不需要实现互斥的访问共享数据(因为读进程读数据,并不会像之前的生产者消费者问题中的消费者那样改变数据或者是将数据清空,所以多个读进程可以同时访问共享数据)
程序的完成
一、写者优先
(一)思路
写者优先与读者优先类似。不同之处在于一旦一个写者到来,它应该尽快对文件进行写操作,如果有一个写者在等待,则新到来的读者不允许进行读操作。为此应当添加一个整型变量write_count,用于记录正在等待的写者的数目,当 write_count=0 时,才可以释放等待的读者线程队列。
为了对全局变量 write_count 实现互斥,必须增加一个互斥对象mutex2。
为了实现写者优先,应当添加一个临界区对象 read,当有写者在写文件或等待时,读者必须阻塞在 read 上。同样,有读者读时,写者必须等待。于是,必须有一个互斥对象 RW_mutex 来实现这个互斥。
有写者在写时,写者必须等待。
读者线程要对全局变量 read_count 实现操作上的互斥,必须有一个互斥对象命名为 mutex1。
(二)源代码
/*
* 写者优先
*/
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <sys/types.h>
# include <pthread.h>
# include <semaphore.h>
# include <string.h>
# include <unistd.h>
//semaphores
sem_t RWMutex, mutex1, mutex2, mutex3, wrt;
int writeCount, readCount;
struct data {
int id;
int opTime;
int lastTime;
};
//读者
void* Reader(void* param) {
int id = ((struct data*)param)->id;
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
sleep(opTime);
printf("Thread %d: waiting to read\n", id);
sem_wait(&mutex3);
sem_wait(&RWMutex);
sem_wait(&mutex2);
readCount++;
if(readCount == 1)
sem_wait(&wrt);
sem_post(&mutex2);
sem_post(&RWMutex);
sem_post(&mutex3);
printf("Thread %d: start reading\n", id);
/* reading is performed */
sleep(lastTime);
printf("Thread %d: end reading\n", id);
sem_wait(&mutex2);
readCount--;
if(readCount == 0)
sem_post(&wrt);
sem_post(&mutex2);
pthread_exit(0);
}
//写者
void* Writer(void* param) {
int id = ((struct data*)param)->id;
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
sleep(opTime);
printf("Thread %d: waiting to write\n", id);
sem_wait(&mutex1);
writeCount++;
if(writeCount == 1){
sem_wait(&RWMutex);
}
sem_post(&mutex1);
sem_wait(&wrt);
printf("Thread %d: start writing\n", id);
/* writing is performed */
sleep(lastTime);
printf("Thread %d: end writing\n", id);
sem_post(&wrt);
sem_wait(&mutex1);
writeCount--;
if(writeCount == 0) {
sem_post(&RWMutex);
}
sem_post(&mutex1);
pthread_exit(0);
}
int main() {
//pthread
pthread_t tid; // the thread identifier
pthread_attr_t attr; //set of thread attributes
/* get the default attributes */
pthread_attr_init(&attr);
//initial the semaphores
sem_init(&mutex1, 0, 1);
sem_init(&mutex2, 0, 1);
sem_init(&mutex3, 0, 1);
sem_init(&wrt, 0, 1);
sem_init(&RWMutex, 0, 1);
readCount = writeCount = 0;
int id = 0;
while(scanf("%d", &id) != EOF) {
char role; //producer or consumer
int opTime; //operating time
int lastTime; //run time
scanf("%c%d%d", &role, &opTime, &lastTime);
struct data* d = (struct data*)malloc(sizeof(struct data));
d->id = id;
d->opTime = opTime;
d->lastTime = lastTime;
if(role == 'R') {
printf("Create the %d thread: Reader\n", id);
pthread_create(&tid, &attr, Reader, d);
}
else if(role == 'W') {
printf("Create the %d thread: Writer\n", id);
pthread_create(&tid, &attr, Writer, d);
}
}
sem_destroy(&mutex1);
sem_destroy(&mutex2);
sem_destroy(&mutex3);
sem_destroy(&RWMutex);
sem_destroy(&wrt);
return 0;
}
二、读者优先
(一)思路
读者优先指的是除非有写者在写文件,否则读者不需要等待。所以可以用一个整型变量read_count记录当前的读者数目,用于确定是否需要释放正在等待的写者线程(当read_count=0时,表明所有的读者读完,需要释放写者等待队列中的一个写者)。每一个读者开始读文件时,必须修改read_count变量。因此需要一个互斥对象mutex来实现对全局变量read_count修改时的互斥。
另外,为了实现写-写互斥,需要增加一个临界区对象write。当写者发出写请求时,必须申请临界区对象的所有权。通过这种方法,也可以实现读-写互斥,当read_count=1时(即第一个读者到来时),读者线程也必须申请临界区对象的所有权。
当读者拥有临界区的所有权时,写者阻塞在临界区对象write上。当写者拥有临界区的所有权时,第一个读者判断完“read_count==1”后阻塞在write上,其余的读者由于等待对read_count的判断,阻塞在mutex上。
(二)代码
/*
* 读者优先
*/
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <sys/types.h>
# include <pthread.h>
# include <semaphore.h>
# include <string.h>
# include <unistd.h>
//semaphores
sem_t wrt, mutex;
int readCount;
struct data {
int id;
int opTime;
int lastTime;
};
//读者
void* Reader(void* param) {
int id = ((struct data*)param)->id;
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
sleep(opTime);
printf("Thread %d: waiting to read\n", id);
sem_wait(&mutex);
readCount++;
if(readCount == 1)
sem_wait(&wrt);
sem_post(&mutex);
printf("Thread %d: start reading\n", id);
/* reading is performed */
sleep(lastTime);
printf("Thread %d: end reading\n", id);
sem_wait(&mutex);
readCount--;
if(readCount == 0)
sem_post(&wrt);
sem_post(&mutex);
pthread_exit(0);
}
//写者
void* Writer(void* param) {
int id = ((struct data*)param)->id;
int lastTime = ((struct data*)param)->lastTime;
int opTime = ((struct data*)param)->opTime;
sleep(opTime);
printf("Thread %d: waiting to write\n", id);
sem_wait(&wrt);
printf("Thread %d: start writing\n", id);
/* writing is performed */
sleep(lastTime);
printf("Thread %d: end writing\n", id);
sem_post(&wrt);
pthread_exit(0);
}
int main() {
//pthread
pthread_t tid; // the thread identifier
pthread_attr_t attr; //set of thread attributes
/* get the default attributes */
pthread_attr_init(&attr);
//initial the semaphores
sem_init(&mutex, 0, 1);
sem_init(&wrt, 0, 1);
readCount = 0;
int id = 0;
while(scanf("%d", &id) != EOF) {
char role; //producer or consumer
int opTime; //operating time
int lastTime; //run time
scanf("%c%d%d", &role, &opTime, &lastTime);
struct data* d = (struct data*)malloc(sizeof(struct data));
d->id = id;
d->opTime = opTime;
d->lastTime = lastTime;
if(role == 'R') {
printf("Create the %d thread: Reader\n", id);
pthread_create(&tid, &attr, Reader, d);
}
else if(role == 'W') {
printf("Create the %d thread: Writer\n", id);
pthread_create(&tid, &attr, Writer, d);
}
}
//信号量销毁
sem_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}