您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 生产者消费者问题—操作系统课程设计—英文
1NameofProject:Producer-ConsumerProblemCourseName:OperatingSystemCourseNumber:CSCI330Student’sName:Student’sID:DateofSubmission:Dec20,20112TableofContentsRequirementofProject……………………………………………………………………………………….3OperatingEnvironment…………………………………………………………..………….………………3PrincipleofProgram………………………………………………………..………….………………………3Structure……………………………………………………………………………………………………………………3FlowChart…………………………………………………………………………………………………………………4CodeoftheProgram……………………………………………………………………………..……………………5ResultsofSimulation…………………………………………………………………………….…………………….8ConsiderableImprovement…………………………………………………………………………………9Conclusion……………………………………………………………………………..……………………………11References……………………………………………………………………………………………..……………123RequirementofProjectTheproducer-consumerproblemillustratedasthemodelofbounded-buffer,requiresequentialprocessesorthreadsrunningasynchronouslyandpossiblysharingdata.Thereareseveralmethodstosolvethisproblem,andsemaphore,mutex(simplifiedsemaphore)andmonitorarecommonlyusedonthis.Thisprojectrequiresustomakeproducersandconsumers–runningasseparatedthreads,andsemaphoreswithfull,empty,andmutexparameterstosimulatethisprocedure.Inthetextbook,wearesuggestedtousePthreadsorWin32API,however,IdecidedtouseJavatosimulatetheprocedure,becauseJavaisthemostfamiliarlanguagethatwehavelearned.BecauseoftheJavaVirtualMachine(JVM)layer,manystructuresaremadesimpleinJavacomparedtowin32APIandCinUNIXenvironment,andwecansimulatetheproblemwithoutacertainsemaphore.OperatingEnvironmentOS:Windows7,JDK:Java7edition1.7.0_02PrincipleofProgramBecauseofthelimitedJavaprogrammingskills,Icanonlysimulatetheprocedureinthemostsimplifiedway.Onlythreesub-classesareputundertheProducerConsumerClassintheprogram,andonlyonemethodwhichisthenecessarymainvoidmethodinthisClass.Thethreesub-classesarestorage,producerandconsumer,andproducerandconsumerclassesaresetasJavathreads,becausetheyextendlibraryclassofThreadinjava.lang.Objectpackage.Thereareseveralobjectsandmethodsinthesethreesub-classes,andtheyarecarefullydefinedasprivate,publicorsynchronized.Structure:Thefullstructureofwholeprogramisillustratedinthetablebelow.SuperClass:ProducerConsumerWithObject:p1~p7(sevenproducerobjects),c1~c3(threeconsumerobjects)Withmethod:mainSub-class:Godown//storageclassInnervariable:max_size,curnum//thenumberthatthestoragehavecurrentlyInnermethod:produce(int),consume(int)Sub-class:Producer4Innervariable:neednum//numberofproducingInnermethod:run//inheritfromtheThreadClassinjava.lang.ObjectSub-class:ConsumerInnervariable:neednum//numberofconsumingInnermethod:run//inheritfromtheThreadClassinjava.lang.ObjectFlowChart:Theflowchartofthisprogramisillustratedasbelow:Thekeyofrealizationofthisprocedureisthatmakeeachtheproducerandconsumerarealjavathread.BylookinguptheofficialjavadocinOracleWebsites,wecanfinddetailsoftheclassThreadinjava.lang.Objectpackages.Ifindsomemethodsandexamplesofusage,andbythefunctionsandmethodsprovidedbyofficial,IusethemethodofnotifyAll(),wait().Tableofmethodsofjava.lang.Objectinofficialjavadocisshownbelow:MethodSummaryprotectedObjectclone()Createsandreturnsacopyofthisobject.booleanequals(Objectobj)Indicateswhethersomeotherobjectisequaltothisone.protectedvoidfinalize()Calledbythegarbagecollectoronanobjectwhengarbagecollectiondeterminesthat5therearenomorereferencestotheobject.Class?getClass()ReturnstheruntimeclassofthisObject.inthashCode()Returnsahashcodevaluefortheobject.voidnotify()Wakesupasinglethreadthatiswaitingonthisobject'smonitor.voidnotifyAll()Wakesupallthreadsthatarewaitingonthisobject'smonitor.StringtoString()Returnsastringrepresentationoftheobject.voidwait()Causesthecurrentthreadtowaituntilanotherthreadinvokesthenotify()methodorthenotifyAll()methodforthisobject.voidwait(longtimeout)Causesthecurrentthreadtowaituntileitheranotherthreadinvokesthenotify()methodorthenotifyAll()methodforthisobject,oraspecifiedamountoftimehaselapsed.voidwait(longtimeout,intnanos)Causesthecurrentthreadtowaituntilanotherthreadinvokesthenotify()methodorthenotifyAll()methodforthisobject,orsomeotherthreadinterruptsthecurrentthread,oracertainamountofrealtimehaselapsed.Inthisprogram,allthethreadsofproducersandconsumersareparalleled,andhavesamelimitsofauthority.Atthebeginning,eachthreadstartsindividually,andorderofsequenceandthewinnerofcompetitionisrandom.Thesuccessfulrunningofanythread,allthethreadswouldbewakedup,andprocesswouldberestartedoverly.Asaresult,eachtimehasadifferentresultofsimulation.Insomedegree,thisproceduresimulatesthepracticalsituation.CodeoftheProgram:Packageproject_of_os;importjava.lang.Object;publicclassProducerConsumer{publicstaticvoidmain(String[]args){Godowngodown=newGodown(30);//30为初始时库存值Consumerc1=newConsumer(50,godown);6Consumerc2=newConsumer(20,godown);Consumerc3=newConsumer(30,godown);Producerp1=newProducer(10,godown);Producerp2=newProducer(10,godown);Producerp3=newProducer(10,godown);Producerp4=newProducer(10,godown);Producerp5=newProducer(10,godown);Producerp6=newProducer(10,godown);Producerp7=newProducer(80,godown);//各个线程并发运行,先后次序为随机,不能运行则休眠,每个线程结束后,会唤醒所有休眠的进程,重新并发运行c
本文标题:生产者消费者问题—操作系统课程设计—英文
链接地址:https://www.777doc.com/doc-1624298 .html