读者写者问题

问题简述
读者-写者问题的读写操作限制(包括读者优先和写者优先):
1)写-写互斥,即不能有两个写者同时进行写操作。
2)读-写互斥,即不能同时有一个线程在读,而另一个线程在写。
3)读-读允许,即可以有一个或多个读者在读。
读者优先的附加限制:如果一个读者申请进行读操作时已有另一个读者正在进行读操作,则该读者可直接开始读操作。
写者优先的附加限制:如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必须等到没有写者处于等待状态后才能开始读操作。
测试内容
测试数据文件包括n行测试数据,分别描述创建的n个线程是读者还是写者,以及读写操作的开始时间和持续时间。每行测试数据包括四个字段,各个字段间用空格分隔。第一字段为一个正整数,表示线程序号。第二字段表示相应线程角色,R表示读者,w表示写者。第三字段为一个正数,表示读写操作的开始时间:线程创建后,延迟相应时间(单位为秒)后发出对共享资源的读写申请。第四字段为一个正数,表示读写操作的持续时间。当线程读写申请成功后,开始对共享资源的读写操作,该操作持续相应时间后结束,并释放共享资源。
下面是一个测试数据文件的例子:
1 R 3 5
2 W 4 5
3 R 5 2
4 R 6 5
5 W 5.1 3
函数解释
1.CreateThread
函数功能:该函数创建一个在调用进程的地址空间中执行的线程。
函数模型:

 HANDLE CreateThread(
 LPSECURITY_ATTRIBUTES  lpThreadAttributes, 
 DWORD dwStackSize,
 LPTHREAD_START_ROUTINE  lpStartAddress,
 LPVOID lpParameter,
 DWORD dwCreationFlag,
 LPDWORD  lpThreadId)

2.S1eep
函数功能:
该函数对于指定的时间间隔挂起当前的执行线程。
函数原型:VOID Sleep(DWORD dwMilliseconds);
3.CreateMutex
函数功能:
该函数创建有名或者无名的互斥对象。
函数原型:

HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName);

4.ReleaseMutex
函数功能:
该函数放弃指定互斥对象的所有权。
函数原型:Bool ReleaseMutex (HANDLE hMutex)
5.WaitForSingleObject
函数功能:
当下列情况之一发生时该函数返回:(1)指定对象处于信号态;(2)超时。
函数原型:
DWORD WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds);

程序简介
1.读者优先.cpp
读者优先指的是除非有写者在写文件,否则读者不需要等待。所以可以用一个整型变量read_count记录当前的读者数目,用于确定是否需要释放正在等待的写者线程(当read_count=0时,表明所有的读者读完,需要释放写者等待队列中的一个写者)。每一个读者开始读文件时,必须修改read_count变量。因此需要一个互斥对象mutex来实现对全局变量read_count修改时的互斥。
另外,为了实现写-写互斥,需要增加一个临界区对象write。当写者发出写请求时,必须申请临界区对象的所有权。通过这种方法,也可以实现读-写互斥,当read_count=1时(即第一个读者到来时),读者线程也必须申请临界区对象的所有权。
当读者拥有临界区的所有权时,写者阻塞在临界区对象write上。当写者拥有临界区的所有权时,第一个读者判断完“read_count==1”后阻塞在write上,其余的读者由于等待对read_count的判断,阻塞在mutex上。

2.写者优先.cpp
写者优先与读者优先类似。不同之处在于一旦一个写者到来,它应该尽快对文件进行写操作,如果有一个写者在等待,则新到来的读者不允许进行读操作。为此应当添加一个整型变量write_count,用于记录正在等待的写者的数目,当write_count=0时,才可以释放等待的读者线程队列。
为了对全局变量write_count实现互斥,必须增加一个互斥对象mutex3。
为了实现写者优先,应当添加一个临界区对象read,当有写者在写文件或等待时,读者必须阻塞在read上。
读者线程除了要对全局变量read_count实现操作上的互斥外,还必须有一个互斥对象对阻塞read这一过程实现互斥。这两个互斥对象分别命名为mutexl和mutex2。

代码
读者优先

