




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
操作系统大作业学号:091401223 姓名:高玉林本次上机作业使用的软件是Microsoft Visual Studio Community 2017 RC,版本 15.0.26020.0,使用的语言是C#。第一题:编写求f(x)值的程序f(x)=f1(x)+f2(x)+f3(x)f1(x)=10*xf2(x)=100*xf3(x)=f1(x)*f2(x)要求给出f(x)、f1(x)、f2(x)、f3(x)四个并发函数并完成计算。思路:应用Mutex类、AutoResetEvent类、Waithandle类,主函数中创建四个线程,使用两个Mutex变量控制函数的执行顺序:f3()函数应在f1()和f2()之后运行,f()函数应在f3()之后运行。代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace homework6 class function static Mutex m1; static Mutex m2; double x; double f1_result; double f2_result; double f3_result; double final_result; static AutoResetEvent event1 = new AutoResetEvent(false); static AutoResetEvent event2 = new AutoResetEvent(false); static AutoResetEvent event3 = new AutoResetEvent(false); static AutoResetEvent event4 = new AutoResetEvent(false); function(double x) this.x = x; static void Main(string args) Console.WriteLine(输入一个数); double x = double.Parse(Console.ReadLine(); function function = new function(x); m1 = new Mutex(true); m2 = new Mutex(true); AutoResetEvent a = new AutoResetEvent4; a0 = event1; a1 = event2; a2 = event3; a3 = event4; Thread p = new Thread(new ThreadStart(function.f); Thread p1 = new Thread(new ThreadStart(function.f1); Thread p2 = new Thread(new ThreadStart(function.f2); Thread p3 = new Thread(new ThreadStart(function.f3); p.Start(); p3.Start(); p1.Start(); p2.Start(); Thread.Sleep(1000); m1.ReleaseMutex(); Thread.Sleep(1000); m2.ReleaseMutex(); WaitHandle.WaitAll(a); Console.ReadLine(); public void f1() Console.WriteLine(function f1() start.); f1_result = 10 * x; Console.WriteLine(f1()的结果是: + f1_result); Console.WriteLine(function f1() is finished.); event1.Set(); Console.WriteLine(); public void f2() Console.WriteLine(function f2() start.); f2_result = 100 * x; Console.WriteLine(f2()的结果是: + f2_result); Console.WriteLine(function f2() is finished.); event2.Set(); Console.WriteLine(); public void f3() Console.WriteLine(function f3() start.); m1.WaitOne(); f3_result = f2_result * f1_result; Console.WriteLine(f3()的结果是: + f3_result); Console.WriteLine(function f3() is finished.); event3.Set(); m1.ReleaseMutex(); Console.WriteLine(); public void f() Console.WriteLine(function F() start.); Mutex a = new Mutex2; a0 = m1; a1 = m2; Mutex.WaitAll(a); final_result = f1_result * f2_result * f3_result; Console.WriteLine(F()的结果是: + final_result); Console.WriteLine(function F() is finished。); event4.Set(); Console.WriteLine(); 运行结果:第二题:设程序中有一整形公共变量x,有一写线程向x连续写入1,2,10(用循环)。有读线程将写入变量x的值依次读出(用循环),试完成程序。思路:使用lock关键字、Monito类,构造两个void函数,分别对公共变量进行读与写,在主函数中创建读线程与写线程,交替执行。代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace homework3 class ProducerAndConsumer int x; Boolean readFlag = false;/true表示可以读取 false表示不可以读取可以写入 int getX() return x; void setX(int x) this.x = x; void write(int x) lock (this) if (readFlag) try Monitor.Wait(this); catch(Exception e) Console.Write(e); setX(x); Console.WriteLine(Producer: + this.x); readFlag = true; Monitor.Pulse(this); void threadProduce() for(int i = 1; i = 10; i+) write(i); void read() lock (this) if (!readFlag) try Monitor.Wait(this); catch(Exception e) Console.Write(e); int temp =getX(); readFlag = false; Console.WriteLine(Consumer:0, temp); Monitor.Pulse(this); void threadConsumer() for(int i = 1; i = 10; i+) read(); static void Main(string args) ProducerAndConsumer pc = new ProducerAndConsumer(); Thread producer = new Thread(new Thread
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大连汽车职业技术学院《企业开发实训》2024-2025学年第一学期期末试卷
- 淄博师范高等专科学校《文献检索》2024-2025学年第一学期期末试卷
- 武昌职业学院《原画临基》2024-2025学年第一学期期末试卷
- 长春金融高等专科学校《原理与应用实训》2024-2025学年第一学期期末试卷
- 江西财经职业学院《中国经济前沿(英语)》2024-2025学年第一学期期末试卷
- 安徽电子信息职业技术学院《运筹学(全英)》2024-2025学年第一学期期末试卷
- 哈尔滨幼儿师范高等专科学校《标识系统设计》2024-2025学年第一学期期末试卷
- 南京航空航天大学《家蚕遗传育种学》2024-2025学年第一学期期末试卷
- 2024年幼儿园应急疏散演练计划
- 珠海城市职业技术学院《信息计量中俄》2024-2025学年第一学期期末试卷
- 电脑购销合同模板(六篇)
- 初中英语语法句型转换综合训练100题(含参考答案)
- 选题策划导论PPT完整全套教学课件
- 奥氏体不锈钢对接接头超声检测方法和质量分级
- 新航标职业英语语音技能教程unit
- 普通高中语文课程标准测试题及答案
- 科目二考试成绩单
- 正确认识胰岛素
- 微电网的总体结构
- 辽宁省盘锦市各县区乡镇行政村村庄村名居民村民委员会明细
- DL∕T 617-2019 气体绝缘金属封闭开关设备技术条件
评论
0/150
提交评论