资源简介
资源包括进程同步经典算法的读写者问题,读者优先的全部代码机器附带的相关文件。
代码片段和文件信息
#include
#include
#include
#include
using namespace std;
/*=============== class signal ===============*/
class signal //信号量对象.
{
private:
int value;
int queue; //用int型数据模拟等待队列.
public:
signal();
signal(int n);
int P(); //检查临界资源
int V(); //释放临界资源
int Get_Value();
int Get_Queue();
};
const int MaxThread=20;
struct ThreadInfo
{
int num;
char type;
double start;
double time;
} thread_info[MaxThread];
void Creat_Writer(); //添加一个写者
void Del_Writer(); //删除一个写者
void Creat_Reader(); //添加一个读者
void Reader_goon(); //读者进程运行函数
void R_Wakeup(); //唤醒等待读者
void Del_Reader(); //删除一个读者
void Show(); //显示运行状态
HANDLE hX;
HANDLE hWsem;
HANDLE thread[MaxThread];
int readcount;
double totaltime;
void WRITEUNIT(int iProcess)
{
printf(“Thread %d begins to write.\n“iProcess);
Sleep((DWORD)(thread_info[iProcess-1].time*1000));
printf(“End of thread %d for writing.\n“iProcess);
}
void READUNIT(int iProcess)
{
printf(“Thread %d begins to read.\n“iProcess);
Sleep((DWORD)(thread_info[iProcess-1].time*1000));
printf(“End of thread %d for reading.\n“iProcess);
}
DWORD WINAPI reader(LPVOID lpVoid)
{
int iProcess = *(int*)lpVoid;
Sleep((DWORD)(thread_info[iProcess-1].start*1000));
DWORD wait_for=WaitForSingleobject(hXINFINITE);
printf(“Thread %d requres reading.\n“iProcess);
readcount++;
if(readcount==1)WaitForSingleobject(hWsemINFINITE);
ReleaseMutex(hX);
READUNIT(iProcess);
wait_for=WaitForSingleobject(hXINFINITE);
readcount--;
if(readcount==0)
ReleaseSemaphore(hWsem10);
ReleaseMutex(hX);
return iProcess;
}
DWORD WINAPI writer(LPVOID lpVoid)
{
int iProcess = *(int*)lpVoid;
Sleep((DWORD)(thread_info[iProcess-1].start*1000));
printf(“Thread %d requres writing.\n“iProcess);
DWORD wait_for=WaitForSingleobject(hWsemINFINITE);
WRITEUNIT(iProcess);
ReleaseSemaphore(hWsem10);
return iProcess;
}
int main()
{
int threadNum;
int threadcount;
ifstream file;
hX=CreateMutex(NULL FALSE NULL);
hWsem=CreateSemaphore(NULL11NULL);
//???????????????????
readcount=0;
threadcount=0;
totaltime=0;
file.open(“thread.dat“ios::in);
if(file==0)
{
printf(“File Open Error.\n“);
return 0;
}
while(file>>threadNum)
{
thread_info[threadNum-1].num=threadNum;
file>>thread_info[threadNum-1].type;
file>>thread_info[threadNum-1].start;
file>>thread_info[threadNum-1].time;
totaltime+=thread_info[threadNum-1].time;
switch(thread_info[threadNum-1].type)
{
case ‘W‘:
printf(“Creating Thread %d for writing.\n“thread_info[threadNum-1].num);
thread[threadNu
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-12-26 17:34 read_write\bin\
目录 0 2015-12-26 17:36 read_write\bin\Debug\
文件 973622 2015-12-26 17:36 read_write\bin\Debug\read_write.exe
文件 3525 2015-12-26 17:36 read_write\main.cpp
文件 3278 2015-12-26 17:33 read_write\main.cpp.save
目录 0 2015-12-26 17:34 read_write\obj\
目录 0 2015-12-26 17:36 read_write\obj\Debug\
文件 19717 2015-12-26 17:36 read_write\obj\Debug\main.o
文件 1115 2015-12-26 17:31 read_write\read_write.cbp
文件 138 2015-12-26 17:54 read_write\read_write.depend
文件 320 2015-12-27 21:42 read_write\read_write.layout
文件 322 2015-12-27 21:08 read_write\read_write.layout.save
文件 53 2015-12-26 17:54 read_write\thread.dat
评论
共有 条评论