#include<fstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
#include<windows.h>
using namespace std;
//读者优先 
struct Thread{
    HANDLE h_thread;
    int num;
    char type;
    double start;
    double duration;
}thread[100];
HANDLE rmutex; //hX
HANDLE wmutex; //hWsem
int readcount;
int totalDura;
int curTime;

void write_op(int iProcess){
    cout << "写进程" << iProcess << "开始写操作" << endl;
    Sleep((DWORD)(thread[iProcess - 1].duration * 1000));
    /*while (curTime<(thread[iProcess - 1].duration +
        thread[iProcess - 1].start)){
    }*/
    cout << "写进程" << iProcess << "结束写操作" << endl;
}

void read_op(int iProcess){
    cout << "读进程" << iProcess << "开始读操作" << endl;
    Sleep((DWORD)(thread[iProcess - 1].duration * 1000));
    /*while (curTime<(thread[iProcess - 1].duration +
        thread[iProcess - 1].start)){
    }*/
    cout << "读进程" << iProcess << "结束读操作" << endl;
}

DWORD WINAPI reader(LPVOID lpVoid){
    int iProcess = *(int*)lpVoid;
    Sleep((DWORD)(thread[iProcess - 1].start * 1000));
    DWORD wait_for = WaitForSingleObject(rmutex, INFINITE);
    cout << "读进程" << iProcess << "请求读操作" << endl;
    if (readcount == 0) WaitForSingleObject(wmutex, INFINITE);
    readcount++;
    ReleaseMutex(rmutex);
    read_op(iProcess);
    wait_for = WaitForSingleObject(rmutex, INFINITE);
    readcount--;
    if (readcount == 0) ReleaseMutex(wmutex);
    ReleaseMutex(rmutex);
    return iProcess;
}

DWORD WINAPI writer(LPVOID lpVoid){
    int iProcess = *(int*)lpVoid;
    Sleep((DWORD)(thread[iProcess - 1].start * 1000));
    cout << "写进程" << iProcess << "请求写操作" << endl;
    DWORD wait_for = WaitForSingleObject(wmutex, INFINITE);
    write_op(iProcess);
    ReleaseMutex(wmutex);
    return iProcess;
}

int main(){
    int ThreadNum;
    ifstream file;
    rmutex = CreateMutex(NULL, false, NULL);
    wmutex = CreateMutex(NULL, false, NULL);
    readcount = 0;
    totalDura = 0;
    curTime = 1;
    file.open("input.txt", ios::in);
    while (true){
        while (file >> ThreadNum){
            thread[ThreadNum - 1].num = ThreadNum;
            file >> thread[ThreadNum - 1].type;
            file >> thread[ThreadNum - 1].start;
            file >> thread[ThreadNum - 1].duration;
            totalDura += thread[ThreadNum - 1].duration;
            switch (thread[ThreadNum - 1].type){
            case 'W':
                cout << "创建写进程" << thread[ThreadNum - 1].num << endl;
                thread[ThreadNum - 1].h_thread = CreateThread(NULL, 0, writer,
                    &thread[ThreadNum - 1].num, 0, 0);
                break;
            case 'R':
                cout << "创建读进程" << thread[ThreadNum - 1].num << endl;
                thread[ThreadNum - 1].h_thread = CreateThread(NULL, 0, reader,
                    &thread[ThreadNum - 1].num, 0, 0);
                break;
            }
        }
        curTime++;
        Sleep(1000);
        cout << "当前时间=" << curTime << endl;
        if (curTime == 23) break;
    }
    file.close();
    //Sleep((DWORD)(totalDura * 1000));
    //cout << thread[1].num << endl;
    //system("pause");
    getchar();
}

写者优先

#include<fstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
#include<windows.h>
using namespace std;

struct Thread{
    HANDLE h_thread;
    int num;
    char type;
    double start;
    double duration;
}thread[100];
HANDLE rmutex; //hX
HANDLE wmutex; //hWsem
HANDLE wblock;
HANDLE rblock;
HANDLE mutex;
int readcount;
int writecount;
int totalDura;
int curTime;

