東北大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告_第1頁
東北大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告_第2頁
東北大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告_第3頁
東北大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告_第4頁
東北大學(xué)操作系統(tǒng)實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩23頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上計(jì)算機(jī)科學(xué)與工程學(xué)院實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)課程名稱操作系統(tǒng)實(shí)驗(yàn)實(shí)驗(yàn)成績(jī)專業(yè)計(jì)算機(jī)科學(xué)與技術(shù)班級(jí)1507班指導(dǎo)教師簽字學(xué)號(hào)姓名羅藝博實(shí)驗(yàn)報(bào)告批改時(shí)間實(shí)驗(yàn)項(xiàng)目目錄1. 實(shí)驗(yàn)一 熟悉Linux系統(tǒng)2. 實(shí)驗(yàn)二 進(jìn)程狀態(tài)3. 實(shí)驗(yàn)三 進(jìn)程同步和通信4. 實(shí)驗(yàn)四 進(jìn)程的管道通信5. 實(shí)驗(yàn)五 頁面置換算法實(shí)驗(yàn)報(bào)告正文實(shí)驗(yàn)一 熟悉Linux系統(tǒng)一、 實(shí)驗(yàn)?zāi)康氖煜ず驼莆誏inux系統(tǒng)基本命令,熟悉Linux編程環(huán)境,為以后的實(shí)驗(yàn)打下基礎(chǔ)。二、 實(shí)驗(yàn)原理基于linux系統(tǒng)的基礎(chǔ)操作三、 實(shí)驗(yàn)內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴(kuò)展點(diǎn)等)啟動(dòng)、退出、ls(顯示目錄內(nèi)容)、cp(文件或目錄的復(fù)制)、mv(文

2、件、目錄更名或移動(dòng))、rm(刪除文件或目錄)、mkdir(創(chuàng)建目錄)、rmdir(刪除空目錄)、cd(改變工作目錄)C語言編輯、編譯四、 實(shí)驗(yàn)結(jié)果(截圖)ls mkdir:cd:rmdir:實(shí)驗(yàn)二 進(jìn)程狀態(tài)一、 實(shí)驗(yàn)?zāi)康淖孕芯幹颇M程序,通過形象化的狀態(tài)顯示,使學(xué)生理解進(jìn)程的概念、進(jìn)程之間的狀態(tài)轉(zhuǎn)換及其所帶來的PCB內(nèi)容 、組織的變化,理解進(jìn)程與其PCB間的一一對(duì)應(yīng)關(guān)系。二、 實(shí)驗(yàn)原理1.進(jìn)程在內(nèi)存中存在三種基本狀態(tài):就緒態(tài)、執(zhí)行態(tài)、阻塞態(tài)2.三種狀態(tài)在滿足某種條件時(shí)會(huì)發(fā)生轉(zhuǎn)換:就緒運(yùn)行:調(diào)度程序選擇一個(gè)新的進(jìn)程運(yùn)行運(yùn)行就緒:運(yùn)行進(jìn)程用完了時(shí)間片運(yùn)行進(jìn)程被中斷,因?yàn)橐桓邇?yōu)先級(jí)進(jìn)程處于就緒狀態(tài)運(yùn)

3、行阻塞:當(dāng)一進(jìn)程等待某一事件的發(fā)生時(shí),如請(qǐng)求系統(tǒng)服務(wù); 初始化I/O 且必須等待結(jié)果; 無新工作可做; 等待某一進(jìn)程提供輸入 (IPC)阻塞就緒:當(dāng)所等待的事件發(fā)生時(shí)三、 實(shí)驗(yàn)內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴(kuò)展點(diǎn)等)#include <iostream>#include <vector>#include <stdlib.h>#include <algorithm> /for "sort" in vectorusing namespace std;class Pro /process classpublic: char name;

4、 string status;vector<Pro> ru,re,bl; /ru->running,re->ready,bl->blocked/function declarationint helloUI();int iniQ();int showPCB();int ruTOre();int ruTObl();int blTOre();int neTOre();int ruTOex();int helloUI() /start UI cout << "Hello!Welcome to come back." << en

