第一范文网 - 专业文章范例文档资料分享平台

操作系统原理实验报告

来源:用户分享 时间:2025/7/24 5:02:46 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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(\

搜索更多关于: 操作系统原理实验报告 的文档
操作系统原理实验报告.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8vvt84ps8m6d7jn4l8uv58u602x74s012j3_9.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top