云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 操作系统原理实验报告

操作系统原理实验报告

  • 62 次阅读
  • 3 次下载
  • 2025/5/6 7:36:03

printf(\

// 阻塞互斥对象mutex3,保证对writecount的访问、修改互斥 wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1);

writecount++; //修改读者数目 if(writecount==1) {

//第一个写者,等待读者读完 EnterCriticalSection(&cs_Read); }

ReleaseMutex(h_Mutex3);

//进入写者临界区

EnterCriticalSection(&cs_Write); //写文件

printf(\Sleep(m_persist);

// 退出线程

printf(\

//离开临界区

LeaveCriticalSection(&cs_Write);

// 阻塞互斥对象mutex3,保证对writecount的访问、修改互斥 wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1); writecount--; //修改读者数目 if(writecount==0) {

//写者写完,读者可以读

LeaveCriticalSection(&cs_Read); }

ReleaseMutex(h_Mutex3); }

/////////////////////////////////////////////////////////////////////////// // 写者优先处理函数 //file:文件名

void WriterPriority(char* file) {

DWORD n_thread=0; //线程数目 DWORD thread_ID; //线程ID

DWORD wait_for_all; //等待所有线程结束

//互斥对象

HANDLE h_Mutex1;

h_Mutex1=CreateMutex(NULL,FALSE,\HANDLE h_Mutex2;

h_Mutex2=CreateMutex(NULL,FALSE,\HANDLE h_Mutex3;

h_Mutex3=CreateMutex(NULL,FALSE,\

//线程对象

HANDLE h_Thread[MAX_THREAD_NUM]; ThreadInfo thread_info[MAX_THREAD_NUM];

readcount=0; // 初始化 readcount writecount=0; // 初始化writecount InitializeCriticalSection(&cs_Write); //初始化临界区

InitializeCriticalSection(&cs_Read); ifstream inFile;

inFile.open(file); //打开文件 printf(\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< (int)(n_thread);i++) {

if (thread_info[i].entity==READER || thread_info[i].entity=='R') {

//创建读者线程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),&thread_info[i],0,&thread_ID);

} else{

//创建写者线程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),&thread_info[i],0,&thread_ID);

} }

//等待所有线程结束

wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1); printf(\}

/////////////////////////////////////////////////////////////////////////////// //主函数

int main(int argc,char* argv[]) { char ch;

while (true) {

//打印提示信息

printf(\ printf(\ 1:Reader Priority\\n\ printf(\ 2:Writer Priority\\n\ printf(\ 3:Exit Priority\\n\

printf(\ printf(\ //如果输入信息不正确,继续输入 do{

ch=(char)_getch( );

}while(ch != '1' &&ch != '2' && ch != '3');

system(\ //选择3,返回 if(ch=='3') return 0; //选择1,读者优先 else if(ch=='1')

ReaderPriority(\

搜索更多关于: 操作系统原理实验报告 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

printf(\ // 阻塞互斥对象mutex3,保证对writecount的访问、修改互斥 wait_for_mutex3= WaitForSingleObject(h_Mutex3,-1); writecount++; //修改读者数目 if(writecount==1) { //第一个写者,等待读者读完 EnterCriticalSection(&cs_Read); } ReleaseMutex(h_Mutex3); //进入写者临界区 EnterCriticalSection(&cs_Write); //写文件 printf(\Sleep(m_persist); // 退出

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com