#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <winbase.h>
#include <process.h>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
#define READER 'R' //读者
#define WRITER 'W' //写者目
#define MAXSIZE 80
#define MAXNUMBER 10
#define INTE_PER_SEC 1000 //每秒时钟中断的数
int buffersize;
int readcount=0;
int writecount=0;
struct ThreadInfo{
int serial;
char entity;
double persist;
vector<int> product_q;
};
//仓库
char *store[MAXNUMBER] ;
//临界资源
CRITICAL_SECTION RP_Write;
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
////////////////////////////////////////////////////////
//写者优先---读者线程
//P:读者线程信息
void WP_ReaderThread(void *p)
{
//互斥变量
HANDLE h_Mutex1;
h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,(LPCWSTR)"mutex1");
HANDLE h_Mutex2;
h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,(LPCWSTR)"mutex2");
DWORD wait_for_mutex1; //等待互斥变量所有权
DWORD wait_for_mutex2;
DWORD m_persist; //读文件持续时间
int m_serial; //线程的序号
//从参数中得到信息
m_serial=((ThreadInfo*)(p))->serial ;
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);
EnterCriticalSection(&cs_Read);
readcount++;
if(readcount==1)
{
EnterCriticalSection(&cs_Write);
///////////////////////////////////////////////////////////////////////////////
}
ReleaseMutex(h_Mutex2);
LeaveCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex1);
printf("读进程 %d 开始读操作\n",m_serial);
//用STL迭代器访问每个元素
vector<int>::iterator pos;
for(pos=((ThreadInfo*)(p))->product_q.begin();pos < ((ThreadInfo*)(p))->product_q.end(); ++pos){
printf("consumer %d got the product of %d\n",m_serial,*pos);
printf(" ################and the product is: %d \n",store[*pos]);
}
Sleep(m_persist);
printf("读线程 %d 完成了读操作.\n",m_serial);
wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);
readcount--;
if(readcount==0)
{
//最后一个读者,唤醒写者
LeaveCriticalSection(&cs_Write);
///////////////////////////////////////////////////////////////////////////////
}
ReleaseMutex(h_Mutex2); //释放互斥信号
}
//写者优先---写者线程
//P:写者线程信息
void WP_WriterThread(void *p)
{
DWORD wait_for_mutex3; //互斥变量
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号
HANDLE h_Mutex3;
h_Mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,(LPCWSTR)"mutex3");
//从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial;
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
printf("写线程 %d 发送写请求.\n",m_serial);
wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);
writecount++; //修改写者数目
if(writecount==1)
{
EnterCriticalSection(&cs_Read);
}
ReleaseMutex(h_Mutex3);
EnterCriticalSection(&cs_Write);
////////////////////////////////////////////////////
printf("写线程 %d 开始写操作.\n",m_serial);
memset(store[m_serial-1],'\0',buffersize);
itoa(m_serial-1,store[m_serial-1],buffersize);
Sleep(m_persist);
printf("写线程 %d 完成写操作.\n",m_serial);
////////////////////////////////////////////////////
LeaveCriticalSection(&cs_Write);
wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);
writecount--;
if(writecount==0)
{
LeaveCriticalSection(&cs_Read);
}
ReleaseMutex(h_Mutex3);
}
//写者优先
int main(int argc,char *argv[])
{
int n_thread = 0;
int n_productor = 0;
FILE * fp;
char input[80];
ThreadInfo ThreadInfo[MAXNUMBER];
printf("read file begin....\n");
//read file begin....
if((fp = fopen("pc.dat","r")) == NULL)
{
printf("open file failed \n");
exit(-1);
}
memset(input,'\0',80);
fgets(input,20,fp);
buffersize = atoi(input);
while(!feof(fp)){
memset(input,'\0',80);
fgets(input,80,fp);
if(input[2] == 'p')
{
sscanf(input,"%d %c %d",&ThreadInfo[n_thread].serial, &ThreadInfo[n_thread].entity, &ThreadInfo[n_thread].persist);
printf("thread %d------------------------>OK\n",ThreadInfo[n_thread].serial);
n_productor++;
}else if(input[2] == 'c'){
char buffer[20] = "";
sscanf(input,"%d %c %f %[1-9| ]*",&ThreadInfo[n_thread].serial, &ThreadInfo[n_thread].entity, &ThreadInfo[n_thread].persist, buffer);
printf("thread %d------------------------>OK\n",ThreadInfo[n_thread].serial);
//begin to process the buffer
//
int count = 0;
while(count < strlen(buffer)){
char temp[20] = "";
memset(temp,'\0',20);
sscanf(&buffer[count],"%[0-9]+ ",&temp);
count += strlen(temp)+1;
int id = atoi(temp);
ThreadInfo[n_thread].product_q.push_back(id);
}
}
n_thread++;
}
printf("number is : %d\n",n_thread);
//end of reading file ,and all vars already inintialized!
//threads begin.......
//shared buffer
//printf(">>>>>%d>>>>>>>>>>>>>%d\n",buffersize,n_thread);
if((buffersize >= 0 || buffersize < MAXNUMBER)&& n_productor > 0){
for(int i =0; i < n_thread; i++){
store[i] = (char *)malloc(sizeof(char)*buffersize);
}
}else
{
printf("please input a number for the size of buffer!\n");
_getch();
exit(-1);
}
//互斥对象
HANDLE h_Mutex;
h_Mutex=CreateMutex(NULL,FALSE,(LPCWSTR)"mutex_for_readcount");
HANDLE h_Mutex1;
h_Mutex1=CreateMutex(NULL,FALSE,(LPCWSTR)"mutex1");
HANDLE h_Mutex2;
h_Mutex2=CreateMutex(NULL,FALSE,(LPCWSTR)"mutex2");
HANDLE h_Mutex3;
h_Mutex3=CreateMutex(NULL,FALSE,(LPCWSTR)"mutex3");
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//线程对象的数组
HANDLE *h_Thread = (HANDLE *)malloc(sizeof(HANDLE)*buffersize);;
readcount=0; //初始化readcount
//InitializeCriticalSection(&RP_Write); //初始化临界区
InitializeCriticalSection(&cs_Read);
InitializeCriticalSection(&cs_Write);
for(int i=0;i<(int)(n_thread);i++)
{
if(ThreadInfo[i].entity == READER|| ThreadInfo[i].entity == 'p')
{
//创建写线程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&ThreadInfo[i],0,&thread_ID);
}
else
{
//创建读者进程
h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&ThreadInfo[i],0,&thread_ID);
}
}
//等待所有的线程结束
wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("Game over!\n");
_getch();
return 1;
}
评论0