您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 实验1编程实现进程(线程)同步和互斥
实验1编程实现进程(线程)同步和互斥一、实验目的①通过编写程序实现进程同步和互斥,使学生掌握有关进程(线程)同步与互斥的原理,以及解决进程(线程)同步和互斥的算法,从而进一步巩固进程(线程)同步和互斥②等有关的内容。③了解Windows2000/XP中多线程的并发执行机制,线程间的同步和互斥。④学习使用Windows2000/XP中基本的同步对象,掌握相应的⑤API函数。⑥掌握进程和线程的概念,进程(线程)的控制原语或系统调用的使用。⑦掌握多道程序设计的基本理论、方法和技术,培养学生多道程序设计的能力。二、实验内容在WindowsXP、Windows2000等操作系统下,使用的VC、VB、java或C等编程语言,采用进程(线程)同步和互斥的技术编写程序实现生产者消费者问题或哲学家进餐问题或读者-写者问题或自己设计一个简单进程(线程)同步和互斥的实际问题。三、实验要求①经调试后程序能够正常运行。②采用多进程或多线程方式运行,体现了进程(线程)同步和互斥的关系。③程序界面美观。四、实验步骤、过程让写者与读者、读者与读者之间互斥的访问同一数据集,在无写者进程到来时各读者可同时的访问数据集,在读者和写者同时等待时写者优先唤醒。设置两个全局变量readcount和writecount来记录读者与写者的数目,设置了3个信号量。h_mutex1表示互斥对象对阻塞在read这一个过程实现互斥,h_mutex2实现全局变量readcount操作上的互斥,h_mutex3实现对全局变量writecount的互斥访问。设置了两个临界区,为了实现写者优先,用了临界区read。数据结构:(1)用了两个临界区(2)自定义结构ThreadInfo记录一条线程信息,多个线程对应一个ThreadInfo数组。(3)设置了互斥量h_mutex1,实现了互斥对象对阻塞read这一过程,h_mutex2实现对readcount操作的互斥,h_mutex3实现对writecount的互斥访问。流程:(1)主函数(2)临界区初始化(3)提取线程相关信息(4)完成线程相关信号量的初始化(5)创建读者与写者线程(6)等待所有线程结束(7)程序结束(8)读者(9)提取本线程信息(10)等待,提出读请求(11)进入临界区(12)进行读操作,并判读是否读完(15)退出临界区,读操作完成(16)如果readcount=0,则唤醒写者(17)写者(18)提取本线程信息(19)等待,发出写请求(20)进入临界区(21)进行写操作,并判断是否写完(22)退出临界区,写操作完成(23)如果writecount=0,则唤醒读者代码:#includewindows.h#includefstream.h#includestdio.h#includestring#includeconio.h#defineINTE_PER_SEC1000//每秒时钟中断的数目#defineMAX_THREAD_NUM64//最大线程数intreadcount=0;//读者数目intwritecount=0;//写者数目CRITICAL_SECTIONcs_write;CRITICAL_SECTIONcs_read;voidWriter(void*p);voidReader(void*p);//定义记录在测试文件中的线程参数数据结构structThreadInfo{intserial;charentity;doubledelay;doublepersist;};//全局变量定义//临界区对象的声明,用于管理缓冲区的互斥访问intm;HANDLEh_Thread[MAX_THREAD_NUM];//存储线程句柄的数组ThreadInfoThread_Info[MAX_THREAD_NUM];//线程信息数组HANDLEh_mutex1;//互斥对象对阻塞在read这一个过程实现互斥HANDLEh_mutex2;//全局变量readcount实现操作上的互斥DWORDn_Thread=0;//实际线程数目HANDLEh_mutex3;//对全局变量writecount的互斥访问intmain(void){DWORDwait_for_all;FILE*inFile;//ifstreaminFile;inti,j;InitializeCriticalSection(&cs_write);//初始化临界区InitializeCriticalSection(&cs_read);//打开输入文件,提取线程信息inFile=fopen(test.txt,r);fscanf(inFile,%d,&m);//提取各线程信息到相应的数据结构中while(!feof(inFile)){fscanf(inFile,%d,&Thread_Info[n_Thread].serial);fscanf(inFile,\t%c,&Thread_Info[n_Thread].entity);fscanf(inFile,%lf,&Thread_Info[n_Thread].delay);fscanf(inFile,%lf,&Thread_Info[n_Thread].persist);n_Thread++;}//回显获得的线程信息,便于确认正确性for(j=0;j(int)n_Thread;j++){intTemp_serial=Thread_Info[j].serial;charTemp_entity=Thread_Info[j].entity;doubleTemp_delay=Thread_Info[j].delay;doubleTemp_persist=Thread_Info[j].persist;printf(\nthread%2d%c%lf%lf,Temp_serial,Temp_entity,Temp_delay,Temp_persist);printf(\n);}printf(\n\n);//创建模拟过程中必要的信号量h_mutex1=CreateMutex(NULL,FALSE,mutex_for_readcount);h_mutex2=CreateMutex(NULL,FALSE,mutex_for_readwait);h_mutex3=CreateMutex(NULL,FALSE,mutex_for_writecount);//创建读者和写者线程for(i=0;i(int)(n_Thread);i++){if(Thread_Info[i].entity=='R'||Thread_Info[i].entity=='r'){h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Reader),&(Thread_Info[i]),0,NULL);}else{h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Writer),&(Thread_Info[i]),0,NULL);}}//主程序等待各个线程的动作结束wait_for_all=WaitForMultipleObjects(n_Thread,h_Thread,TRUE,-1);printf(\n\nALLREADERandWRITERhavefinishedtheirwork,\n);printf(Pressanykeytoquit!\n);return0;}//读者线程voidReader(void*p){//局部变量声明DWORDwait_for_semaphore,wait_for_mutex;DWORDm_delay;DWORDm_persist;intm_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(Readerthread%dsendsreaderingrequire.\n,m_serial);wait_for_semaphore=WaitForSingleObject(h_mutex1,-1);EnterCriticalSection(&cs_read);wait_for_mutex=WaitForSingleObject(h_mutex2,-1);readcount++;if(readcount==1){//如果是第1个读者,等待写者写完EnterCriticalSection(&cs_write);}ReleaseMutex(h_mutex2);//释放互斥信号Mutex2//让其他读者进去临界区LeaveCriticalSection(&cs_read);ReleaseMutex(h_mutex1);//读文件printf(Readerthread%dbeginstoreadfile.\n,m_serial);Sleep(m_persist);//退出线程printf(Readerthread%dfinishedreadingfile.\n,m_serial);//阻塞互斥对象Mutex2,保证对readcount的访问,修改互斥wait_for_mutex=WaitForSingleObject(h_mutex2,-1);readcount--;if(readcount==0){//最后一个读者,唤醒写者LeaveCriticalSection(&cs_write);}ReleaseMutex(h_mutex2);//释放互斥信号}//写者线程voidWriter(void*p){DWORDwait_for_mutex3;//互斥变量DWORDm_delay;//延迟时间DWORDm_persist;//读文件持续时间intm_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(Writerthread%dsentsthewritingrequire.\n,m_serial);wait_for_mutex3=WaitForSingleObject(h_mutex3,-1);writecount++;//修改写者数目if(writecount==1){EnterCriticalSection(&cs_read);}ReleaseMutex(h_mutex3);EnterCriticalSection(&cs_write);printf(Writerthread%dbeginstowritetothefile.\n,m_serial);Sleep(m_persist);printf(Writerthread%dfinishedwritingtothefile.\n,m_serial);LeaveCriticalSection(&cs_write);wait_for_mutex3=WaitForSingleObject(h_mutex3,-1);writecount--;if(writecount==0){LeaveCriticalSection(&cs_read);}ReleaseMutex(h_mutex3);}五、实验结果或总结1、测试文件1R232R353W114R1055R1246W987R1368W1259R201010W
本文标题:实验1编程实现进程(线程)同步和互斥
链接地址:https://www.777doc.com/doc-2005235 .html