5、dl; cout << "#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#" << endl; cout << endl; cout << endl; return 0;int iniQ() /initialize the process int i; cout << "Please enter processes names and their status." << endl; cout << endl; for(i=0; i<5;

6、 i+) /15 process a to o Pro process; char nam; string sta; cout << "Please enter " << i <<" processes names." << endl; cin >> nam; = nam; cout << "Please enter processes status." << endl; cout << "Status

7、contains r1(running),r2(ready) and b(blocked)." << endl; cin >> sta; process.status = sta; if(sta = "r1") /judge which status if(ru.size()<1) ru.push_back(process); cout << "yes" << endl; else cout << "Error!" << endl; else if

8、(sta = "r2") re.push_back(process); else if(sta = "b") bl.push_back(process); else cout << "Error!" << endl; cout << "" << endl; cout << endl; showPCB(); return 0;int showPCB() int i; cout << "running:" for(i=0; i&

9、lt;ru.size(); i+) cout << << "," cout << endl; cout << "ready:" for(i=0; i<re.size(); i+) cout << << "," cout << endl; cout << "blocked:" for(i=0; i<bl.size(); i+) cout << &

10、lt;< "," cout << endl; return 0;int ruTOre() if(!ru.empty() /runningQueue is being used. re.push_back(ru.front(); /running's first process go to ready'last ru.erase(ru.begin(); /delete running's first process ru.push_back(re.front(); re.erase(re.begin(); else cout <

11、< "Error in ruTOre" << endl; showPCB(); return 0;int ruTObl() if(!ru.empty() /runningQueue is being used. bl.push_back(ru.front(); ru.erase(ru.begin(); if(!re.empty() ru.push_back(re.front(); re.erase(re.begin(); else cout << "Error in ruTObl1." << endl; else

12、 cout << "Error in ruTObl2." << endl; showPCB(); return 0;int blTOre() if(!bl.empty() /blockedQueue is not empty. re.push_back(bl.front(); bl.erase(bl.begin(); if(ru.empty() /runningQueue is empty,then ready->running ru.push_back(re.front(); re.erase(re.begin(); else cout &l

13、t;< "Error in blTOre" << endl; showPCB(); return 0;int neTOre() int i; cout << "Please enter processes names and their status." << endl; cout << endl; Pro process; char nam; string sta; cout << "Please enter processes names." << en

14、dl; cin >> nam; = nam; process.status = "r2" re.push_back(process); cout << "" << endl; cout << endl; if(ru.empty() ru.push_back(re.front(); re.erase(re.begin(); showPCB(); return 0;int ruTOex() if(!ru.empty() /runningQueue is being used. ru.e

15、rase(ru.begin(); if(!re.empty() ru.push_back(re.front(); re.erase(re.begin(); else cout << "Error in ruTOex1." << endl; else cout << "Error in ruTOex2." << endl; showPCB(); return 0;int main() int act; /choose action helloUI(); cout << "Please i

16、nitialize the process." << endl; iniQ(); while(1) cout << "" << endl; cout << "Please select the action to take." << endl; cout << "2:running->ready" << endl; cout << "3:running->blocked" << endl

17、; cout << "4:blocked->ready" << endl; cout << "5:new->ready" << endl; cout << "6:running->exit" << endl; cin >> act; if(act = 2) ruTOre(); else if(act = 3) ruTObl(); else if(act = 4) blTOre(); else if(act = 5) neTOre();

18、 else if(act = 6) ruTOex(); else cout << "Error in select."<<endl; cout << "#" << endl; cout << endl; return 0;拓展點(diǎn):五狀態(tài)模型四、 實(shí)驗(yàn)結(jié)果(截圖)創(chuàng)建進(jìn)程:狀態(tài) runningàready:狀態(tài) runningàblocked:狀態(tài) blockedàready:創(chuàng)建新進(jìn)程:情況一 有進(jìn)程正在運(yùn)行情況二 無進(jìn)程正在運(yùn)行終止進(jìn)程:實(shí)驗(yàn)三 進(jìn)程同步和通信一、

19、實(shí)驗(yàn)?zāi)康恼{(diào)試、修改、運(yùn)行模擬程序,通過形象化的狀態(tài)顯示,使學(xué)生理解進(jìn)程的概念,了解同步和通信的過程,掌握進(jìn)程通信和同步的機(jī)制,特別是利用緩沖區(qū)進(jìn)行同步和通信的過程。通過補(bǔ)充新功能,使學(xué)生能靈活運(yùn)用相關(guān)知識(shí),培養(yǎng)創(chuàng)新能力。二、 實(shí)驗(yàn)原理假定.緩沖區(qū)可以容納8個(gè)數(shù)據(jù);因?yàn)榫彌_區(qū)是有限的,因此當(dāng)其滿了時(shí)生產(chǎn)者進(jìn)程應(yīng)該等待;當(dāng)消費(fèi)者取走一個(gè)數(shù)據(jù)后,應(yīng)喚醒正在等待的生產(chǎn)者進(jìn)程;當(dāng)緩沖區(qū)空時(shí),消費(fèi)者進(jìn)程應(yīng)該等待;當(dāng)生產(chǎn)者向緩沖區(qū)放入了一個(gè)數(shù)據(jù)時(shí),應(yīng)喚醒正在等待的消費(fèi)者進(jìn)程。這就是生產(chǎn)者和消費(fèi)者之間的同步三、 實(shí)驗(yàn)內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴(kuò)展點(diǎn)等)基礎(chǔ)內(nèi)容:編寫程序使其模擬兩個(gè)進(jìn)程,即生產(chǎn)者(produ

20、cer)進(jìn)程和消費(fèi)者(Consumer)進(jìn)程工作;生產(chǎn)者每次產(chǎn)生一個(gè)數(shù)據(jù),送入緩沖區(qū)中;消費(fèi)者每次從緩沖區(qū)中取走一個(gè)數(shù)據(jù)。每次寫入和讀出數(shù)據(jù)時(shí),都將讀和寫指針加一。當(dāng)指針到達(dá)緩沖區(qū)尾,重新將指針退回起點(diǎn);/*/* PROGRAM NAME: PRODUCER_CONSUMER */* This program simulates two processes, producer which */* continues to produce message and put it into a buffer */* implemented by PIPE, and consumer which co

21、ntinues to get */* message from the buffer and use it. */* The program also demonstrates the synchronism between */* processes and uses of PIPE. */*/#include <stdio.h>#include <stdlib.h>/#include <time.h>#define PIPESIZE 8#define PRODUCER 0#define CONSUMER 1#define RUN 0 /* statu o

22、f process */#define WAIT 1 /* statu of process */#define READY 2 /* statu of process */#define NORMAL 0#define SLEEP 1#define AWAKE 2#include <stdio.h>/Process Control block struct pcb char *name; int statu; int time; ; /* times of execution */ /Buffer struct pipetype char type; /type int writ

23、eptr; /Write pointer int readptr; /Read pointer struct pcb *pointp; /* write wait point */ struct pcb *pointc; ; /* read wait point */ int pipePIPESIZE; /Buffer array struct pipetype pipetb; struct pcb process2; /number of producer - number of consumer,buffer. count >= 8:too many prodecers; <=

24、0:too many consumers int count=0; int countp=0,countr=0;main()int output,ret,i; /output->number of times, ret->present status char in2; int runp(),runc(),prn(); pipetb.type = 'c' pipetb.writeptr = 0; pipetb.readptr = 0; pipetb.pointp = pipetb.pointc = NULL; processPRODUCER.name = "

25、;Producer0" processCONSUMER.name = "Consumer0" processPRODUCER.statu = processCONSUMER.statu = READY; processPRODUCER.time = processCONSUMER.time = 0; output = 0;printf("Now starting the program!n"); printf(" Press 'p1' to run PRODUCER1, press 'p2' to ru

26、n PRODUCER2.n press 'c' to run CONSUMER.n"); /PRODUCER1->product 1 new data, PRODUCER2->product 2 new data printf(" Press 'e' to exit from the program.n"); for(i=0;i<1000;i+)in0='N' while(in0='N')scanf("%s",in); if(in0!='e'&am

27、p;&in0!='p'&&in0!='c') /when not p,c,e continuein0='N' if(in0='p'&&processPRODUCER.statu=READY) /producer and ready if(in1 = '2') /producer2 int m; for(m=0;m<2;m+) if(countp > 3) /the number of waitting producer over 4,waitqueue=4 pri

28、ntf("wrong!n"); continue; if(count<8) /output = rand()%99+1; /099 output = (output+1)%100; if(ret=runp(output,process,pipe,&pipetb,PRODUCER)=SLEEP) /sleep,set write wait pointer pipetb.pointp = &processPRODUCER; if(ret=AWAKE) /awake,execute consumer (pipetb.pointc)->statu=REA

29、DY; runc(process,pipe,&pipetb,CONSUMER); countr-; printf("countr=%dn",countr); if(countr=0) pipetb.pointc=NULL; else if(in1 = '1') /producer1 if(countp > 3) printf("wrong!n"); continue; if(count<8) /output = rand()%99+1; /099 output = (output+1)%100; if(ret=runp

30、(output,process,pipe,&pipetb,PRODUCER)=SLEEP) /sleep pipetb.pointp = &processPRODUCER; if(ret=AWAKE) /awake (pipetb.pointc)->statu=READY; runc(process,pipe,&pipetb,CONSUMER); countr-; printf("countr=%dn",countr); if(countr=0)pipetb.pointc=NULL; if(in0='c'&&pr

31、ocessCONSUMER.statu=READY) /consumer and readyif(ret=runc(process,pipe,&pipetb,CONSUMER)=SLEEP) /sleeppipetb.pointc = &processCONSUMER; if(ret=AWAKE) /awake(pipetb.pointp)->statu=READY;output=(output+1)%100; runp(output,process,pipe,&pipetb,PRODUCER);countp-;printf("countp=%dn&qu

32、ot;,countp);if(countp=0)pipetb.pointp=NULL;if(in0='p'&&processPRODUCER.statu=WAIT) /producer and wait if(in1 = '2') /producer2 int m; for(m=0;m<2;m+) if(countp > 3) printf("wrong!n"); printf("PRODUCER is waiting, can't be scheduled.n"); continue;

33、 countp+; printf("countp=%dn",countp); else if(in1 = '1') /producer1 if(countp > 3) printf("wrong!n"); printf("PRODUCER is waiting, can't be scheduled.n"); continue; countp+; printf("countp=%dn",countp); printf("Look out.n");/printf(&q

34、uot;PRODUCER is waiting, can't be scheduled.n"); if(in0='c'&&processCONSUMER.statu=WAIT) /consumer and wait if(countr > 3) printf("wrong!n"); printf("CONSUMER is waiting, can't be scheduled.n"); continue; countr+;printf("countr=%dn",count

35、r);printf("Look out!n"); /printf("CONSUMER is waiting, can't be scheduled.n"); if(in0='e') exit(1); prn(process,pipe,pipetb); in0='N' runp(out,p,pipe,tb,t) /* run producer */ int out,pipe,t; struct pcb p; struct pipetype *tb; pt.statu=RUN; printf("run PRO

36、DUCER. product %d ",out); if(count>=8) /buffer over 8 pt.statu=WAIT; return(SLEEP); tb->writeptr=tb->writeptr%8; /only has pipetb->writeptr=out; /change tb->writeptr+; printf("writeptr%dn",tb->writeptr); count+; printf("count=%dn",count); pt.time+; /printf(

37、"time+%dn",pt.time); pt.statu=READY; if(tb->pointc)!=NULL) /printf("返回AWAKE"); return(AWAKE); return(NORMAL); runc(p,pipe,tb,t) /* run consumer */ int pipe,t; struct pcb p; struct pipetype *tb; int c; pt.statu = RUN; printf("run CONSUMER. "); if(count<=0) /buffer

38、too less pt.statu=WAIT; return(SLEEP); c=pipetb->readptr; pipetb->readptr=0; tb->readptr+; tb->readptr=tb->readptr%8; printf("readptr=%dn",tb->readptr); printf(" use %d ",c); count-; printf("count=%dn",count); pt.time+; /printf("time+%dn",pt.

39、time); pt.statu=READY; if(tb->pointp!=NULL) /printf("返回AWAKEn"); return(AWAKE); return(NORMAL); prn(p,pipe,tb) int pipe; struct pipetype tb; struct pcb p; int i; printf("n "); for(i=0;i<PIPESIZE;i+) printf("- "); printf("n |");for(i=0;i<PIPESIZE;i+) /

40、pipe insideif(pipei!=0) /pipei has somethingprintf(" %2d |",pipei);elseprintf(" |"); printf("n "); for(i=0;i<PIPESIZE;i+) printf("- "); printf("nwriteptr = %d, readptr = %d, ",tb.writeptr,tb.readptr); if(pPRODUCER.statu=WAIT)|countp>=1) /number

41、 of producer >=1 printf("%d PRODUCER wait ",countp); else printf("PRODUCER ready "); if(pCONSUMER.statu=WAIT)|countr>=1) printf("%d CONSUMER wait ",countr); else printf("CONSUMER ready "); printf("n"); printf("n#n"); 拓展點(diǎn):設(shè)置了等待隊(duì)列的長(zhǎng)度,并顯

42、示等待隊(duì)列內(nèi)部情況四、 實(shí)驗(yàn)結(jié)果(截圖)生產(chǎn)者:情況一 正常生產(chǎn)情況二 超過緩沖區(qū)情況三 超過等待隊(duì)列消費(fèi)者:情況一 正常情況二 無產(chǎn)品可消費(fèi)又開始生產(chǎn):實(shí)驗(yàn)四 進(jìn)程的管道通信一、 實(shí)驗(yàn)?zāi)康募由顚?duì)進(jìn)程概念的理解,明確進(jìn)程和程序的區(qū)別;學(xué)習(xí)進(jìn)程創(chuàng)建的過程,進(jìn)一步認(rèn)識(shí)并發(fā)執(zhí)行的實(shí)質(zhì);分析進(jìn)程爭(zhēng)用資源的現(xiàn)象,學(xué)習(xí)解決進(jìn)程互斥的方法;學(xué)習(xí)解決進(jìn)程同步的方法;掌握Linux系統(tǒng)進(jìn)程間通過管道通信的具體實(shí)現(xiàn)方法。二、 實(shí)驗(yàn)原理1.基本概念:進(jìn)程的概念;進(jìn)程與程序的區(qū)別;并發(fā)執(zhí)行的概念進(jìn)程互斥的概念;進(jìn)程通信的基本原理2.系統(tǒng)調(diào)用:設(shè)置系統(tǒng)調(diào)用號(hào):設(shè)置多條系統(tǒng)調(diào)用命令,并賦予每條系統(tǒng)調(diào)用命令一個(gè)唯一的系統(tǒng)調(diào)

43、用號(hào)處理系統(tǒng)調(diào)用:OS中有一張系統(tǒng)調(diào)用入口表,表中每個(gè)表目對(duì)應(yīng)一條系統(tǒng)調(diào)用命令,包含該系統(tǒng)調(diào)用自帶參數(shù)的數(shù)目、系統(tǒng)調(diào)用命令處理程序的入口地址等。OS內(nèi)核便是根據(jù)所輸入的系統(tǒng)調(diào)用號(hào)在該表中查找到相應(yīng)的系統(tǒng)調(diào)用,進(jìn)而轉(zhuǎn)入它的入口地址去執(zhí)行系統(tǒng)調(diào)用程序。Linux的系統(tǒng)調(diào)用機(jī)制:通過中斷機(jī)制實(shí)現(xiàn)三、 實(shí)驗(yàn)內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴(kuò)展點(diǎn)等)#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>in

44、t main() int pid1,pid2,pid3; int fd2; char outpipe50,inpipe50; pipe(fd); while (pid1=fork() = -1); if(pid1 = 0) printf("s1n"); lockf(fd1,1,0);/ printf("s11n"); sprintf(outpipe,"I am the 1st!"); write(fd1,outpipe,50); sleep(3); lockf(fd1,0,0); /sleep(2); exit(0); else wh

45、ile(pid2=fork() = -1); if(pid2 = 0) printf("s2n");/ if(pid1 != 0)/ wait(0);/ printf("s22n"); lockf(fd1,1,0); /printf("f1"); sprintf(outpipe,"I am the 2nd!"); write(fd1,outpipe,50); sleep(2); lockf(fd1,0,0); /sleep(2); exit(0); else while(pid3=fork() = -1); if (pid3 = 0) printf("s3n"); lockf(fd1,1,0); sprintf(outpipe,"I am the 3rd!"); write(fd1,outpipe,50); sleep(2); lockf(fd1,0,0); exit(0); else wait(0); read(fd0,inpipe,50); printf("%s n",inpipe);/ printf("f1"); wait(0); read(fd0,inpi

溫馨提示

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

評(píng)論

0/150

提交評(píng)論