操作系統(tǒng)實(shí)習(xí)報(bào)告 -1_第1頁
操作系統(tǒng)實(shí)習(xí)報(bào)告 -1_第2頁
操作系統(tǒng)實(shí)習(xí)報(bào)告 -1_第3頁
操作系統(tǒng)實(shí)習(xí)報(bào)告 -1_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

操作系統(tǒng)實(shí)習(xí)報(bào)告日期:實(shí)習(xí)題目:基于Windows環(huán)境的讀者寫者問題完成人姓名:楊雪亞班級:物聯(lián)網(wǎng)工程學(xué)號:2014141461034實(shí)習(xí)內(nèi)容簡要描述創(chuàng)建一個(gè)控制臺進(jìn)程。此進(jìn)程包含n個(gè)線程。用這n個(gè)線程來表示n個(gè)讀者或?qū)懻?。每個(gè)線程按相應(yīng)測試數(shù)據(jù)文件(后面有介紹)的要求進(jìn)行讀寫操作。用信號量機(jī)制分別實(shí)現(xiàn)讀者優(yōu)先和寫者優(yōu)先的讀者-寫者問題。運(yùn)行結(jié)果顯示要求:要求在每個(gè)線程創(chuàng)建、發(fā)出讀寫操作申請、開始讀寫操作和結(jié)束讀寫操作時(shí)分別顯示一行提示信息,以確定所有處理都遵守相應(yīng)的讀寫操作限制。主要代碼結(jié)構(gòu)(附注釋)publicclassreaderfirst{classwriterextendsThread{//寫者進(jìn)程publicwriter(intno,doublest,doublert);publicvoidrun();}classreaderextendsThread{//讀者進(jìn)程publicreader(intno,doublest,doublert);publicvoidrun();}publicvoidrun();//數(shù)據(jù)寫入和讀取publicstaticvoidmain(Stringargs[]){//主程序 (newreaderfirst()).run(); }}結(jié)果分析(或錯(cuò)誤原因分析)運(yùn)行結(jié)果如左注釋:如果上述表格空間不夠,可以另附表格進(jìn)行說明附代碼:importjava.util.concurrent.Semaphore;publicclassreaderfirst{ Semaphoremutex=newSemaphore(1);//提供互斥reader_count信號量 Semaphorewrt=newSemaphore(1);//提供讀者寫者同步信號量 intrwn=0;//讀者等待數(shù) intreader_count=0;//讀者數(shù),初始值為0 classwriterextendsThread{//寫者進(jìn)程 intnum; doublesleeptime; doubleruntime; publicwriter(intno,doublest,doublert){ num=no; sleeptime=st; runtime=rt; } publicvoidrun(){ try{ sleep((int)(sleeptime*1000)); System.out.println("第"+num+"號寫者想寫,現(xiàn)在共有讀者"+reader_count +"個(gè)"); if(rwn==0){ wrt.acquire(); System.out.println("第"+num+"號寫者正在寫,請等待..."); sleep((int)(runtime*1000)); System.out.println("第"+num+"號寫者寫完了"); wrt.release(); } }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }System.out.println(num+”“+sleeptime+”“+runtime); } } classreaderextendsThread{//讀者進(jìn)程 intnum; doublesleeptime; doubleruntime; publicreader(intno,doublest,doublert){ num=no; sleeptime=st; runtime=rt; } publicvoidrun(){ try{ sleep((int)(sleeptime*1000)); System.out.println("第"+num+"號讀者想讀"); rwn++; mutex.acquire(); rwn--; if(reader_count==0){ wrt.acquire(); } reader_count++; System.out.println("第"+num+"號讀者正在讀"); System.out.println("現(xiàn)在共有"+reader_count+"個(gè)讀者"); mutex.release(); sleep((int)(runtime*1000)); mutex.acquire(); reader_count--; System.out.println("第"+num+"號讀者讀完了,此時(shí)共有"+reader_count +"個(gè)讀者"); if(reader_count==0){ wrt.release(); } mutex.release(); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }System.out.println(num+”“+sleeptime+”“+runtime); } } publicvoidrun(){ readerr1=newreader(1,3,5); writerw2=newwriter(2,4,5); readerr3=newreader(3,5,2); readerr4=newreader(4,6,5); writerw5=newwriter(5,5.2,3);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論