




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025内蒙古应急管理厅选聘应急管理综合行政执法社会监督员的考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025甘肃陇南市成县消防救援大队招聘政府专职消防员12人考前自测高频考点模拟试题及参考答案详解一套
- 2025甘肃酒泉市公安局招聘留置看护岗位警务辅助人员(第二批)30人考前自测高频考点模拟试题及答案详解(必刷)
- 2025黑龙江齐齐哈尔市尚志市招聘警务辅助人员60人模拟试卷及参考答案详解
- 2025辽宁盘锦市卫生健康委所属部分公立医院校园招聘94人考前自测高频考点模拟试题附答案详解
- 2025甘肃甘南州临潭县人民法院招聘司法辅助警察4人考前自测高频考点模拟试题附答案详解(完整版)
- 洗选煤原理课件
- 2025福建漳州市公安局招聘警务辅助人员76人模拟试卷及参考答案详解
- 2025年龙岩市供电服务有限公司招聘59人考前自测高频考点模拟试题及答案详解(名校卷)
- 2025贵州铜仁职业技术学院引进博士研究生15人模拟试卷及参考答案详解1套
- 《淞沪会战》课件
- 国庆节课件下载
- 画家徐悲鸿简介
- 幼儿园中班彩虹泡泡龙课件
- 湖南省长沙市长郡教育集团2024-2025学年上学期七年级数学期中考试试卷(含答案)
- 大量输血课件教学课件
- 1中华传统文化学习通超星期末考试答案章节答案2024年
- 二甲医院评审应知应会知识手册草版
- 水的组成说课-2024-2025学年九年级化学人教版(2024)上册
- 妈妈课堂系列医生讲课文档
- 110kv变电站安全距离110kv变电站设计规范
评论
0/150
提交评论