操作系统实践_第1页
操作系统实践_第2页
操作系统实践_第3页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、学院:计算机科学学院 专业: 软件工程2018 年4 月20 日姓 名班 级课程名称苗琪2班操作系统实践学号指导老2016040121078李 琼成实验名称进程同步与互斥的模拟与实现绩实实验报告实验目的重点:进程的互斥、进程的同步、锁、PV 信号灯。难点:进程同步与互斥的实现。实验内容请使用 PV 信号灯方法解决并实现生产者消费者问题。实验环境硬件环境:PC 机软件环境:Windows 操作系统,用JAVA 语言实现;或Linux 操作系统,用 C 语言实现。实验方法和步骤(含设计)要求:有问题分析过程,算法(用伪代码描述),源代码等。PC12K12Cn 个缓冲区组成缓冲池,共同完成“生产和消

2、费”任务。每个缓冲区存放一个m消息,生产者将生产出的消息放入空缓冲区,消费者从满缓冲区中取出消息。当所有缓冲区均满时,生产者必须等待消费者提供空缓冲区;当缓冲池中所有缓冲区全为空时, 消费者必须等待生产者提供有消息缓冲区。另外,对所有生产者和消费者进程来说,把缓冲池看成一个整体,因此缓冲池是临界资源,即任何一个进程在对池中某个缓冲区进行“存”或“取”操作时须和其他进程互斥执行。用信号量机制来解决这种问题,首先定义下列公共信号量。 信号量 mutex。初值为 1,用于控制互斥访问缓冲池。full0,用于计数。full数。emptyn,用于计数。Empty区数。有限缓冲区生产者/消费者进程描述如下

3、: typedef struct.item;/消息类型typedef struct struct item inst; struct buffer*next;buffer;/缓冲类型semaphore full,empty,mutex; /信号量struct item nextp,nextc;/消息变full=0;/设置信号量初值empty=n; mutex=1;doproduce an item in nextp P(empty);P(mutex);/获得一个空缓冲区/nextp/V(full);V(mutex);while(1);消费者进程代码框架如下: doP(full);P(mutex

4、);/获得一个满缓冲区/nextc/将缓冲区还回空缓冲队列V(empty);V(mutex);consume theiteminnextcwhile(1);流程图如下:源 代 码 如 下 : #include #includeconst unsigned short SIZE_OF_BUFFER=10; /thelengthofbuffer unsigned shortbufferSIZE_OF_BUFFER=0; /createspacefor buffer unsigned shortin=0;/the markposition enteringthe spaceunsigned shor

5、tout=0;/the markposition leavingthe spaceunsigned shortProduct_ID=0;/theIDof from 1 to10,notfor countunsigned shortConsume_ID=0;/theIDof product_ID inthe bufferunsigned intproduce_sum=0;/the totalproducenumber unsigned intconsume_sum=0;/the totalconsume HANDLE mutex;/themutex betweenthreadsHANDLE Fu

6、ll_Semaphore; /the resource semaphore: buffer is HANDLE Empty_Semaphore; /the resource semaphore: buffer is const unsigned short p_count=20; /the number of produce one const unsigned short c_count=6; /the number of consumer one const unsigned short s_count=p_count+c_count; /the sum number threadsHAN

7、DLE threadss_count;/the handle of every DWORD Producer_IDp_count;/the mark of producer DWORD Consumer_IDc_count;/the mark of consumer unsigned short control=1; /control the program run or stopDWORD WINAPI producer(LPVOID);/theproducer threadDWORD WINAPI consumer(LPVOID);/theconsumer void produce();v

8、oid consume();void Create_P_Threads();/createproducer threadvoid Create_C_Threads();/createconsumer void Product_Sum();/print theotal of remainproduct numberandprint thebuffervoid info();/infovoid Product_Sum()inti,sum=0; for(i=0;iSIZE_OF_BUFFER;i+)if(bufferi!=0) sum+;std:coutsumfor(i=0;iSIZE_OF_BUF

9、FER;i+)std:coutbufferi ;printf(n);void produce()int i; std:cout=10) Product_ID=0;Product_ID+; produce_sum+; bufferin=Product_ID;printf( buffer%d=%din=(in+1)%SIZE_OF_BUFFER;Product_Sum();void consume()int i; std:coutconsume; consume_sum+; Consume_ID=bufferout;printf( buffer%d=%dbufferout=0;out=(out+1

10、)%SIZE_OF_BUFFER;Product_Sum();DWORDWINAPIproducer(LPVOID)/producerthreadwhile(control)WaitForSingleObject(Full_Semaphore,INFINITE);/resource semaphore operationWaitForSingleObject(mutex,INFINITE);/the mutex operationproduce(); Sleep(1000);ReleaseMutex(mutex);/resource semaphore operationReleaseSema

11、phore(Empty_Semaphore,1,NULL);/the mutex operationreturn 0;DWORD WINAPI consumer(LPVOID)/consumer threadwhile(control)WaitForSingleObject(Empty_Semaphore,INFINITE); WaitForSingleObject(mutex,INFINITE); consume();Sleep(1000); ReleaseMutex(mutex);ReleaseSemaphore(Full_Semaphore,1,NULL);return 0;void C

12、reate_P_Threads()/create threadfor(int i=0;ip_count;i+)threadsi=CreateThread(NULL,0,producer,NULL,0,&Producer_IDi); if(threadsi=NULL)control=0;void Create_C_Threads()for(int i=p_count;is_count;i+)threadsi=CreateThread(NULL,0,consumer,NULL,0,&Consumer_IDi-p_count); if(threadsi=NULL)control=0;void inf

13、o()std:coutnstd:endl;std:cout*I did refer to the program on the web.std:endl; std:cout*and I simplify some things and make it morepowful!std:endl;std:cout*But it is really a word a word knocked out by me based on understandingnstd:endl;std:coutproduce/consumeremain_totalbuffer_state(from 0 9)std:end

14、l;int main()info(); mutex=CreateMutex(NULL,FALSE,NULL);Full_Semaphore=CreateSemaphore(NULL,SIZE_OF_BUFFER,SIZE_OF_BUFFER,NULL); Empty_Semaphore=CreateSemaphore(NULL,0,SIZE_OF_BUFFER,NULL); Create_P_Threads();Create_C_Threads(); while(control)if(getchar()std:coutstd:endl;std:coutthe total produce product number is produce_sumstd:endl; std:coutthe total consume product num

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论