操作系统实验报告——进程同步与互斥(共12页).doc
精选优质文档-倾情为你奉上进程同步与互斥实验报告实验序号:01 实验项目名称:进程同步与互斥学号姓名专业、班实验地点指导教师时间一、实验目的1、掌握基本的进程同步与互斥算法,理解生产者-消费者问题。2、学习使用Windows 2000/XP中基本的同步对象,掌握相关API的使用方法。3、了解Windows 2000/XP中多线程的并发执行机制,实现进程的同步与互斥。4、设计程序,实现生产者-消费者进程(线程)的同步与互斥;二、实验环境Windows 2000/XP + Visual C+ 6.0 三、实验内容以生产者-消费者模型为依据,在Windows 2000/XP环境下创建一个控制台进程,在该进程中创建n个线程模拟生产者和消费者,实现进程(线程)的同步与互斥。四、设计思路和流程框图生产者进程的功能:生产东西,供消费者消费;消费者进程的功能:消费生产者生产的东西。生产者生产产品并存入缓冲区供消费者取走使用,消费者从缓冲器内取出产品去消费。在生产者和消费者同时工作时,必须禁止生产者将产品放入已装满的缓冲器内,禁止消费者从空缓冲器内取产品。五、源程序(含注释)清单#include<windows.h>#include<fstream.h>#include<stdio.h>#include<string>#include<conio.h>/定义一些常量;/本程序允许的最大临界区数;#define MAX_BUFFER_NUM10/秒到微秒的乘法因子;#define INTE_PER_SEC 1000/本程序允许的生产和消费线程的总数;#define MAX_THREAD_NUM 64/定义一个结构,记录在测试文件中指定的每一个线程的参数struct ThreadInfointserial;/线程序列号charentity; /是P还是Cdoubledelay;/线程延迟intthread_requestMAX_THREAD_NUM; /线程请求队列intn_request;/请求个数;/全局变量的定义/临界区对象的声明,用于管理缓冲区的互斥访问;CRITICAL_SECTIONPC_CriticalMAX_BUFFER_NUM;int Buffer_CriticalMAX_BUFFER_NUM; /缓冲区声明,用于存放产品;HANDLE h_ThreadMAX_THREAD_NUM; /用于存储每个线程句柄的数组;ThreadInfoThread_InfoMAX_THREAD_NUM; /线程信息数组;HANDLEempty_semaphore; /一个信号量;HANDLEh_mutex; /一个互斥量;DWORDn_Thread = 0; /实际的线程的数目;DWORDn_Buffer_or_Critical; /实际的缓冲区或者临界区的数目;HANDLEh_SemaphoreMAX_THREAD_NUM; /生产者允许消费者开始消费的信号量;/生产消费及辅助函数的声明void Produce(void *p);void Consume(void *p);bool IfInOtherRequest(int);int FindProducePositon();int FindBufferPosition(int);int main(void)/声明所需变量;DWORDwait_for_all;ifstreaminFile;/初始化缓冲区;for(int i=0;i< MAX_BUFFER_NUM;i+)Buffer_Criticali = -1;/初始化每个线程的请求队列;for(int j=0;j<MAX_THREAD_NUM;j+)for(int k=0;k<MAX_THREAD_NUM;k+)Thread_Infoj.thread_requestk = -1;Thread_Infoj.n_request = 0;/初始化临界区;for(i =0;i< MAX_BUFFER_NUM;i+) InitializeCriticalSection(&PC_Criticali);/打开输入文件,按照规定的格式提取线程等信息;inFile.open("test.txt");/从文件中获得实际的缓冲区的数目;inFile >> n_Buffer_or_Critical;inFile.get();printf("输入文件是:n");/回显获得的缓冲区的数目信息;printf("%d n",(int) n_Buffer_or_Critical);/提取每个线程的信息到相应数据结构中;while(inFile)inFile >> Thread_Infon_Thread.serial;inFile >> Thread_Infon_Thread.entity;inFile >> Thread_Infon_Thread.delay;char c;inFile.get(c);while(c!='n'&& !inFile.eof() inFile>> Thread_Infon_Thread.thread_requestThread_Infon_Thread.n_request+; inFile.get(c);n_Thread+; /回显获得的线程信息,便于确认正确性;for(j=0;j<(int) n_Thread;j+)int Temp_serial = Thread_Infoj.serial;char Temp_entity = Thread_Infoj.entity;double Temp_delay = Thread_Infoj.delay;printf(" n thread%2d %c %f ",Temp_serial,Temp_entity,Temp_delay);int Temp_request = Thread_Infoj.n_request;for(int k=0;k<Temp_request;k+)printf(" %d ", Thread_Infoj.thread_requestk);cout<<endl;printf("nn"); /创建在模拟过程中几个必要的信号量empty_semaphore=CreateSemaphore(NULL,n_Buffer_or_Critical,n_Buffer_or_Critical, "semaphore_for_empty");h_mutex= CreateMutex(NULL,FALSE,"mutex_for_update");/下面这个循环用线程的ID号来为相应生产线程的产品读写时所/使用的同步信号量命名;for(j=0;j<(int)n_Thread;j+) std:string lp ="semaphore_for_produce_"int temp =j;while(temp)char c = (char)(temp%10);lp+=c;temp/=10;h_Semaphorej+1=CreateSemaphore(NULL,0,n_Thread,lp.c_str();/创建生产者和消费者线程;for(i =0;i< (int) n_Thread;i+)if(Thread_Infoi.entity ='P')h_Threadi= CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Produce),&(Thread_Infoi),0,NULL);else h_Threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Consume),&(Thread_Infoi),0,NULL);/主程序等待各个线程的动作结束;wait_for_all = WaitForMultipleObjects(n_Thread,h_Thread,TRUE,-1);printf(" n nALL Producer and consumer have finished their work. n");printf("Press any key to quit!n");_getch();return 0;/确认是否还有对同一产品的消费请求未执行;bool IfInOtherRequest(int req)for(int i=0;i<n_Thread;i+)for(int j=0;j<Thread_Infoi.n_request;j+)if(Thread_Infoi.thread_requestj = req)return TRUE;return FALSE;/找出当前可以进行产品生产的空缓冲区位置;intFindProducePosition()int EmptyPosition;for (int i =0;i<n_Buffer_or_Critical;i+)if(Buffer_Criticali = -1)EmptyPosition = i;/用下面这个特殊值表示本缓冲区正处于被写状态;Buffer_Criticali = -2;break;return EmptyPosition;/找出当前所需生产者生产的产品的位置;int FindBufferPosition(int ProPos)int TempPos;for (int i =0 ;i<n_Buffer_or_Critical;i+)if(Buffer_Criticali=ProPos)TempPos = i;break;return TempPos;/生产者进程void Produce(void *p)/局部变量声明;DWORDwait_for_semaphore,wait_for_mutex,m_delay;intm_serial;/获得本线程的信息;m_serial = (ThreadInfo*)(p)->serial;m_delay = (DWORD)(ThreadInfo*)(p)->delay *INTE_PER_SEC);Sleep(m_delay);/开始请求生产printf("Producer %2d sends the produce require.n",m_serial);/确认有空缓冲区可供生产,同时将空位置数empty减1;用于生产者和消费者的同步;wait_for_semaphore= WaitForSingleObject(empty_semaphore,-1);/互斥访问下一个可用于生产的空临界区,实现写写互斥;wait_for_mutex = WaitForSingleObject(h_mutex,-1);int ProducePos = FindProducePosition(); ReleaseMutex(h_mutex);/生产者在获得自己的空位置并做上标记后,以下的写操作在生产者之间可以并发;/核心生产步骤中,程序将生产者的ID作为产品编号放入,方便消费者识别;printf("Producer %2d begin to produce at position %2d.n",m_serial,ProducePos);Buffer_CriticalProducePos = m_serial;printf("Producer %2d finish producing :n ",m_serial);printf(" position %2d :%3d n" ,ProducePos,Buffer_CriticalProducePos);/使生产者写的缓冲区可以被多个消费者使用,实现读写同步;ReleaseSemaphore(h_Semaphorem_serial,n_Thread,NULL);/消费者进程void Consume(void * p)/局部变量声明;DWORDwait_for_semaphore,m_delay;intm_serial,m_requestNum; /消费者的序列号和请求的数目;intm_thread_requestMAX_THREAD_NUM;/本消费线程的请求队列;/提取本线程的信息到本地;m_serial = (ThreadInfo*)(p)->serial;m_delay = (DWORD)(ThreadInfo*)(p)->delay *INTE_PER_SEC);m_requestNum = (ThreadInfo *)(p)->n_request;for (int i = 0;i<m_requestNum;i+)m_thread_requesti = (ThreadInfo*)(p)->thread_requesti;Sleep(m_delay);/循环进行所需产品的消费for(i =0;i<m_requestNum;i+) /请求消费下一个产品 printf("Consumer %2d request to consume %2d productn",m_serial,m_thread_requesti); /如果对应生产者没有生产,则等待;如果生产了,允许的消费者数目-1;实现了读写同步; wait_for_semaphore=WaitForSingleObject(h_Semaphorem_thread_requesti,-1); /查询所需产品放到缓冲区的号 int BufferPos=FindBufferPosition(m_thread_requesti); /开始进行具体缓冲区的消费处理,读和读在该缓冲区上仍然是互斥的; /进入临界区后执行消费动作;并在完成此次请求后,通知另外的消费者本处请求已 /经满足;同时如果对应的产品使用完毕,就做相应处理;并给出相应动作的界面提 /示;该相应处理指将相应缓冲区清空,并增加代表空缓冲区的信号量; EnterCriticalSection(&PC_CriticalBufferPos); printf("Consumer%2d begin to consume %2d product n",m_serial,m_thread_requesti); (ThreadInfo*)(p)->thread_requesti =-1; if(!IfInOtherRequest(m_thread_requesti)Buffer_CriticalBufferPos = -1;/标记缓冲区为空;printf("Consumer%2d finish consuming %2d:n ",m_serial,m_thread_requesti);printf(" position %2d :%3d n" ,BufferPos,Buffer_CriticalBufferPos);ReleaseSemaphore(empty_semaphore,1,NULL); elseprintf("Consumer %2d finish consuming product %2dn ",m_serial,m_thread_requesti); /离开临界区 LeaveCriticalSection(&PC_CriticalBufferPos);六、测试结果以及实验总结1、 通过实验进一步了解了基本的进程同步与互斥算法,理解生产者-消费者问题2、掌握了相关API的使用方法。3、了解到进程是一个可以拥有资源的基本单位,是一个可以独立调度和分派的基本单位。而线程是进程中的一个实体,是被系统独立调度和分配的基本单位,故又称为轻权(轻型)进程(Light Weight Process)。4、了解到同步对象是指Windows中用于实现同步与互斥的实体,包括信号量(Semaphore)、互斥量(Mutex)、临界区(Critical Section)和事件(Events)等。本实验中使用到信号量、互斥量和临界区三个同步对象。成绩备注:实验报告文档的名称:姓名_实验编号 (例如:张三_1、张三_2);实验报告发送到:os365专心-专注-专业