生产者消费者问题操作系统课程设计英文Word文档下载推荐.docx
《生产者消费者问题操作系统课程设计英文Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《生产者消费者问题操作系统课程设计英文Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
Student’sID:
DateofSubmission:
Dec20,2011
TableofContents
RequirementofProject……………………………………………………………………………………….3
OperatingEnvironment…………………………………………………………..………….………………3
PrincipleofProgram………………………………………………………..………….………………………3
Structure……………………………………………………………………………………………………………………3
FlowChart…………………………………………………………………………………………………………………4
CodeoftheProgram……………………………………………………………………………..……………………5
ResultsofSimulation…………………………………………………………………………….…………………….8
ConsiderableImprovement…………………………………………………………………………………9
Conclusion……………………………………………………………………………..……………………………11
References……………………………………………………………………………………………..……………12
RequirementofProject
Theproducer-consumerproblemillustratedasthemodelofbounded-buffer,requiresequentialprocessesorthreadsrunningasynchronouslyandpossiblysharingdata.Thereareseveralmethodstosolvethisproblem,andsemaphore,mutex(simplifiedsemaphore)andmonitorarecommonlyusedonthis.Thisprojectrequiresustomakeproducersandconsumers–runningasseparatedthreads,andsemaphoreswithfull,empty,andmutexparameterstosimulatethisprocedure.
Inthetextbook,wearesuggestedtousePthreadsorWin32API,however,IdecidedtouseJavatosimulatetheprocedure,becauseJavaisthemostfamiliarlanguagethatwehavelearned.
BecauseoftheJavaVirtualMachine(JVM)layer,manystructuresaremadesimpleinJavacomparedtowin32APIandCinUNIXenvironment,andwecansimulatetheproblemwithoutacertainsemaphore.
OperatingEnvironment
OS:
Windows7,
JDK:
Java7edition1.7.0_02
PrincipleofProgram
BecauseofthelimitedJavaprogrammingskills,Icanonlysimulatetheprocedureinthemostsimplifiedway.Onlythreesub-classesareputundertheProducerConsumerClassintheprogram,andonlyonemethodwhichisthenecessarymainvoidmethodinthisClass.
Thethreesub-classesarestorage,producerandconsumer,andproducerandconsumerclassesaresetasJavathreads,becausetheyextendlibraryclassofThreadinjava.lang.Objectpackage.
Thereareseveralobjectsandmethodsinthesethreesub-classes,andtheyarecarefullydefinedasprivate,publicorsynchronized.
Structure:
Thefullstructureofwholeprogramisillustratedinthetablebelow.
SuperClass:
ProducerConsumer
WithObject:
p1~p7(sevenproducerobjects),c1~c3(threeconsumerobjects)
Withmethod:
main
Sub-class:
Godown//storageclass
Innervariable:
max_size,curnum//thenumberthatthestoragehavecurrently
Innermethod:
produce(int),consume(int)
Producer
Innervariable:
neednum//numberofproducing
run//inheritfromtheThreadClassinjava.lang.Object
Consumer
neednum//numberofconsuming
FlowChart:
Theflowchartofthisprogramisillustratedasbelow:
Thekeyofrealizationofthisprocedureisthatmakeeachtheproducerandconsumerarealjavathread.BylookinguptheofficialjavadocinOracleWebsites,wecanfinddetailsoftheclassThreadinjava.lang.Objectpackages.Ifindsomemethodsandexamplesofusage,andbythefunctionsandmethodsprovidedbyofficial,IusethemethodofnotifyAll(),wait().
Tableofmethodsofjava.lang.Objectinofficialjavadocisshownbelow:
MethodSummary
protected
Object
clone()
Createsandreturnsacopyofthisobject.
boolean
equals(Object
obj)
Indicateswhethersomeotherobjectis"
equalto"
thisone.
void
finalize()
Calledbythegarbagecollectoronanobjectwhengarbagecollectiondeterminesthattherearenomorereferencestotheobject.
Class<
?
>
getClass()
Returnstheruntimeclassofthis
Object.
int
hashCode()
Returnsahashcodevaluefortheobject.
notify()
Wakesupasinglethreadthatiswaitingonthisobject'
smonitor.
notifyAll()
Wakesupallthreadsthatarewaitingonthisobject'
String
toString()
Returnsastringrepresentationoftheobject.
wait()
Causesthecurrentthreadtowaituntilanotherthreadinvokesthe
methodorthe
methodforthisobject.
wait(long
timeout)
Causesthecurrentthreadtowaituntileitheranotherthreadinvokesthe
methodforthisobject,oraspecifiedamountoftimehaselapsed.
timeout,int
nanos)
methodforthisobject,orsomeotherthreadinterruptsthecurrentthread,oracertainamountofrealtimeh