教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 资格考试 >

东北大学操作系统实验2

来源:网络收集 时间:2026-02-09
导读: 操作系统实验报告 实验名称 班 级 学 号 姓 名 实验二: 进程同步和通信——生产者和消费 者问题模拟 物联网1201班 20124052 李垠桥 一. 实验目的 这是一个验证型实验。通过对给出的程序进行验证、修改,进一步加深理解进程的概念,了解同步和通信的过程,掌

操作系统实验报告

实验名称

班 级 学 号 姓 名

实验二:

进程同步和通信——生产者和消费

者问题模拟

物联网1201班 20124052 李垠桥

一. 实验目的 这是一个验证型实验。通过对给出的程序进行验证、修改,进一步加深理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。通过补充新功能,加强对知识的灵活运用,培养创新能力。 二. 实验内容及要求 1. 实验内容 所给程序模拟两个进程,即生产者(producer)进程和消费者(Consumer)进程工作。生产者每次产生一个数据,送入缓冲区中。消费者每次从缓冲区中取走一个数据,如图1所示。

图形 1 程序模型

2. 实验要求 (1) 调试、运行给出的程序,从操作系统原理的角度验证程序的正确性。 (2) 发现并修改程序中的原理性错误或不完善的地方。 (3) 鼓励在程序中增加新的功能。完成基本。 (4) 在程序中适当地加入注释。 (5) 认真进行预习,阅读原程序,发现其中的原理性错误,完成预习报告。 (6) 实验完成后,要认真总结,完成实验报告。 三. 程序流程图

1. 窗口载入函数 (Form1_Load):

2. 更新等待队列显示的timer函数 (runningTimer_Tick)

3. 更新等待队列的timer函数(waitingQueueTimer_Tick)

START

Mutex等待队列非空 TRUE

FALSE

Mutex等待队列 显示框置空

FOR

TRUE显示Mutex等待队列 中的元素 FALSE

Full等待队列非空 TRUE

FALSE Full等待队列 显示框置空

FOR TRUE显示Full等待队列 中的元素 FALSE

Empty等待队列非空

FALSE

TRUE

Empty等待队 列显示框置空

FOR TRUE 显示Empty等待队列 中的元素 FALSE

END

注:由于程序较大,故只绘制核心功能的流程图。

四. 程序源代码、文档注释及文字说明 1. 程序语言:c# 2. 源代码: (1)设置界面

using System;

using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms;

namespace 生产者消费者问题 {

public partial class set : Form {

public static int bufferSize=0; public static int producerNum = 0; public static int consumerNum = 0; public set() {

InitializeComponent(); }

private void yesButton_Click(object sender, EventArgs e) { try {

bufferSize = int.Parse(bufferSizeTextbox.Text); producerNum = int.Parse(producerNumTextBox.Text); consumerNum = int.Parse(consumerNumTextBox.Text); } catch {

MessageBox.Show("ERROR:键入错误!"); Application.Exit(); }

if (producerNum + consumerNum <= 15) {

Form1 mainForm = new Form1(); this.Hide();

mainForm.Show(); } else {

MessageBox.Show("ERROR:最大支持的生产者消费者人数和为15!"); set set1 = new set(); set1.Show(); this.Hide(); } } } }

(2)运行界面

using System;

using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms; using System.Collections;

namespace 生产者消费者问题 {

public partial class Form1 : Form {

public static Queue waitingFQueue = new Queue(); public static Queue readyFQueue = new Queue(); public static Queue waitingEQueue = new Queue(); public static Queue readyEQueue = new Queue(); public static Queue waitingMQueue = new Queue(); public static Queue readyMQueue = new Queue(); public static int mutex = 1; public static int full; public static int empty; public static int bufferSize; public static bool[] bufferArray; public static int producerNum; public static int consumerNum; producerProgress[] producerArray; consumerProgress[] consumerArray; public static string resultChoice = "";

public const int progressMax = 15; public Form1() {

InitializeComponent(); }

public class progress {

public string name = "";

public bool runOrready = true;//是否处于运行或就绪 public int step = 1;//运行到第几步 public bool nextStep() {

step++; step=step%7; return true; }

public bool mP(ref int semaphore) {

semaphore--; if (semaphore < 0) {

waitingMQueue.Enqueue(this); runOrready = false; }

return true; }

public bool mV(ref int semaphore) {

semaphore++; if (semaphore <= 0) {

progress temp = (progress)waitingMQueue.Dequeue(); readyMQueue.Enqueue(temp); temp.runOrready = true; }

return true; }

public bool fP(ref int semaphore) {

semaphore--; if (semaphore < 0) {

waitingFQueue.Enqueue(this);

runOrready = false; }

return true; }

public bool fV(ref int semaphore) {

semaphore++; if (semaphore <= 0) {

progress temp = (progress)waitingFQueue.Dequeue(); readyFQueue.Enqueue(temp); temp.runOrready = true; }

return true; }

public bool eP(ref int semaphore) {

semaphore--; if (semaphore < 0) {

waitingEQueue.Enqueue(this); runOrready = false; }

return true; }

public bool eV(ref int semaphore) {

semapho …… 此处隐藏:3449字,全部文档内容请下载后查看。喜欢就下载吧 ……

东北大学操作系统实验2.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/96812.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)