已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 儿科护理学考试题及答案
- 部编人教六年级上册道德与法治教案含教学反思
- 阳江分布式光伏项目可行性研究报告
- 黄花深加工实施方案
- 广西中考物理5年(2021-2025)真题分类汇编:专题17 信息的传递与能源与可持续发展(原卷版)
- 2020-2025年初级银行从业资格之初级风险管理押题练习试题A卷含答案
- 山洪灾害预防指南
- 武炼巅峰 协议书转折
- 房屋继承遗产协议书
- 网络协议书哪个最快
- 海南医院面试题目及答案
- 血液内科缺铁性贫血教案
- GB/T 5760-2025塑料离子交换树脂氢氧型阴离子交换树脂交换容量的测定
- 2025-2026学年山东师范大学附中高三上学期10月段考地理试题及答案
- 合规经理岗位常见面试题及答案
- 磁共振安全注意事项课件
- 2025年农电工考试题库题及答案
- 浙江省杭州市2026届高三上学期教学质量检测数学试卷(杭州一模)(含答案)
- 2025贵州毕节市中医医院招聘暨人才引进编外聘用专业技术人员78人考试笔试备考题库及答案解析
- 2025广东广州生态环境监测中心站招聘编外人员4人笔试考试备考试题及答案解析
- 2025年互联网营销师(视频创推员)三级理论考试题库及答案
评论
0/150
提交评论