#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_PERSON 100
#define READER 0 //读者
#define WRITER 1 //写者
#define END -1
#define R READER
#define W WRITER
typedef struct _Person {
HANDLE m_hThread; //定义处理线程的句柄
int m_nType; //进程类型(读写)
int m_nStartTime; //开始时间
int m_nWorkTime; //运行时间
int m_nID;//进程号
}Person;
Person g_Persons[MAX_PERSON];
int g_NumPerson = 0;
long g_CurrentTime= 0; //基本时间片数
int g_PersonLists[] = {//进程队列
1, W, 3, 5, 2, W, 16, 5, 3, R, 5, 2,
4, W, 6, 5, 5, R, 4, 3, 6, R, 17,7,
END,
};
int g_NumOfReading = 0;
int g_NumOfWriteRequest = 0; //申请写进程的个数
HANDLE g_hReadSemaphore; //读者信号
HANDLE g_hWriteSemaphore; //写者信号
bool finished = false; //所有的读完成
//bool wfinished = false; //所有的写完成
void CreatePersonList(int *pPersonList);
bool CreateReader(int StartTime,int WorkTime,int ID);
bool CreateWriter(int StartTime,int WorkTime,int ID);
DWORD WINAPI ReaderProc(LPVOID lpParam);
DWORD WINAPI WriterProc(LPVOID lpParam);
int main()
{
g_hReadSemaphore = CreateSemaphore(NULL,1,100,NULL); //创建信号灯,当前可用的资源数为 1,最大为100
g_hWriteSemaphore = CreateSemaphore(NULL,1,100,NULL); //创建信号灯,当前 可用的资源数为 1,最大为100
CreatePersonList(g_PersonLists); // 创建所有的读者和写者
printf("◆◆◆◆◆◆ 创建所有的读者和写者 ◆◆◆◆◆◆\n");
g_CurrentTime = 0;
while(true)
{
g_CurrentTime++;
Sleep(300); // 300 ms
printf(" 运行时间 = %d\n",g_CurrentTime);
if(finished) return 0;
} // return 0;
}
void CreatePersonList(int *pPersonLists)
{
int i=0;
int *pList = pPersonLists;
bool Ret;