void write_op(int iProcess){
    cout << "写进程" << iProcess << "开始写操作" << endl;
    Sleep((DWORD)(thread[iProcess - 1].duration * 1000));
    /*while (curTime<(thread[iProcess - 1].duration +
        thread[iProcess - 1].start)){
    }*/
    cout << "写进程" << iProcess << "结束写操作" << endl;
}

void read_op(int iProcess){
    cout << "读进程" << iProcess << "开始读操作" << endl;
    Sleep((DWORD)(thread[iProcess - 1].duration * 1000));
    /*while (curTime<(thread[iProcess - 1].duration +
        thread[iProcess - 1].start)){
    }*/
    cout << "读进程" << iProcess << "结束读操作" << endl;
}

DWORD WINAPI reader(LPVOID lpVoid){
    int iProcess = *(int*)lpVoid;
    Sleep((DWORD)(thread[iProcess - 1].start * 1000));
    cout << "读进程" << iProcess << "请求读操作" << endl;
    DWORD wait_for = WaitForSingleObject(wblock, INFINITE);
    wait_for = WaitForSingleObject(rblock, INFINITE);
    wait_for = WaitForSingleObject(rmutex, INFINITE);   
    readcount++;
    if (readcount == 1) WaitForSingleObject(mutex, INFINITE);
    ReleaseMutex(rmutex);
    ReleaseMutex(rblock);
    ReleaseMutex(wblock);
    read_op(iProcess);
    wait_for = WaitForSingleObject(rmutex, INFINITE);
    readcount--;
    if (readcount == 0) ReleaseMutex(mutex);
    ReleaseMutex(rmutex);
    return iProcess;

}

DWORD WINAPI writer(LPVOID lpVoid){
    int iProcess = *(int*)lpVoid;
    Sleep((DWORD)(thread[iProcess - 1].start * 1000));
    cout << "写进程" << iProcess << "请求写操作" << endl;
    DWORD wait_for = WaitForSingleObject(wmutex, INFINITE);
    writecount++;
    if(writecount==1) WaitForSingleObject(rblock, INFINITE);
    ReleaseMutex(wmutex);
    wait_for = WaitForSingleObject(mutex, INFINITE);
    write_op(iProcess);
    ReleaseMutex(mutex);
    wait_for = WaitForSingleObject(wmutex, INFINITE);
    writecount--;
    if(writecount==0)   ReleaseMutex(rblock);
    ReleaseMutex(wmutex);
    return iProcess;
}

int main(){
    int ThreadNum;
    ifstream file;
    rmutex = CreateMutex(NULL, false, NULL);
    wmutex = CreateMutex(NULL, false, NULL);
    rblock = CreateMutex(NULL, false, NULL);
    wblock = CreateMutex(NULL, false, NULL);
    mutex = CreateMutex(NULL, false, NULL);
    readcount = 0;
    writecount= 0;
    totalDura = 0;
    curTime = 1;
    file.open("input.txt", ios::in);
    while (true){
        while (file >> ThreadNum){
            thread[ThreadNum - 1].num = ThreadNum;
            file >> thread[ThreadNum - 1].type;
            file >> thread[ThreadNum - 1].start;
            file >> thread[ThreadNum - 1].duration;
            totalDura += thread[ThreadNum - 1].duration;
            switch (thread[ThreadNum - 1].type){
            case 'W':
                cout << "创建写进程" << thread[ThreadNum - 1].num << endl;
                thread[ThreadNum - 1].h_thread = CreateThread(NULL, 0, writer,
                    &thread[ThreadNum - 1].num, 0, 0);
                break;
            case 'R':
                cout << "创建读进程" << thread[ThreadNum - 1].num << endl;
                thread[ThreadNum - 1].h_thread = CreateThread(NULL, 0, reader,
                    &thread[ThreadNum - 1].num, 0, 0);
                break;
            }
        }
        curTime++;
        Sleep(1000);
        cout << "当前时间=" << curTime << endl;
        if (curTime == 25) break;
    }
    file.close();
    //Sleep((DWORD)(totalDura * 1000));
    //cout << thread[1].num << endl;
    //system("pause");
    getchar();
}

图解执行过程
读者优先:
读者优先执行过程示意图
写者优先:
写者优先执行过程示意图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值