过桥问题课设报告.doc

上传人:苏美尔 文档编号:6361202 上传时间:2020-11-01 格式:DOC 页数:17 大小:365.01KB
返回 下载 相关 举报
过桥问题课设报告.doc_第1页
第1页 / 共17页
过桥问题课设报告.doc_第2页
第2页 / 共17页
过桥问题课设报告.doc_第3页
第3页 / 共17页
过桥问题课设报告.doc_第4页
第4页 / 共17页
过桥问题课设报告.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《过桥问题课设报告.doc》由会员分享,可在线阅读,更多相关《过桥问题课设报告.doc(17页珍藏版)》请在三一文库上搜索。

1、 操作系统 课程设计报告书题 目 过桥问题 系 别 计算机工程系 专 业 计算机科学与技术 班 级 姓 名 指导教师 下达日期 2013 年 6 月 15 日设计时间自 2013 年 6 月 17 日 至 2013 年6月 30 日第一章 课程设计目的 该课程是在学完操作系统课程之后,让学生在掌握基本原理和方法的基础上,运用熟悉的开发工具,在计算机上模拟实现操作系统课程中介绍的操作和算法。以帮助同学切身体验各种操作的过程及效果,同时加深同学对基本原理的理解,提高同学的编程能力、综合应用知识的能力和分析解决问题的能力,初步培养研制简单系统程序开发的能力。第二章 课程设计内容与要求掌握并发进程中同

2、步与互斥的机制,基于线程的编程技术开发类似生产者消费者问题的演示程序。模拟程序的开发不但要正确体现原算法的基本原理,还要精心设计,充分体现学生自身在演示过程设计中的独特性,使模拟程序演示过程清晰、准确,演示的效果直观、图文并茂,达到能够帮助学生更深入的理解算法的目的。第三章 过桥Pv操作过桥的规则:同一方向的可连续过桥,某方向有人过桥时另一方向的人要等待。/mutex左右两边过桥的互斥信号量;Lcount, Rcount左右两边过桥人数/Lmutex, Rmutex 分别表示对Lcount, Rcount互斥访问的信号量Var Semaphore mutex,Lmutex,Rmutex=1,0

3、,0; int Lcount=0, Rcount=0;Main() cobegin Left(); Right(); Coend Left() while (true) P(Lmutex); if Lcount=0 P(mutex); Lcount=Lcount+1; V(Lmutex) 过桥; p(Lmutex); if Lcount=0 V(mutex); Lcount=Lcount-1; V(Lmutex) Right() while (true) P(Rmutex); if Rcount=0 P(mutex); Rcount=Rcount+1; V(Rmutex) 过桥; p(Rmut

4、ex); if Rcount=0 V(mutex); Rcount=Rcount-1; V(Rmutex) 第四章 源代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using System.Collections;namespace Cross

5、Bridge public partial class Form1 : Form /int bridge = 0;/0:空闲 int leftCount = 0; int rightCount = 0; int time = 3000; IList list = new ArrayList(); private delegate void SetTextBoxCallBack(string str); private delegate void SetTBInBridge(int value); SetTextBoxCallBack setTB; SetTBInBridge setBridge

6、; public Form1() InitializeComponent(); setTB = new SetTextBoxCallBack(SetTextBox); setBridge = new SetTBInBridge(SetTextBoxInBridge); void SetTextBox(string str) textBox1.AppendText(str + System.Environment.NewLine); void SetTextBoxInBridge(int value) textBox5.Text = value.ToString(); private void

