进程(线程)同步和互斥实验报告_第1页
进程(线程)同步和互斥实验报告_第2页
进程(线程)同步和互斥实验报告_第3页
进程(线程)同步和互斥实验报告_第4页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、进程(线程)同步和互斥实验报告 操 作 系 统 实 验 报 告 课程名称 操作系统 实验名称 进程(线程)的同步与互斥 成绩 学生姓名 作业君 专业 软件工程 班级、学号 同组者姓名 无 实验日期 2021 一、实验题目: : 进程(线程)的同步与互斥 二、实验目的 : 自行编制模拟程序,通过形象化的状态显示,加深理解进程的概念、进程之间的状态转换及其所带来的 pcb 内容 、组织的变化,理解进程与其 pcb 间的一一对应关系。 1掌握基本的同步与互斥算法,理解生产者消费者模型。 2学习使用 windows 中基本的同步对象,掌握相关 api 的使用方法。 3了解 windows 中多线程的并

2、发执行机制,实现进程的同步与互斥 三、实验内容与要求 : 1实验内容 以生产者/消费者模型为依据,在 windows 环境下创建一个控制台进程,在该进程中创建 n 个线程模拟生产者和消费者,实现进程(线程)的同步与互斥。 2实验要求 学习并理解生产者/消费者模型及其同步/互斥规则; 学习了解 windows 同步对象及其特性; 熟悉实验环境,掌握相关 api 的使用方法; 设计程序,实现生产者/消费者进程(线程)的同步与互斥; 四、算法描述(含数据结构定义)或流程图 #include windows.h #include iostream #include stdio.h #include m

3、ath.h #include stdlib.h #include time.h using namespace std; #define max_thread_num 64 /最大线程数 #define inte_per_sec 1000 /延迟时间的毫秒值 const int size_of_buffer = 10; /缓冲区长度 int productid = 0; /产品号 int consumeid = 0; /将被消耗的产品号 int in = 0; /产品进缓冲区时的缓冲区下标 int out = 0; /产品出缓冲区时的缓冲区下标 bool running = true; /判断

4、程序能否继续执行的逻辑值 int g_buffersize_of_buffer; /缓冲区是个循环队列 handle g_hmutex; /公有信号量,用于线程间的互斥 handle g_hfullsemaphore; /生产者的私有信号量,当缓冲区满时迫使生产者等待 handle g_hemptysemaphore; /消费者的私有信号量,当缓冲区空时迫使消费者等待 /定义一个结构体用于存储线程的信息 struct threadinfo int serial; /线程号 char entity; /线程类别(生产者或消费者) double delay; /等待时间 double persis

5、t; /操作时间 ; /生产者 void producer(void* p) /定义变量用于存储当前线程的信息 dword m_delay; dword m_persist; int m_serial; /从参数中获得信息 m_serial = (threadinfo*)(p)-serial; m_delay = (dword)(threadinfo*)(p)-delay * inte_per_sec); m_persist = (dword)(threadinfo*)(p)-persist * inte_per_sec); while (running) /p 操作 cout 生产者线程 m

6、_serial 请求生产. endl; waitforsingleobject(g_hemptysemaphore, infinite); cout 生产者线程 m_serial 请求独占缓冲区. endl; waitforsingleobject(g_hmutex, infinite); sleep(m_delay); /延迟等待 /生产一个产品 cout 生产者线程 m_serial 生产 +productid 号产品成功. endl; cout 生产者线程 m_serial 请求将产品 productid 投入缓冲区. endl; /把新生产的产品放入缓冲区 g_bufferin = p

7、roductid; in = (in +1)%size_of_buffer; sleep(m_persist); /操作等待 cout 生产者线程 m_serial 将产品 productid 投入缓冲区中成功. endl; /输出缓冲区当前的状态 cout * endl n 当前缓冲区情况如图(代表已有产品,代表没有产品): endl; for (int i = 0;i size_of_buffer;+i) if (g_bufferi != 0) cout ; else cout ; cout nn*n endl; /v 操作 releasemutex(g_hmutex); releases

