欢迎来到天天文库
浏览记录
ID:48219133
大小:161.80 KB
页数:7页
时间:2020-01-23
《操作系统大作业.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、操作系统大作业学号:091401223姓名:高玉林本次上机作业使用的软件是MicrosoftVisualStudioCommunity2017RC,版本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变量控制函
2、数的执行顺序:f3()函数应在f1()和f2()之后运行,f()函数应在f3()之后运行。代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespacehomework6{classfunction{staticMutexm1;staticMutexm2;doublex;doublef1_result;doublef2_result
3、;doublef3_result;doublefinal_result;staticAutoResetEventevent1=newAutoResetEvent(false);staticAutoResetEventevent2=newAutoResetEvent(false);staticAutoResetEventevent3=newAutoResetEvent(false);staticAutoResetEventevent4=newAutoResetEvent(false);function(doublex){this.x=x;}stat
4、icvoidMain(string[]args){Console.WriteLine("输入一个数");doublex=double.Parse(Console.ReadLine());functionfunction=newfunction(x);m1=newMutex(true);m2=newMutex(true);AutoResetEvent[]a=newAutoResetEvent[4];a[0]=event1;a[1]=event2;a[2]=event3;a[3]=event4;Threadp=newThread(newThreadS
5、tart(function.f));Threadp1=newThread(newThreadStart(function.f1));Threadp2=newThread(newThreadStart(function.f2));Threadp3=newThread(newThreadStart(function.f3));p.Start();p3.Start();p1.Start();p2.Start();Thread.Sleep(1000);m1.ReleaseMutex();Thread.Sleep(1000);m2.ReleaseMutex
6、();WaitHandle.WaitAll(a);Console.ReadLine();}publicvoidf1(){Console.WriteLine("functionf1()start.");f1_result=10*x;Console.WriteLine("f1()的结果是:"+f1_result);Console.WriteLine("functionf1()isfinished.");event1.Set();Console.WriteLine();}publicvoidf2(){Console.WriteLine("functio
7、nf2()start.");f2_result=100*x;Console.WriteLine("f2()的结果是:"+f2_result);Console.WriteLine("functionf2()isfinished.");event2.Set();Console.WriteLine();}publicvoidf3(){Console.WriteLine("functionf3()start.");m1.WaitOne();f3_result=f2_result*f1_result;Console.WriteLine("f3()的结果是:
8、"+f3_result);Console.WriteLine("functionf3()isfinished.");event3.Set
此文档下载收益归作者所有