操作系统大作业

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

操作系统大作业学号: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变量控制函数的执行顺序: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;doublef3_result;doublefinal_result;staticAutoResetEventevent1=newAutoResetEvent(false);staticAutoResetEventevent2=newAutoResetEvent(false);staticAutoResetEventevent3=newAutoResetEvent(false);staticAutoResetEventevent4=newAutoResetEvent(false);function(doublex){this.x=x;}staticvoidMain(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(newThreadStart(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();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(functionf2()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()的结果是:+f3_result);Console.WriteLine(functionf3()isfinished.);event3.Set();m1.ReleaseMutex();Console.WriteLine();}publicvoidf(){Console.WriteLine(functionF()start.);Mutex[]a=newMutex[2];a[0]=m1;a[1]=m2;Mutex.WaitAll(a);final_result=f1_result*f2_result*f3_result;Console.WriteLine(F()的结果是:+final_result);Console.WriteLine(functionF()isfinished。);event4.Set();Console.WriteLine();}}}运行结果:第二题:设程序中有一整形公共变量x,有一写线程向x连续写入1,2,…10(用循环)。有读线程将写入变量x的值依次读出(用循环),试完成程序。思路:使用lock关键字、Monito类,构造两个void函数,分别对公共变量进行读与写,在主函数中创建读线程与写线程,交替执行。代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespacehomework3{classProducerAndConsumer{intx;BooleanreadFlag=false;//true表示可以读取false表示不可以读取可以写入intgetX(){returnx;}voidsetX(intx){this.x=x;}voidwrite(intx){lock(this){if(readFlag){try{Monitor.Wait(this);}catch(Exceptione){Console.Write(e);}}setX(x);Console.WriteLine(Producer:+this.x);readFlag=true;Monitor.Pulse(this);}}voidthreadProduce(){for(inti=1;i=10;i++){write(i);}}voidread(){lock(this){if(!readFlag){try{Monitor.Wait(this);}catch(Exceptione){Console.Write(e);}}inttemp=getX();readFlag=false;Console.WriteLine(Consumer:{0},temp);Monitor.Pulse(this);}}voidthreadConsumer(){for(inti=1;i=10;i++){read();}}staticvoidMain(string[]args){ProducerAndConsumerpc=newProducerAndConsumer();Threadproducer=newThread(newThreadStart(pc.threadProduce));Threadconsumer=newThread(newThreadStart(pc.threadConsumer));producer.Start();consumer.Start();producer.Join();consumer.Join();Console.ReadLine();}}}运行结果:

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功