7、btleft_Click(object sender, EventArgs e) Thread thread1 = new Thread(new ThreadStart(LeftHonkey); thread1.Name = System.DateTime.Now.ToString(); /list.Add(thread1); thread1.Start(); private void btright_Click(object sender, EventArgs e) Thread thread1 = new Thread(new ThreadStart(RightHonkey); /list

8、.Add(thread1); thread1.Name = System.DateTime.Now.ToString(); thread1.Start(); public void LeftHonkey() lock (this) while (rightCount 0) try textBox5.Invoke(setBridge, rightCount + leftCount); textBox1.Invoke(setTB, Thread.CurrentThread.ManagedThreadId + 左猴子阻塞); Monitor.Wait(this); catch (System.Exc

9、eption ex) leftCount+; textBox5.Invoke(setBridge, rightCount + leftCount); textBox1.Invoke(setTB, Thread.CurrentThread.Name + 左猴子上桥); Monitor.PulseAll(this); Thread.Sleep(int.Parse(textBox2.Text.Trim() * 1000); lock(this) leftCount-; textBox5.Invoke(setBridge, rightCount + leftCount); textBox1.Invok

10、e(setTB, Thread.CurrentThread.Name + 左猴子通过); Monitor.PulseAll(this); public void RightHonkey() lock (this) while (leftCount 0) try textBox5.Invoke(setBridge, rightCount+leftCount); textBox1.Invoke(setTB, Thread.CurrentThread.Name + 右猴子阻塞); Monitor.Wait(this); catch (System.Exception ex) rightCount+;

11、 textBox5.Invoke(setBridge, rightCount + leftCount); textBox1.Invoke(setTB, Thread.CurrentThread.Name + 右猴子上桥); Monitor.PulseAll(this); Thread.Sleep(int.Parse(textBox2.Text.Trim() * 1000); lock(this) rightCount-; textBox5.Invoke(setBridge, rightCount + leftCount); textBox1.Invoke(setTB, Thread.Curre

12、ntThread.Name + 右猴子通过); Monitor.PulseAll(this); private void button1_Click(object sender, EventArgs e) namespace CrossBridge partial class Form1 / / 必需的设计器变量。 / private System.ComponentModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispos

13、e(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private void InitializeComponent() this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Labe

14、l(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBox4 = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Labe

15、l(); this.label5 = new System.Windows.Forms.Label(); this.btleft = new System.Windows.Forms.Button(); this.btright = new System.Windows.Forms.Button(); this.textBox5 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); / / textBox1 / this.text

16、Box1.Location = new System.Drawing.Point(13, 58); this.textBox1.Multiline = true; this.textBox1.Name = textBox1; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(208, 206); this.textBox1.TabIndex = 0; / / label1 / this.label1.AutoSize

17、= true; this.label1.Location = new System.Drawing.Point(228, 68); this.label1.Name = label1; this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 1; this.label1.Text = 过桥时间:; / / textBox2 / this.textBox2.Location = new System.Drawing.Point(286, 62); this.textBox2.Name = textBox

18、2; this.textBox2.Size = new System.Drawing.Size(67, 21); this.textBox2.TabIndex = 2; this.textBox2.Text = 3; / / label2 / this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(360, 66); this.label2.Name = label2; this.label2.Size = new System.Drawing.Size(17, 12); this.label2.

19、TabIndex = 3; this.label2.Text = 秒; / / textBox3 / this.textBox3.Location = new System.Drawing.Point(288, 95); this.textBox3.Name = textBox3; this.textBox3.Size = new System.Drawing.Size(67, 21); this.textBox3.TabIndex = 5; / / label3 / this.label3.AutoSize = true; this.label3.Location = new System.

20、Drawing.Point(230, 101); this.label3.Name = label3; this.label3.Size = new System.Drawing.Size(65, 12); this.label3.TabIndex = 4; this.label3.Text = 左边猴子:; / / textBox4 / this.textBox4.Location = new System.Drawing.Point(289, 130); this.textBox4.Name = textBox4; this.textBox4.Size = new System.Drawi

21、ng.Size(67, 21); this.textBox4.TabIndex = 7; / / label4 / this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(231, 136); this.label4.Name = label4; this.label4.Size = new System.Drawing.Size(65, 12); this.label4.TabIndex = 6; this.label4.Text = 右边猴子:; / / label5 / this.label

22、5.AutoSize = true; this.label5.Location = new System.Drawing.Point(231, 175); this.label5.Name = label5; this.label5.Size = new System.Drawing.Size(65, 12); this.label5.TabIndex = 8; this.label5.Text = 桥上猴子:; / / btleft / this.btleft.Location = new System.Drawing.Point(259, 210); this.btleft.Name =

23、btleft; this.btleft.Size = new System.Drawing.Size(75, 23); this.btleft.TabIndex = 9; this.btleft.Text = 左猴子; this.btleft.UseVisualStyleBackColor = true; this.btleft.Click += new System.EventHandler(this.btleft_Click); / / btright / this.btright.Location = new System.Drawing.Point(259, 241); this.bt

24、right.Name = btright; this.btright.Size = new System.Drawing.Size(75, 23); this.btright.TabIndex = 10; this.btright.Text = 右猴子; this.btright.UseVisualStyleBackColor = true; this.btright.Click += new System.EventHandler(this.btright_Click); / / textBox5 / this.textBox5.Location = new System.Drawing.P

25、oint(289, 166); this.textBox5.Name = textBox5; this.textBox5.Size = new System.Drawing.Size(67, 21); this.textBox5.TabIndex = 11; this.textBox5.Text = 0; / / button1 / this.button1.Location = new System.Drawing.Point(259, 289); this.button1.Name = button1; this.button1.Size = new System.Drawing.Size

26、(75, 23); this.button1.TabIndex = 12; this.button1.Text = 暂停; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleM

27、ode.Font; this.ClientSize = new System.Drawing.Size(398, 333); this.Controls.Add(this.button1); this.Controls.Add(this.textBox5); this.Controls.Add(this.btright); this.Controls.Add(this.btleft); this.Controls.Add(this.label5); this.Controls.Add(this.textBox4); this.Controls.Add(this.label4); this.Co

28、ntrols.Add(this.textBox3); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.textBox2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Name = Form1; this.Text = Form1; this.ResumeLayout(false); this.PerformLayout(); #endregion private Syst

29、em.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textB

30、ox4; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button btleft; private System.Windows.Forms.Button btright; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.Button button1; using System;using System.Co

31、llections.Generic;using System.Linq;using System.Windows.Forms;namespace CrossBridge static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); 第五章 程序运行结果 总 结一个星期的课程设计就像一场无烟的战争,只有不断地查找资料,不断思考,尝试代码,才会成功。这次课程设计让我收获颇多。是我对操作系统的基本知识有了进一步的提高,并在实践中对各种概念有了进一步的深化。对Pv操作和多线程有了更深的理解。收获:1. 程序设计的思想的精巧的重要性,好的设计可以让大家很快的明白你的思想,而且很方便实现它。2. 与别人的交流很重要,别人的说明或思想都可能会给你很大启发。3. 良好的编程习惯,尽可能多的写注释。它可以使你的程序很方便的被别人阅读,也方便被修改。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1