8、emaphore(g_hfullsemaphore, 1, null); /消费者 void consumer(void* p) dword m_delay; dword m_persist; int m_serial; /从参数中获得信息 m_serial = (threadinfo*)(p)-serial; m_delay = (dword)(threadinfo*)(p)-delay * inte_per_sec); m_persist = (dword)(threadinfo*)(p)-persist * inte_per_sec); while (running) /p 操作 cou

9、t 消费者线程 m_serial 请求消费. endl; waitforsingleobject(g_hfullsemaphore, infinite); cout 消费者线程 m_serial 请求独占缓冲区. endl; waitforsingleobject(g_hmutex,infinite); sleep(m_delay); /延迟等待 /从缓冲区中取出一个产品 cout 消费者线程 m_serial 请求取出一个产品. endl; consumeid = g_bufferout; g_bufferout = 0; out = (out + 1) % size_of_buffer;

10、cout 消费者线程 m_serial 取出产品 consumeid 成功. endl; /消耗一个产品 cout 消费者线程 m_serial 开始消费消费产品 consumeid . endl; sleep(m_persist); cout 消费者线程 m_serial 消费产品 consumeid 成功. endl; /输出缓冲区当前的状态 cout * endl n 当前缓冲区情况如图: endl; for (int i = 0;i size_of_buffer;+i) if (g_bufferi != 0) cout ; else cout ; cout nn*n endl; /v

11、操作 releasemutex(g_hmutex); releasesemaphore(g_hemptysemaphore, 1, null); void prod_cons() /创建互斥信号量 g_hmutex = createmutex(null, false, null); /创建同步信号量 g_hemptysemaphore = createsemaphore(null, size_of_buffer, size_of_buffer, null); g_hfullsemaphore = createsemaphore(null, 0, size_of_buffer, null); s

12、rand(unsigned)time(null); /以时间函数为种子 const unsigned short threads_count = rand() % 5 + 5; /总的线程数(随机生成) /线程对象的数组 handle hthreadsmax_thread_num; threadinfo thread_infomax_thread_num; dword thread_id; /线程 id int num = 0; /临时变量,用于循环语句 cout 系统开始模拟,并自动生成模拟数据. endl; system(pause); /暂停确认开始执行 cout 线程总数: threa

13、ds_count endl; /循环随机生成各个线程的信息 while (num != threads_count) thread_infonum.serial = num + 1; if (rand() % 2 = 1) thread_infonum.entity = "p" else thread_infonum.entity = "c" thread_infonum.delay = rand() % 5 + 1; thread_infonum.persist = rand() % 6 + 2; num+; cout n 系统生成数据结束,模拟数据如

14、下: endl 线程号 线程类别 延迟时间 操作时间 endl; for (int x = 0;x threads_count;x+) cout thread_infox.serial t thread_infox.entity t thread_infox.delay tt thread_infox.persist endl; cout nn=生产者-消费者 开始=n endl; /创建线程 for (int i = 0;i threads_count;i+) /创建生产者线程 if (thread_infoi.entity = "p") hthreadsi = crea

15、tethread(null, 0, (lpthread_start_routine)(producer), thread_infoi, 0, thread_id); /创建消费者线程 else hthreadsi = createthread(null, 0, (lpthread_start_routine)(consumer), thread_infoi, 0, thread_id); while (running) if (getchar() /按回车后终止程序运行 running = false; cout 系统模拟结束. endl; int main() cout n=生产者-消费者

16、模拟=n endl; prod_cons(); 五、实验过程 1、记录生产者和消费者的同步执行过程。 2、分析 producer 函数和 consumer 函数的功能,并画出对应的程序流程图。 producer 函数:调用函数,获取资源情况,然后判断条件是否满足,判断是否执行,接着发出生产请求,请求通过后独占缓冲区资源,接着生产-一个产品投入缓冲区,成功后释放所占缓冲区资源,到此此函数执行完成。 consumer 函数: 通过用变量提取保存提取当前资源信息, 然后判断是否执行, 接着发出消费请求, 请求通过后独占缓冲区资源, 接着消费一个产品取出缓冲区, 成功后释放所占缓冲区资源, 到此此函数执行完成。 3、试将同步和互斥的 p 操作颠倒次序执行,观察并分析程序的运行情况。 答: 如将同步和互斥的 p

温馨提示

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

评论

0/150

提交评论