操作系统原理实验报告

loading 分享 2026-8-20 下载文档

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文档下载到电脑
搜索更多关于: 操作系统原理实验报告 的文档
相关推荐
相关阅读