山大操作系統課程設計報告(全套)_第1頁
山大操作系統課程設計報告(全套)_第2頁
山大操作系統課程設計報告(全套)_第3頁
山大操作系統課程設計報告(全套)_第4頁
山大操作系統課程設計報告(全套)_第5頁
已閱讀5頁,還剩47頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、計算機科學與技術學院實驗報告:3實驗題目:信號量同步問題日期:2010-11-10姓名: 實驗目的:在本次實驗中,通過使用信號量,在原有的程序框架的基礎上添加關鍵代碼實現生產者/消費者同步問題。從而深入理解nachos的信號量的使用以及實現,生產者/消費者問題是如何用信號量實現的以及在nachos中是如何創建線程,實現多線程。硬件環境:軟件環境:linux 實驗步驟:1.首先初始化三個信號量,代碼如下:mutex = new semaphore(mutux,1);信號量初始化為1,才能起到加鎖功能nfull = new semaphore(full,0);nfull的大小在生產者沒生產前為0n

2、empty = new semaphore(empty,buff_size);nempty的大小應該為buffer的大小2.首先考慮生產者進程,首先要查看buffer是否有空, nempty-p();if nempty0,nempty=nempty -1,當對緩沖區操作時必須要加鎖:mutex-p();加鎖.然后向ring中放入message信息,其次還要解鎖mutex-v();解鎖.最后通知消費者buffer有新信息, nfull-v();nfull=nfull+1;具體實現代碼如下:3. 考慮消費者進程,像生產者進程一樣,查看buffer中是否有信息nfull-p();if nfull0,

3、nfull-1;取消息時也要上鎖,即:mutex-p();加鎖.然后從ring buffer中取出信息;其次mutex-v();解鎖;最后通知生產者bufferr有空nempty-v();nempty=nempty+1,具體代碼如下:4. 創建線程生成一個生產者的代碼:producersi = new thread(prod_namesi);producersi - fork(producer,i);4. 創建線程生成一個消費者的代碼:producersi = new thread(prod_namesi);producersi - fork(producer,i);關鍵代碼:voidprod

