- 22
- 1
- 约9.52千字
- 约 9页
- 2018-02-06 发布于河南
- 举报
期中大作业
期中大作业报告
作业内容:
以生产者/消费者模型为依据,在Windows2000/xp环境下创建一个控制台进程,在该进程中创建n个线程模拟生产者和消费者,实现进程(线程)的同步与互斥。
作业要求:
学习并理解生产者/消费者模型及其同步/互斥规则;
学习了解Windows同步对象及其特性;
熟悉编程环境,掌握相关API的使用方法;
设计程序,实现生产者/消费者进程(线程)的同步与互斥;
提交大作业(打印版:包含标题,作业内容,流程图,运行结果及分析,程序)。
流程图:
运行结果及分析:
运行结果:
输入文件是:
3
thread 1 P 3.000000
thread 2 P 4.000000
thread 3 C 4.000000 1
thread 4 P 2.000000
thread 5 C 3.000000 1 2 4
thread 0
Producer 4 sends the produce require.
Producer 4 begin to produce at position 0.
Producer 4 finish producing :
Position[ 0 ]: 4
Consumer 5 request to consume 1 product
Consumer 5 request to consume 1 product
Producer 1 sends the produce require.
Producer 1 begin to produce at position 0.
Producer 1 finish producing :
Position[ 0 ]: 1
Consumer 5 begin to cosume 1 product
Consumer 5 finish consuming product 1
Consumer 5 request to consume 2 product
Producer 2 sends the produce require.
Producer 2 sends the produce require.
Consumer 3 request to consume 1 product
Producer 2 begin to produce at position 0.
Consumer 3 begin to cosume 1 product
Producer 2 finish producing :
Consumer 3 finish consuming 1:
Position[ 0 ]: -1
position[ 0]: -1
Consumer 5 begin to cosume 2 product
Consumer 5 finish consuming 2:
position[ 0]: -1
Consumer 5 request to consume 4 product
Consumer 5 begin to cosume 4 product
Consumer 5 finish consuming 4:
position[ 0]: -1
ALL Producer and Consumer have finished their work,
Press any key to quit!
Press any key to continue
分析:
(1)程序分析:
同步对象的使用步骤:
创建/初始化同步对象。
请求同步对象,进入临界区(互斥量上锁)。
释放同步对象(互斥量解锁)。
这些对象在一个线程中创建,在其他线程中都可以使用,实现同步与互斥。
相关API的功能及使用
我们利用Windows SDK提供的API编程实现题目要求,而VC中包含有Windows SDK的所有工具和定义。要使用这些API,需要包含对这些函数进行说明的SDK头文件——最常见的是Windows.h(特殊的API调用还需要包含其他头文件)。
数据结构
(a) 用整型数组Buffer_Critical表示缓冲区。
(b) 用自定义结构ThreadInfo记录一条线程信息,多个线程对应一个ThreadInfo数组。
(c) 通过如下同步对象实现互斥:
设一个互斥量h-mutex,实现生产者在查询和保留缓冲区的下一个空位置时进行互斥。
设置h_Semaphore[MAX_THREAD_NUM]信号量数组表示相应产品已经生产,实现生产者与消费者之间的同步。同时,用表示空缓冲区树木的信号量empty_semephore指示是否存在空位置,实现类似的同步,以便开始下一个产
您可能关注的文档
最近下载
- 2026年成都锦江人才发展有限责任公司公开招聘成都市锦江区编外人员的备考题库及完整答案详解一套.docx VIP
- 001-CB33附表2 已完工程量汇总表.xls VIP
- 《演讲与口才》全套教学课件.pptx
- 2025年南京社区专职工作人员招聘考试笔试试卷【附答案】.pdf
- M-PM-003-00 药品生产场地管理文件(SMF)管理规程.docx VIP
- 招标代理服务质量保证措施.pdf VIP
- 提升班组长胜任力.doc VIP
- 2025高考英语试题分类汇编:三大从句(全国通用)含解析.pdf VIP
- 2025年度党员领导干部民主生活会上的主持词(带表态总结发言).docx VIP
- 班组长胜任力模型及考核.pdf VIP
原创力文档

文档评论(0)