生产者消费者问题—操作系统课程设计—英文13539.docx

上传人:you****now 文档编号:48062883 上传时间:2022-10-05 格式:DOCX 页数:17 大小:168.79KB
返回 下载 相关 举报
生产者消费者问题—操作系统课程设计—英文13539.docx_第1页
第1页 / 共17页
生产者消费者问题—操作系统课程设计—英文13539.docx_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《生产者消费者问题—操作系统课程设计—英文13539.docx》由会员分享,可在线阅读,更多相关《生产者消费者问题—操作系统课程设计—英文13539.docx(17页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Namee off Prrojeect:Prodduceer-CConssumeer PProbblemmCourrse Namme: Opeerattingg SyysteemCourrse Nummberr:CSSCI 3300Studdentts NNamee:Studdentts IID:Datee off Suubmiissiion: Deec 220, 20111Tablle oof CConttenttsRequuireemennt oof PProjjectt.3Operratiing Envviroonmeent.3Prinncipple of Proograam.3Stru

2、uctuure3Floww Chhartt4Codee off thhe PProggramm.5Resuultss off Siimullatiion.8Conssideerabble Impprovvemeent 9Concclussionn.11Refeerenncess.12Requuireemennt oof PProjjecttThe prooduccer-connsummer proobleem iilluustrrateed aas tthe moddel of bouundeed-bbufffer, reequiire seqquenntiaal pproccessses o

3、r thrreadds rrunnningg assyncchroonouuslyy annd pposssiblly ssharringg daata. Thheree arre sseveerall meethoods to sollve thiis pprobblemm, aand semmaphhoree, mmuteex(ssimppliffiedd seemapphorre) andd moonittor aree coommoonlyy ussed on thiis. Thiis pprojjectt reequiiress uss too maake prooduccerss

4、annd cconssumeers runnninng aas ssepaaratted thrreadds, andd seemapphorres witth ffulll, eemptty, andd muutexx paarammeteers to simmulaate thiis pprocceduure.In tthe texxtboook, wee arre ssugggestted to usee Ptthreeadss orr Wiin322 APPI, howweveer, I ddeciidedd too usse JJavaa too siimullatee thhe p

5、procceduure, beecauuse Javva iis tthe mosst ffamiiliaar llangguagge tthatt wee haave leaarneed.Becaausee off thhe JJavaa Viirtuual Macchinne (JVMM) llayeer, manny sstruuctuuress arre mmadee siimplle iin JJavaa coompaaredd too wiin322 APPI aand C iin UUNIXX ennvirronmmentt, aand we cann siimullatee t

6、hhe pprobblemm wiithoout a ccerttainn seemapphorre.Operratiing EnvviroonmeentOS: Winndowws 77, JDK: Jaava 7 eedittionn 1.7.00_022Prinncipple of ProograamBecaausee off thhe llimiitedd Jaava proograammiing skiillss, II caan oonlyy siimullatee thhe pprocceduure in thee moost simmpliifieed wway. Onnly t

7、hrree subb-cllassses aree puut uundeer tthe ProoduccerCConssumeer CClasss iin tthe proograam, andd onnly onee meethood wwhicch iis tthe neccesssaryy maain voiid mmethhod in thiis CClasss.The thrree subb-cllassses aree sttoraage, prroduucerr annd cconssumeer, andd prroduucerr annd cconssumeer cclasss

8、ess arre sset as Javva tthreeadss, bbecaausee thhey exttendd liibraary claass of Thrreadd inn jaava.lanng.OObjeect pacckagge.Therre aare sevveraal oobjeectss annd mmethhodss inn thhesee thhreee suub-cclasssess, aand theey aare carrefuullyy deefinned as priivatte, pubblicc orr syynchhronnizeed. Struu

9、ctuure:The fulll sstruuctuure of whoole proograam iis iilluustrrateed iin tthe tabble bellow.Supeer CClasss: ProoduccerCConssumeer Withh Obbjecct: p1p7 (seevenn prroduucerr obbjeccts), cc1cc3 (thrree connsummer objjectts)Withh meethood: maiinSub-claass: Goodowwn / sstorragee cllasssInneer vvariiabll

10、e: maxx_siize, cuurnuum / tthe nummberr thhat thee sttoraage havve ccurrrenttlyInneer mmethhod: prroduuce(intt), connsumme(iint)Sub-claass: PrroduucerrInneer vvariiablle: neeednuum / nnumbber of prooduccinggInneer mmethhod: ruun / iinheeritt frrom thee Thhreaad CClasss iin jjavaa.laang.ObjjecttSub-c

11、laass: CoonsuumerrInneer vvariiablle: neeednuum / nnumbber of connsumminggInneer mmethhod: ruun / iinheeritt frrom thee Thhreaad CClasss iin jjavaa.laang.ObjjecttFloww Chhartt:The floow ccharrt oof tthiss prrogrram is illlusttratted as bellow:The keyy off reealiizattionn off thhis proocedduree iss t

12、hhat makke eeachh thhe pprodduceer aand connsummer a rreall jaava thrreadd. BBy llookkingg upp thhe ooffiiciaal jjavaa dooc iin OOraccle Webbsittes, wee caan ffindd deetaiils of thee cllasss Thhreaad iin jjavaa.laang.Objjectt paackaagess. II fiind somme mmethhodss annd eexammplees oof uusagge, andd

13、byy thhe ffuncctioons andd meethoods proovidded by offficiial, I usee thhe mmethhod of nottifyyAlll(), waait().Tablle oof mmethhodss off jaava.lanng.OObjeect in offficiial javva ddoc is shoown bellow:Methhod Summmarryprottectted Obbjecctclonne()CCreaatess annd rretuurnss a coppy oof tthiss obbjecct.

14、boooleaanequaals(Objjecttobbj)Inndiccatees wwhettherr soome othher objjectt iss eequaal tto thiis oone.prottectted vooidfinaalizze()CCallled by thee gaarbaage colllecctorr onn ann obbjecct wwhenn gaarbaage colllecctioon ddeteermiiness thhat theere aree noo moore reffereencees tto tthe objjectt.Claas

15、sgetCClasss()RRetuurnss thhe rrunttimee cllasss off thhisObjjectt.intthashhCodde()RRetuurnss a hassh ccodee vaaluee foor tthe objjectt.voiidnotiify()Wakkes up a ssinggle thrreadd thhat is waiitinng oon tthiss obbjecctss moonittor.voiidnotiifyAAll()Wakkes up alll thhreaads thaat aare waiitinng oon tt

16、hiss obbjecctss moonittor.StrringgtoSttrinng()RRetuurnss a strringg reepreesenntattionn off thhe oobjeect.voiidwaitt()Caausees tthe currrennt tthreead to waiit uuntiil aanottherr thhreaad iinvookess thhennotiify()mmethhod or theenootiffyAlll()meethood ffor thiis oobjeect.voiidwaitt(loongtimmeouut)Ca

17、ausees tthe currrennt tthreead to waiit uuntiil eeithher anootheer tthreead invvokees tthenottifyy()metthodd orr thhennotiifyAAll()mmethhod forr thhis objjectt, oor aa sppeciifieed aamouunt of timme hhas elaapseed.voiidwaitt(loongtimmeouut, inttnaanoss)Cauusess thhe ccurrrentt thhreaad tto wwaitt un

18、ntill annothher thrreadd innvokkes theenootiffy()meethood oor tthenottifyyAlll()metthodd foor tthiss obbjecct, or somme ootheer tthreead intterrruptts tthe currrennt tthreead, orr a cerrtaiin aamouunt of reaal ttimee haas eelappsedd.In tthiss prrogrram, alll tthe thrreadds oof pprodduceers andd coon

19、suumerrs aare parrallleleed, andd haave samme llimiits of autthorrityy. AAt tthe begginnningg, eeachh thhreaad sstarrts inddiviiduaallyy, aand ordder of seqquennce andd thhe wwinnner of commpettitiion is ranndomm.The succcesssfuul rrunnningg off anny tthreead, alll tthe thrreadds wwoulld bbe wwakeed

20、 uup, andd prroceess wouuld be resstarrtedd ovverlly.AAs aa reesullt, eacch ttimee haas aa diiffeerennt rresuult of simmulaatioon.IIn ssomee deegreee, thiis pprocceduure simmulaatess thhe ppraccticcal sittuattionn.Codee off thhe PProggramm:Packkagee prrojeect_of_os;impoortjjavaa.laang.Objjectt;publl

21、iccclasssPrroduucerrConnsummerpubllicsstatticvvoidd maain(Strringg arggs) Goodowwn ggodoown = nnew Goddownn(300); /300为初始始时库存存值Coonsuumerr c11 = neww Coonsuumerr(500, ggodoown); Coonsuumerr c22 = neww Coonsuumerr(200, ggodoown);Coonsuumerr c33 = neww Coonsuumerr(300, ggodoown);Prroduucerr p11 = neww

22、 Prroduucerr(100, ggodoown);Prroduucerr p22 = neww Prroduucerr(100, ggodoown);Prroduucerr p33 = neww Prroduucerr(100, ggodoown);Prroduucerr p44 = neww Prroduucerr(100, ggodoown);Prroduucerr p55 = neww Prroduucerr(100, ggodoown);Prroduucerr p66 = neww Prroduucerr(100, ggodoown);Prroduucerr p77 = neww

23、 Prroduucerr(800, ggodoown);/各个个线程并并发运行行,先后后次序为为随机,不不能运行行则休眠眠,每个个线程结结束后,会会唤醒所所有休眠眠的进程程,重新新并发运运行c11.sttartt(); c22.sttartt();c33.sttartt();p11.sttartt();p22.sttartt();p33.sttartt();p44.sttartt();p55.sttartt();p66.sttartt();p77.sttartt();/* 仓库类类*/classs GGodoown publlicsstatticffinaalinntmaax_ssizee =

24、 1000; / 1100为为设置的的最大库库存量publliciintccurnnum; Goddownn() Goddownn(innt ccurnnum) thiss.currnumm = currnumm;/ 生生产neeednnum数数量的产产品,无无论是否否生产都都输出情情况说明明publlicssyncchroonizzedvvoidd prroduuce(intt neeednnum) / 测测试是否否能生产产whille (neeednuum + cuurnuum maax_ssizee) SSysttem.outt.prrinttln(要生生产的产产品数量量 + neeed

25、nnum + 超过剩剩余库存存量+ (maxx_siize - ccurnnum) + ,暂暂时不能能执行生生产任务务!);try/ 当当前的生生产线程程等待waiit(); caatchh (IInteerruupteedExxcepptioon ee) e.pprinntSttackkTraace();curnnum += neeednuum;/ 满满足生产产条件,则则进行生生产Syysteem.oout.priintlln(已经生生产了 + neeednuum + 个个产品,现现仓储量量为 + ccurnnum);/ 唤唤醒在此此对象mmoniitorr上等待待的所有有线程nootiff

26、yAlll();/消费费neeednuum数量量的产品品,只有有消费成成功才输输出状态态,库存存不足不不显示信信息,线线程休眠眠publlicssyncchroonizzedvvoidd coonsuume(intt neeednnum) / 测测试是否否能消费费whille (currnumm neeednuum)trywaiit();/ 当前前的生产产线程等等待 caatchh (IInteerruupteedExxcepptioon ee) e.prrinttStaackTTracce();/ 满满足消费费条件,则则进行消消费curnnum -= neeednuum;Syysteem.o

27、out.priintlln(已经消消费了 + neeednuum + 个个产品,现现仓储量量为 + ccurnnum);/ 唤唤醒在此此对象mmoniitorr上等待待的所有有线程nootiffyAlll();/* 生产者者类,线线程*/classs PProdduceer eexteendss Thhreaad privvateeinttneeednuum; / 生生产产品品的数量量privvatee Goodowwn ggodoown; / 仓库库Prooduccer(intt neeednnum, Goodowwn ggodoown) thiss.neeednuum = neeednnu

28、m;thiss.goddownn = goddownn;publlicvvoidd ruun() godoown.prooducce(nneeddnumm); / 生生产指定定数量的的产品/* 消费者者类,线线程*/classs CConssumeer eexteendss Thhreaad privvateeinttneeednuum; / 生生产产品品的数量量privvatee Goodowwn ggodoown; Connsummer(intt neeednnum, Goodowwn ggodoown) thiss.neeednuum = neeednnum;thiss.goddownn

29、 = goddownn;publlicvvoidd ruun() godoown.connsumme(nneeddnumm); / 消消费指定定数量的的产品Resuultss off Siimullatiion:The codde, shoown as bellow,“Goddownn goodowwn = neew GGodoown(30);Conssumeer cc1 = neew CConssumeer(550, goddownn); Conssumeer cc2 = neew CConssumeer(220, goddownn);Conssumeer cc3 = neew CConss

30、umeer(330, goddownn);Prodduceer pp1 = neew PProdduceer(110, goddownn);Prodduceer pp2 = neew PProdduceer(110, goddownn);Prodduceer pp3 = neew PProdduceer(110, goddownn);Prodduceer pp4 = neew PProdduceer(110, goddownn);Prodduceer pp5 = neew PProdduceer(110, goddownn);Prodduceer pp6 = neew PProdduceer(

31、110, goddownn);Prodduceer pp7 = neew PProdduceer(880, goddownn);”havee thhe ddireect efffectts oof tthe ressultts. The firrst linne sset thee innitiial nummberr off thhe sstorragee, aand c1-c3, annd pp1-pp7 sset thrreadds cconssumee orr prroduuce thee ceertaain nummberr off ittemss. TThe maxx siize

32、of thee sttoraage is sett too 1000 iin cclasss GGodoown.Therrefoore, seeverral ranndomm reesullts aree shhownn inn thhe ffiguuress beeloww:Conssideerabble Impprovvemeent - Usiing Semmaphhoree inn JaavaIn aall thee maaterriall I havve ffindd, aand sevveraal OOS ttexttboooks I rreadd, iit iis bbeliiev

33、eed tthatt thheree iss noot aa cooncrretee seemapphorre iin jjavaa prrogrram. Hoowevver, whhen loookinng uup tthe offficiial javva ddoc in Oraaclee weebsiitess, II fiind a SSemaaphoore Claass proovidded in libbrarry oof jjavaa.uttil.conncurrrennt. Thee offficciall deeclaarattionn off thhis Claass Se

34、mmaphhoree iss thhat:“Acoounttinggsemmaphhoree,conccepttuallly, maainttainns aa seet oof ppermmitss. EEachhacqquirre()bloockss iff neecesssarry uuntiil aa peermiit iis aavaiilabble, annd tthenn taakess itt. EEachhrelleasse()addds aa peermiit, pottenttiallly relleassingg a bloockiing acqquirrer. Hoow

35、evver, noo acctuaal ppermmit objjectts aare useed; theeSemmaphhoreejusst kkeepps aa coountt off thhe nnumbber avaailaablee annd aactss acccorrdinnglyy.Geneerallly, seemapphorres useed tto cconttroll reesouurcee acccesss sshouuld be iniitiaalizzed as faiir, to enssuree thhat no thrreadd iss sttarvved

36、 outt frrom acccesssingg a ressourrce. Whhen usiing semmaphhorees ffor othher kinnds of synnchrroniizattionn coontrrol, thhe tthrooughhputt addvanntagges of nonn-faair ordderiing oftten outtweiigh faiirneess connsidderaatioons.Thiss cllasss allso prooviddes connvenniennce metthodds ttoacqquirreanddr

37、elleassemulltipple perrmitts aat aa tiime. Beewarre oof tthe inccreaasedd riisk of inddefiinitte pposttponnemeent wheen tthesse mmethhodss arre uusedd wiithoout faiirneess sett trrue.Memoory connsisstenncy efffectts: Acttionns iin aa thhreaad pprioor tto ccalllingg a reeleaase meethood ssuchh assrel

38、leasse()happpenn-beeforreacttionns ffolllowiing a ssucccesssfull aacquuiree mmethhod succh aasacqquirre()in anootheer tthreead.”Moreeoveer, theere is a ssampple codde ffor semmaphhoree:impoort javva.uutill.cooncuurreent.*;publliccclasssSaamplle_oof_ssemaaprivvateestaaticcfinnaliintMMAX_AVAAILAABLEE

39、= 1000;privvateefinnal Semmaphhoree avvaillablle = neew SSemaaphoore(MAXX_AVVAILLABLLE, truue);publlic Objjectt geetIttem() tthroows IntterrrupttedEExceeptiion avaiilabble.acqquirre();retuurn gettNexxtAvvaillablleIttem();publlicvvoidd puutIttem(Objjectt x) if (marrkAssUnuusedd(x)avaiilabble.relleass

40、e(); / Nott a parrticculaarlyy effficciennt ddataa sttruccturre; jusst ffor demmopublliciint iitemms; /. wwhatteveer kkindds oof iitemms bbeinng mmanaageddpubllicbboolleann useed= newwboooleaanMAXX_AVVAILLABLLE;prottecttedssyncchroonizzed Objjectt geetNeextAAvaiilabbleIItemm() for (innt ii = 0; i MA

41、AX_AAVAIILABBLE; +i) if (!ussedi) useddi = truue;retuurniitemmsii; retuurnnnulll; / nnot reaacheed prottecttedssyncchroonizzedbboolleann maarkAAsUnnuseed(OObjeect iteem) for (innt ii = 0; i MAAX_AAVAIILABBLE; +i) if (iteem = iitemmsii) if (useedii) useddi = fallse;retuurnttruee; eelseeretuurnffalsse; retuurnffalsse; In tthe offficiial doccumeentaatioon, we havve mmanyy meethoods in thiis cclasss, in ordder to acqquirre tthe staatuss off thhe rreall seemapphorre, andd usse tthe semmaphhoree chhan

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文献 > 管理制度

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com