4、ucer(_int which) int num; slot *message = new slot(0,0); for (num = 0; num thread_id=which; message-value=num;/p,v 操作 nempty-p(); mutex-p(); ring-put(message);/p,v 操作 mutex-v(); nfull-v(); voidconsumer(_int which) char strmaxlen; char fnamelinelen; int fd; slot *message = new slot(0,0); sprintf(fnam

5、e, tmp_%d, which); / create a file. note that this is a unix system call. if ( (fd = creat(fname, 0600) ) = -1) perror(creat: file create failed);exit(1); for (; ; ) / p,v,操作 nfull-p(); mutex-p(); ring-get(message); / p,v,操作 mutex-v(); nempty-v(); / form a string to record the message sprintf(str,pr

6、oducer id - %d; message number - %d;n,message-thread_id,message-value); /把信息寫入文件 if ( write(fd, str, strlen(str) = -1 ) perror(write: write failed); exit(1); /-/ prodcons/ 初始化信號量以及需要的生產者消費者線程/-voidprodcons() int i; debug(t, entering prodcons);/ 初始化信號量,包括一個訪問互斥信號量,初值為1;/一個nempty信號量,初值為緩沖區的大小/一個nfull的

7、信號量,初值為0 mutex=new semaphore(mutex,1); nempty=new semaphore(nempty,buff_size); nfull=new semaphore(nfull,0); / 新建一個緩沖區 ring=new ring(buff_size+1); / create and fork n_prod of producer threads for (i=0; i fork(producer,i); ; / create and fork n_cons of consumer threads for (i=0; i fork(consumer,i); ;

8、調試記錄:在源代碼中exit(0)沒有大寫,調試過程發現了這個問題改正,在使用linux系統調用寫入文件時,有一個頭文件沒有引入,因而需要修改#include #include copyright.h#include system.h#include #include 而且對于新添加的頭文件的方法其中源文件使用的一個方法是廢棄的,所以改成相應的方法write(fd, str, strlen(str),實驗結果:生成兩個文件分別代表兩個消費者取得的產品的記錄。文件tmp_0producer id - 0; message number - 3;文件tmp_1producer id - 0; me

9、ssage number - 0;producer id - 1; message number - 0;producer id - 1; message number - 1;producer id - 0; message number - 1;producer id - 0; message number - 2;producer id - 1; message number - 2;producer id - 1; message number - 3;分析結果:從實驗結果中可以看出,兩個消費者取得的產品的順序和生成者生產的順序是一致的。結果正確。(實驗所在目錄:home/lu/csc

10、2404/nachos-3.4/code/lab3)結論分析與體會:在本次實驗中,實現生產者/消費者同步問題,通過使用信號量,即nachos 提供的系統調用,進一步理解nachos的信號量的使用以及實現同時,學會在nachos中是如何創建線程,實現多線程,理解了多線程的問題。計算機科學與技術學院實驗報告:5實驗題目:擴展nachos的文件系統學號:200800130090 日期:2010-11-10姓名:陸思思 email:實驗目的:nachos的文件系統的文件的大小是不可擴展的:文件被創建后,文件的大小就不能再改變。本次實驗的目的即是設計擴展nachos的文件系統,使得文件的大小是可以被擴展

11、的。這樣就可以實現在一個文件尾部或者中間追加文件。硬件環境:軟件環境:linux操作系統,nachos操作系統 實驗步驟:1, 了解nachos文件系統的結構,為一級目錄結構,其中目錄結構以及目錄的使用記錄保存在文件中。使用bitmap來獲取空閑的扇區號。 class directoryentry public: bool inuse;/ is this directory entry in use? int sector;/ location on disk to find the / fileheader for this file char namefilenamemaxlen + 1;

12、/ text name for file, with +1 for / the trailing 0;這個是directoryentry類,也就是目錄項。directory:directory(int size) table = new directoryentrysize; tablesize = size; for (int i = 0; i tablesize; i+)tablei.inuse = false;這個是目錄類,也就是一級目錄結構的定義。booldirectory:add(char *name, int newsector) if (findindex(name) != -1

13、)return false; for (int i = 0; i tablesize; i+) if (!tablei.inuse) tablei.inuse = true; strncpy(, name, filenamemaxlen); tablei.sector = newsector; return true; return false;/ no space. fix when we have extensible files.這個是添加一個目錄項的方法,當創建一個新文件的時候使用。boolfilesystem:create(char *name, int ini

14、tialsize)這個是創建一個新的文件,其中主要工作是新建一個fileheader,作為一個目錄項中保存的 int sector;fileheader,即文件頭,中保存了這個文件的大小,所占的扇區的數目,以及所占用的全部的扇區號。即: int numbytes;/ number of bytes in the file int numsectors;/ number of data sectors in the file int datasectorsnumdirect;/ disk sector numbers for each data / block in the file因此,為了實

15、現對文件的追加工作,首先對fileheader類里面加入新的方法bool appsectors(bitmap *freemap, int filesize);,為了改變一個文件的文件頭的大小。2, 實現在一個已有的文件尾部追加新的內容。首先寫改變文件頭中對文件所在扇區的描述,由appsectors來實現;該方法將在openfile類的對象執行append file 時被調用。對fileheader類里面加入新的方法bool appsectors(bitmap *freemap, int filesize);boolfileheader:appsectors(bitmap *freemap, i

16、nt appfilesize)/如果要追加的文件大小小于等于0,則直接函數返回 if(appfilesize= appfilesize) numbytes += appfilesize; return true; else int needfilesize = appfilesize - restfilesize; if(freemap-numclear() divroundup(needfilesize, sectorsize) return false; int i = numsectors; numbytes += appfilesize; numsectors += divroundu

17、p(needfilesize, sectorsize); for( ; i find(); return true; printf(the fileheader see filesize %dn,filelength();3,在openfile.cc文件中,openfile類中加入方法,用于追加文件的appfilesize(int numbyte)。這個方法首先從文件系統中獲取空閑的扇區的位試圖文件,構造bitmap對象,傳給fileheader類的對象(也就是這個openfile的文件頭),執行appsectors(bitmap *freemap, int filesize)方法,擴大文件的

18、長度。boolopenfile:appfilesize(int numbytes) /fetch the bitmap openfile *freemapfile= new openfile(freemapsector); bitmap *freemap = new bitmap(numsectors); freemap-fetchfrom(freemapfile); /ask for new space if(!(hdr-appsectors(freemap, numbytes)return false;printf(openfile see filesize %dn,length(); /

19、write back the bitmap freemap-writeback(freemapfile); delete freemapfile; delete freemap; return true;4,在fstest.cc文件中的修改append方法,也就是在nachos運行的時候執行命令,例如:nachos x ap ./test/small small這個方法是nachos系統中本來就提供好的一個方法,只需要在局部修改一下語句,就可以正確的運行。voidappend(char *from, char *to, int half)/*filelength 是計算出來的linux文件中的

20、文件的大小filelengthbefore是指追加之前的nachos系統的文件的大小start是文件開始追加的位置*/ file *fp; openfile* openfile; int amountread, filelength; char *buffer;/ start position for appending int start;/ open unix file if (fp = fopen(from, r) = null) printf(copy: couldnt open input file %sn, from);return; / figure out length of

21、unix file fseek(fp, 0, 2); filelength = ftell(fp); fseek(fp, 0, 0); if (filelength = 0) printf(append: nothing to append from file %sn, from);return; if ( (openfile = filesystem-open(to) = null) / file to does not exits, then create oneif (!filesystem-create(to, 0) printf(append: couldnt create the

22、file %s to appendn, to); fclose(fp); return;openfile = filesystem-open(to); int filelengthbefore=openfile-length();/*執行追加,并判斷要追加文件的大小是否超過最大長度*/ if(!(openfile-appfilesize(filelength) printf(the appending file is too big!nappending file failedn);/too long file to append return; assert(openfile != null

23、); / append from position start start=openfile-length()-filelengthbefore;printf(value of start %dn,start); if (half) start = start / 2;/如果是從文件的中部追加 openfile-seek(start); / append the data in transfersize chunks buffer = new chartransfersize; while (amountread = fread(buffer, sizeof(char), transfersi

24、ze, fp) 0) int result;printf(start value: %d, amountread %d, , start, amountread);result = openfile-writeat(buffer, amountread, start);printf(result of write: %dn, result);start += amountread; delete buffer;/ write the inode back to the disk, because we have changed it/需要把該文件的文件頭寫回磁盤 openfile-writeb

25、ack(to); printf(inodes have been written backn);/ close the unix and the nachos files delete openfile; fclose(fp);5,運行結果:首先執行./nachos f ,格式化nachos磁盤luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -fno threads ready or runnable, and no pending interrupts.assuming the program completed.machine

26、halting!ticks: total 82600, idle 82350, system 250, user 0disk i/o: reads 3, writes 5console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.然后執行追加文件的操作:./nachos -ap test/small smallluubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -ap test/small smallop

27、enfile see filesize 38value of start 38start value: 38, amountread 10, result of write: 0start value: 48, amountread 10, result of write: 0start value: 58, amountread 10, result of write: 0start value: 68, amountread 8, result of write: 0inodes have been written backno threads ready or runnable, and

28、 no pending interrupts.assuming the program completed.machine halting!ticks: total 99100, idle 98400, system 700, user 0disk i/o: reads 16, writes 6console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.再執行從文件中部追加文件的操作:./nachos -hap test/small smallluubuntu:

29、/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small smallopenfile see filesize 76value of start 38start value: 19, amountread 10, result of write: 10start value: 29, amountread 10, result of write: 10start value: 39, amountread 10, result of write: 10start value: 49, amountread 8, result o

30、f write: 8inodes have been written backno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 83100, idle 82470, system 630, user 0disk i/o: reads 14, writes 6console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, s

31、ent 0cleaning up. luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -hap test/small smallopenfile see filesize 76value of start 38start value: 19, amountread 10, result of write: 10start value: 29, amountread 10, result of write: 10start value: 39, amountread 10, result of write: 10start value:

32、 49, amountread 8, result of write: 8inodes have been written backno threads ready or runnable, and no pending interrupts.assuming the program completed.machine halting!ticks: total 83100, idle 82470, system 630, user 0disk i/o: reads 14, writes 6console i/o: reads 0, writes 0paging: faults 0network

33、 i/o: packets received 0, sent 0cleaning up.最后執行“顯示“操作,即顯示nachos磁盤當前的內容:luubuntu:/csc2404/nachos-3.4/code/appfilesys$ ./nachos -dbit map file header:fileheader contents. file size: 128. file blocks:2 file contents:7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000

34、0000000000000000000000000000000000000000000directory file header:fileheader contents. file size: 200. file blocks:3 4 file contents:10005000small00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

35、00000000000000000000000000000000bitmap set:0, 1, 2, 3, 4, 5, 6, directory contents:name: small, sector: 5fileheader contents. file size: 76. file blocks:6 file contents:this is the spring this is the spring of our discontent.aof our discontent.ano threads ready or runnable, and no pending interrupts

36、.assuming the program completed.machine halting!ticks: total 6500, idle 6090, system 410, user 0disk i/o: reads 12, writes 0console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.(實驗所在目錄:csc2404/nachos-3.4/code/appfilesys主要修改文件,filehdr.cc,openfile.cc,fstest.

37、cc,filehdr.h,openfile.h)結論分析與體會:通過增加nachos文件系統對文件的追加功能,理解了文件系統的管理方式,通過目錄,目錄項,文件頭,進一步理解了文件的存儲方式,以及空閑磁盤空間的管理,使用位試圖 的方式管理空閑的磁盤空間。返回計算機科學與技術學院實驗報告:6,7,8實驗題目:地址空間的擴展,實現系統調用exec,exit日期:2010-11-5email:實驗目的:擴展現有的class addrspace的實現,使得nachos可以實現多用戶程序,完成系統調用exec使nachos按頁分配內存空間硬件環境:軟件環境:linux,nachos,mips 實驗步驟:1

38、. 首先編寫實驗6的print函數,為了可以顯示內存空間的分配。以下為print函數的實現:2. 編寫一個exec.c的源碼,然后編譯生成一個可執行文件,exec.noff exec.c的源碼:3. nachos原來實現addressspace分配的時候沒有使用bitmap來尋找空閑頁,而是直接的從0號內存空間開始分配,因而需要修改成使用bitmap的find函數分配內存空間其中,要聲明一個位試圖的是對象,在addrspace中并且數據段代碼段按頁存儲。4. 計算加載一個程序需要的頁表的數目5. 實現nachos的系統調用,采用的是觸發異常中斷的,在userprog/exception.cc,

39、添加sc_exec異常,存放要執行的文件的路徑的字符串的首地址存放在4號寄存器中,因此可以通過這個方式找到文件的路徑,從而使用文件系統提供的方法打開這個文件:6. 結果分析:編譯成功之后,輸入命令 ./nachos x ./test/exec.noff運行結果:luubuntu:/csc2404/nachos-3.4/code/userprog$ ./nachos -x ./test/exec.noffpage table dump: 12 pages in total=virtpage,physpage0,01,12,23,34,45,56,67,78,89,910,1011,11=the

40、allocated physical address:0he address in the executable file :40copy size 128the allocated physical address:128he address in the executable file :168copy size 128the allocated physical address:256he address in the executable file :296copy size 128sc_exec in exceptionhandlerthe value of the register

41、 4 is, .i.e. the address of the string 272add content of fn is now: 742f2e2ethe char view is . . / tadd content of fn is now: 2f747365the char view is e s t /add content of fn is now: 746c6168the char view is h a l tadd content of fn is now: 666f6e2ethe char view is . n o fadd content of fn is now:

42、66the char view is f add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is add content of fn is now: 0the char view is exec file is ./test/halt.noffpage table dump: 11 pages in t

43、otal=virtpage,physpage0,121,132,143,154,165,176,187,198,209,2110,22=the allocated physical address:1536he address in the executable file :40copy size 128the allocated physical address:1664he address in the executable file :168copy size 128the allocated physical address:1792he address in the executab

44、le file :296copy size 128machine halting!ticks: total 50, idle 0, system 10, user 40disk i/o: reads 0, writes 0console i/o: reads 0, writes 0paging: faults 0network i/o: packets received 0, sent 0cleaning up.luubuntu:/csc2404/nachos-3.4/code/userprog$加載進來的程序的地址空間沒有覆蓋原來那個程序的地址空間,使用新的地址空間去執行關機操作machin

45、e halting!實現了一個程序加載另一個程序運行。(實驗所在路徑: csc2404/nachos-3.4/code/ multiprocess 主要修改的文件,addrspace.cc exception.cc )結論分析與體會:nachos之前沒有實現按頁分配地址空間,物理頁和邏輯頁地址一致,而且數據段代碼段連續分配每當一個新的用戶程序建立的時候,虛擬地址和物理地址都是從0開始分配,這樣新的用戶程序將會沖掉原來在物理0開始的程序。因而使用位示圖分配物理地址。使用bitmap的find函數分配虛存對應的物理地址,在為數據段和代碼段寫入數據的時候是以扇區為單位的,而不是原有的連續一個文件的讀入連續的內存。nachos操作系統通過中斷的方式實現系統調用。需要增加userprog/exception.cc中的內容,即必須在此類中添加處理exec的方法。 返回計算機科學與技術學院實驗報告:9實驗題目:設計并實現具有優先級的線程調度策略 日期:2010-11-13 email:實驗目的:nachos系統采用基本的fcfs的線程調度策略,修改成為具有優先級的線程調度策略硬件環境:軟

溫馨提示

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

評論

0/150

提交評論