




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精品文档 1欢迎下载 精品文档 2欢迎下载 include include include include include include define MAX PERSON 100 define READER 0 读者 define WRITER 1 写者 define END 1 define R READER define W WRITER typedef struct Person HANDLE m hThread 定义处理线程的句柄 int m nType 进程类型 读写 精品文档 3欢迎下载 int m nStartTime 开始时间 int m nWorkTime 运行时间 int m nID 进程号 Person Person g Persons MAX PERSON int g NumPerson 0 long g CurrentTime 0 基本时间片数 int g PersonLists 进程队列 1 W 3 5 2 W 16 5 3 R 5 2 4 W 6 5 5 R 4 3 6 R 17 7 END int g NumOfReading 0 int g NumOfWriteRequest 0 申请写进程的个数 HANDLE g hReadSemaphore 读者信号 HANDLE g hWriteSemaphore 写者信号 bool finished false 所有的读完成 bool wfinished false 所有的写完成 void CreatePersonList int pPersonList bool CreateReader int StartTime int WorkTime int ID bool CreateWriter int StartTime int WorkTime int ID DWORD WINAPI ReaderProc LPVOID lpParam DWORD WINAPI WriterProc LPVOID lpParam int main 精品文档 4欢迎下载 g hReadSemaphore CreateSemaphore NULL 1 100 NULL 创建信号灯 当前可用的资 源数为 1 最大为 100 g hWriteSemaphore CreateSemaphore NULL 1 100 NULL 创建信号灯 当前可用的 资源数为 1 最大为 100 CreatePersonList g PersonLists Create All the reader and writers printf 创建所有读者写者 n g CurrentTime 0 while true g CurrentTime Sleep 300 300 ms printf 当前时间 d n g CurrentTime if finished return 0 return 0 void CreatePersonList int pPersonLists int i 0 int pList pPersonLists bool Ret while pList 0 END switch pList 1 case R Ret CreateReader pList 2 pList 3 pList 0 351 w452 523 654 精品文档 5欢迎下载 break case W Ret CreateWriter pList 2 pList 3 pList 0 break if Ret printf 创建 d 错误 n pList 0 pList 4 move to next person list DWORD WINAPI ReaderProc LPVOID lpParam 读过程 Person pPerson Person lpParam wait for the start time while g CurrentTime pPerson m nStartTime printf 读者 d 正在请求 n pPerson m nID printf n n n wait for the write request WaitForSingleObject g hReadSemaphore INFINITE if g NumOfReading 0 WaitForSingleObject g hWriteSemaphore INFINITE g NumOfReading ReleaseSemaphore g hReadSemaphore 1 NULL pPerson m nStartTime g CurrentTime printf 读者 d 正在读取共享缓冲区 n pPerson m nID 精品文档 6欢迎下载 printf n n n while g CurrentTime m nStartTime pPerson m nWorkTime printf 读者 d 正在退出 n pPerson m nID printf n n n WaitForSingleObject g hReadSemaphore INFINITE g NumOfReading if g NumOfReading 0 ReleaseSemaphore g hWriteSemaphore 1 NULL 此时没有读者 可以写 ReleaseSemaphore g hReadSemaphore 1 NULL if pPerson m nID 4 finished true 所有的读写完成 ExitThread 0 return 0 DWORD WINAPI WriterProc LPVOID lpParam Person pPerson Person lpParam wait for the start time while g CurrentTime pPerson m nStartTime printf 写者 d 正在请求进行写操作 n pPerson m nID printf n n n WaitForSingleObject g hWriteSemaphore INFINITE modify the writer s real start time 精品文档 7欢迎下载 pPerson m nStartTime g CurrentTime printf 写者 d 正在向共享缓冲区写内容 n pPerson m nID while g CurrentTime m nStartTime pPerson m nWorkTime printf 写者 d 正在退出 n pPerson m nID printf n n n g NumOfWriteRequest ReleaseSemaphore g hWriteSemaphore 1 NULL if pPerson m nID 4 finished true 所有的读写完成 ExitThread 0 return 0 bool CreateReader int StartTime int WorkTime int ID DWORD dwThreadID if g NumPerson MAX PERSON return false Person pPerson pPerson m nID ID pPerson m nStartTime StartTime pPerson m nWorkTime WorkTime pPerson m nType READER g NumPerson Create an New Thread pPerson m hThread 精品文档 8欢迎下载 CreateThread NULL 0 ReaderProc LPVOID pPerson 0 if pPerson m hThread NULL return false return true bool CreateWriter int StartTime int WorkTime int ID DWORD dwThreadID if g NumPerson MAX PERSON return false Person pPerson pPerson m nID ID pPerson m nStartTime StartTime pPerson m nWorkTime WorkTime pPerson m nType WRITER g NumPerson Create an New Thread pPerson
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年河北沧州任丘园区产业发展集团有限公司招聘工作人员10名模拟试卷及答案详解(典优)
- 2025北京小汤山医院招聘8人(第三批)模拟试卷及参考答案详解
- 2025制造企业员工合同样本
- 2025广西来宾市忻城县政府投资审计中心招聘见习生2人考前自测高频考点模拟试题附答案详解(完整版)
- 拖车服务费合同10篇
- 2025年国防安全知识竞赛题库及答案
- 2025年锅炉操作工考试题库及参考答案解析
- 2025年新疆农作物制种技术培训合同协议
- 2025年贵州公需科目大数据培训考试试题及答案
- 2025年广元公需科目继续教育试题和答案
- 2025年9月20日云南省直机关遴选公务员笔试真题及答案解析
- 合同纠纷民事起诉状模板示例
- 2025广东广州市花都区招聘社区专职工作人员50人考试参考题库及答案解析
- 招行ai面试题库大全及答案
- 2025四川蜀道养护集团有限公司招聘15人考试参考题库及答案解析
- 2025年秋季上饶银行招聘考试参考题库及答案解析
- 2025至2030中国汽车模塑件行业项目调研及市场前景预测评估报告
- 投标服务响应应急方案(3篇)
- 自动控制原理课件第九章状态空间分析法
- 2025年安全工程师《安全生产管理》考前模拟卷(一)
- 综合实践 记录我们的校园(教案)北师大版数学三年级上册
评论
0/150
提交评论