2、定义一个读者函数:
l 当有写者在占用buffer时,读者应该等待,直到写者不再使用该buffer。
l 当有其他读者在占用buffer时,读者可对buffer进行读取操作。
l 当buffer中有数据时,则从其中读取一个数据,并显示然后退出。
l 当buffer中没有数据时,应等待,直到buffer中有数据可读。
3、定义一个写者函数
l 当有读者在占用buffer时,写者应该等待,直到所有的读者都退出为止。
l 当有其他写者占用buffer时,该写者应该等待,直到占用buffer的写者退出为止。
l 当buffer有空闲时,写者应该在buffer中写入一个数据并退出。
l 当buffer满时,写者应该等待,直到buffer有空闲为止。
#include
#include
#include
#include
#include
#include
#define MAX 8
int reader_count = 0; //纪录读者数目
long currentTime = 0; //纪录相对时间
int buffer = 0; //缓冲区标记:0空;1满
HANDLE w; //控制读者写者对缓冲区访问
HANDLE mutex; //互斥体
struct People{
HANDLE thread; //定义处理线程的句柄
int type; //进程类型(读写)1读,0写
int StartWorkTime; //开始工作时间(相对时间)
int WorkTime; //工作时间
};
People per[MAX]; //人
People *pl = per;
void createMutex(HANDLE mutex){ //创建互斥体用于访问数值
mutex = CreateMutex(
NULL,
TRUE,
NULL);
}
void createSemaphore(HANDLE w){ //创建信号量
w = CreateSemaphore(
NULL,
1, //当前可用的资源数为1
10, //最大为10
NULL);
}
DWORD WINAPI ReadProc(LPVOID lpParam){ //读过程
People *pers = (People*)lpParam;
int st = 1;
while(st == 1 || buffer == 1){
int count = 1;
while(currentTime <>StartWorkTime){ //未到达工作时间时,读者等待
if(count == 1)
printf("%d read_thread is waiting for time.\n",GetCurrentThreadId());
count ++;
}
printf("%d thread is requesting read.\n",GetCurrentThreadId());
count = 1;
while(buffer == 0){ //缓冲区为空时,读者等待
if(count == 1) //等待第一次时,显示输出
printf("%d read_thread is waiting for write_thread.\n",GetCurrentThreadId());
count ++;
}
WaitForSingleObject(mutex,INFINITE); //P(mutex);
reader_count ++; //Rcount++;
if(reader_count == 1){ //if (Rcount==1)
WaitForSingleObject(w,INFINITE); //P(w);
}
ReleaseMutex(mutex);
pers->StartWorkTime = currentTime; //重新设置工作时间
count = 1;
while(currentTime <= pers->StartWorkTime + pers->WorkTime){ //读
if(count == 1)
printf("%d thread is reading.\n",GetCurrentThreadId());
count ++;
}
WaitForSingleObject(mutex,INFINITE); //P(mutex);
reader_count --; //Rcount--;
if(reader_count == 0){ //最后一个读者离开时,缓冲区清空,即0; if (Rcount==0)
buffer = 0;
ReleaseSemaphore(w,1,NULL); //V(w);
}
ReleaseMutex(mutex); //V(mutex);
printf("%d thread finished reading-work and exited.\n",GetCurrentThreadId());
st ++;
Sleep(600);
}
CloseHandle(pers->thread);
return 0;
}
DWORD WINAPI WriteProc(LPVOID lpParam){ //写过程
People *pers = (People*)lpParam;
int count = 1;
while(currentTime <>StartWorkTime){ //未到达工作时间时,读者等待
if(count == 1)
printf("%d write_thread is waiting for time.\n",GetCurrentThreadId());
count ++;
}
printf("%d thread is requesting write.\n",GetCurrentThreadId());
count = 1;
while(buffer == 1){ //缓冲区为满时,写者等待
if(count == 1)
printf("%d write_thread is waiting for buffer.\n",GetCurrentThreadId());
count ++;
}
WaitForSingleObject(w,INFINITE); //P(w);
pers->StartWorkTime = currentTime;
count = 1;
printf("%d thread is writing.\n",GetCurrentThreadId()); //写
buffer = 1; //缓冲区满
printf("%d thread finished writing-work and exited.\n",GetCurrentThreadId());
ReleaseSemaphore(w,1,NULL); //V(w);
CloseHandle(pers->thread);
return 0;
}
void create(int num){
int count = 0;
do{
per[count].StartWorkTime = 1;
per[count].WorkTime = 2;
per[count].type = 0; //写
per[count].thread = CreateThread(
NULL, //默认安全性
0, //默认堆栈
WriteProc, //类范围内的线程proc
(LPVOID)(&per), //发送per指针给proc
0, //线程创建后可立即执行的标志为0
NULL);
count ++;
}while(count < num/2);
do{
per[count].StartWorkTime = 1;
per[count].WorkTime = 2;
per[count].type = 1; //读
per[count].thread = CreateThread(
NULL, //默认安全性
0, //默认堆栈
ReadProc, //类范围内的线程proc
(LPVOID)(&per), //发送per指针给proc
0, //线程创建后可立即执行的标志为0
NULL);
count ++;
}while(count < num);
}
void main(){
char password[7] = {'0','5','2','2','2','6'};
char input[10];
int i = 0;
char c = getch();
while(c != '*'){
input[i] = c;
i ++;
putchar('*');
c = getch();
}
input[i] = '\0';
putchar('\n');
if(strcmp(password,input) == 0){
createMutex(mutex); //创建互斥体
createSemaphore(w); //创建信号量
create(MAX); //创建读者写者
currentTime = 0; //初始化当前时间
while(currentTime < 30){
printf("currentTime = %d\n",currentTime);
Sleep(300);
currentTime++;
}
printf("Time over!\n");
CloseHandle(mutex);
CloseHandle(w);
printf("Thank you for using.:-)\n");
}
else printf("You do not have the right to use this code.:-(\nThank you.\n");
}
没有评论:
发表评论