当前位置: 技术问答>linux和unix
linux课程设计读者写者问题,求高手帮小弟改改错误!急呀!!!
来源: 互联网 发布时间:2016-10-30
本文导语: linux课程设计,读者写者问题,好多错误呀,很多类型都一样,怎么改啊??求高手指导 要在linux下运行的,我用gcc编译的 #include "windows.h" #include #include #include #include kecheng设计 #include #include #define READER 'R'...
linux课程设计,读者写者问题,好多错误呀,很多类型都一样,怎么改啊??求高手指导
要在linux下运行的,我用gcc编译的
#include "windows.h"
#include
#include
#include
#include kecheng设计
#include
#include
#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE_PER_SEC 1000 //每秒时钟中断的数目
#define MAX_THREAD_NUM 64 //最大线程数
#define MAX_FILE_NUM 32 //最大文件数目数
#define MAX_STR_LEN 32 //字符串的长度
int readcount=0; //读者数目
int writecount=0; //写者数目
CRITICAL_SECTION RP_Write; //临界资源
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
struct ThreadInfo
{
int serial; //线程序号
char entity; //线程类别(判断是读者还是写者线程)
double delay; //线程延迟时间
double persist; //线程读写操作时间
};
///////////////////////////////////////////////////////////////////////////
// 读者优先---读者线程
//P:读者线程信息
void RP_ReaderThread(void *p)
{ //互斥变量
HANDLE h_Mutex;
h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
DWORD wait_for_mutex; //等待互斥变量所有权
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号
// 从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial ;
m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
Sleep(m_delay); //延迟等待
printf("Reader thread %d sents the reading require.n",m_serial);
//等待互斥信号,保证对ReadCount 的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目增加
readcount++;
if(readcount==1)
{ //第一个读者,等待资源
EnterCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
//读文件
printf("Reader thread %d begins to read file.n",m_serial);
Sleep(m_persist);
//退出线程
printf("Reader thread %d finished reading file.n",m_serial);
//等待互斥信号,保证对ReadCount的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目减少
readcount--;
if(readcount==0)
{ //如果所有的读者读完,唤醒写者
LeaveCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
}
//////////////////////////////////////////////////////////////
//P:写者线程信息
void RP_WriterThread(void *p)
{ DWORD m_delay; //延迟时间
DWORD m_persist; //写文件持续时间
int m_serial; //线程序号
// 从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial ;
m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
Sleep(m_delay);
printf("Write thread %d sents the writing require.n",m_serial);
//等待资源
EnterCriticalSection(&RP_Write);
//写文件
printf("Writer thread %d begins to write to the file.n",m_serial);
Sleep(m_persist);
//退出线程
printf("Write thread %d finished writing to the file.n",m_serial);
//释放资源
LeaveCriticalSection(&RP_Write);
}
//////////////////////////////////////////////////////////////
//读者优先处理函数
//file:文件名
void ReaderPriority(char *file)
{ DWORD n_thread=0; //线程数目
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//互斥对象
HANDLE h_Mutex;
h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");
//线程对象的数组
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount=0; //初始化readcount
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile;
inFile.open (file);
printf("Reader Priority:nn");
while(inFile)
{ //读入每一个读者,写者的信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread++].persist;
inFile.get();
}
for(int i=0;i
要在linux下运行的,我用gcc编译的
#include "windows.h"
#include
#include
#include
#include kecheng设计
#include
#include
#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE_PER_SEC 1000 //每秒时钟中断的数目
#define MAX_THREAD_NUM 64 //最大线程数
#define MAX_FILE_NUM 32 //最大文件数目数
#define MAX_STR_LEN 32 //字符串的长度
int readcount=0; //读者数目
int writecount=0; //写者数目
CRITICAL_SECTION RP_Write; //临界资源
CRITICAL_SECTION cs_Write;
CRITICAL_SECTION cs_Read;
struct ThreadInfo
{
int serial; //线程序号
char entity; //线程类别(判断是读者还是写者线程)
double delay; //线程延迟时间
double persist; //线程读写操作时间
};
///////////////////////////////////////////////////////////////////////////
// 读者优先---读者线程
//P:读者线程信息
void RP_ReaderThread(void *p)
{ //互斥变量
HANDLE h_Mutex;
h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
DWORD wait_for_mutex; //等待互斥变量所有权
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号
// 从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial ;
m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
Sleep(m_delay); //延迟等待
printf("Reader thread %d sents the reading require.n",m_serial);
//等待互斥信号,保证对ReadCount 的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目增加
readcount++;
if(readcount==1)
{ //第一个读者,等待资源
EnterCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
//读文件
printf("Reader thread %d begins to read file.n",m_serial);
Sleep(m_persist);
//退出线程
printf("Reader thread %d finished reading file.n",m_serial);
//等待互斥信号,保证对ReadCount的访问,修改互斥
wait_for_mutex=WaitForSingleObject(h_Mutex,-1);
//读者数目减少
readcount--;
if(readcount==0)
{ //如果所有的读者读完,唤醒写者
LeaveCriticalSection(&RP_Write);
}
ReleaseMutex(h_Mutex); //释放互斥信号
}
//////////////////////////////////////////////////////////////
//P:写者线程信息
void RP_WriterThread(void *p)
{ DWORD m_delay; //延迟时间
DWORD m_persist; //写文件持续时间
int m_serial; //线程序号
// 从参数中获得信息
m_serial=((ThreadInfo*)(p))->serial ;
m_delay=(DWORD)(((ThreadInfo*)(p))->delay *INTE_PER_SEC);
m_persist=(DWORD)(((ThreadInfo*)(p))->persist *INTE_PER_SEC);
Sleep(m_delay);
printf("Write thread %d sents the writing require.n",m_serial);
//等待资源
EnterCriticalSection(&RP_Write);
//写文件
printf("Writer thread %d begins to write to the file.n",m_serial);
Sleep(m_persist);
//退出线程
printf("Write thread %d finished writing to the file.n",m_serial);
//释放资源
LeaveCriticalSection(&RP_Write);
}
//////////////////////////////////////////////////////////////
//读者优先处理函数
//file:文件名
void ReaderPriority(char *file)
{ DWORD n_thread=0; //线程数目
DWORD thread_ID; //线程ID
DWORD wait_for_all; //等待所有线程结束
//互斥对象
HANDLE h_Mutex;
h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");
//线程对象的数组
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount=0; //初始化readcount
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile;
inFile.open (file);
printf("Reader Priority:nn");
while(inFile)
{ //读入每一个读者,写者的信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread++].persist;
inFile.get();
}
for(int i=0;i