版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、計算機科學(xué)與工程學(xué)院實驗報告實驗課程名稱操作系統(tǒng)實驗實驗成績專業(yè)計算機科學(xué)與技術(shù)班級1507班指導(dǎo)教師簽字學(xué)號姓名羅藝博實驗報告批改時間實驗項目目錄1. 實驗一 熟悉Linux系統(tǒng)2. 實驗二 進程狀態(tài)3. 實驗三 進程同步和通信4. 實驗四 進程的管道通信5. 實驗五 頁面置換算法實驗報告正文實驗一 熟悉Linux系統(tǒng)一、 實驗?zāi)康氖煜ず驼莆誏inux系統(tǒng)基本命令,熟悉Linux編程環(huán)境,為以后的實驗打下基礎(chǔ)。二、 實驗原理基于linux系統(tǒng)的基礎(chǔ)操作三、 實驗內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴展點等)啟動、退出、ls(顯示目錄內(nèi)容)、cp(文件或目錄的復(fù)制)、mv(文件、目錄更名或移動)、rm
2、(刪除文件或目錄)、mkdir(創(chuàng)建目錄)、rmdir(刪除空目錄)、cd(改變工作目錄)C語言編輯、編譯四、 實驗結(jié)果(截圖)ls mkdir:cd:rmdir:實驗二 進程狀態(tài)一、 實驗?zāi)康淖孕芯幹颇M程序,通過形象化的狀態(tài)顯示,使學(xué)生理解進程的概念、進程之間的狀態(tài)轉(zhuǎn)換及其所帶來的PCB內(nèi)容 、組織的變化,理解進程與其PCB間的一一對應(yīng)關(guān)系。二、 實驗原理1.進程在內(nèi)存中存在三種基本狀態(tài):就緒態(tài)、執(zhí)行態(tài)、阻塞態(tài)2.三種狀態(tài)在滿足某種條件時會發(fā)生轉(zhuǎn)換:就緒運行:調(diào)度程序選擇一個新的進程運行運行就緒:運行進程用完了時間片運行進程被中斷,因為一高優(yōu)先級進程處于就緒狀態(tài)運行阻塞:當(dāng)一進程等待某一事
3、件的發(fā)生時,如請求系統(tǒng)服務(wù); 初始化I/O 且必須等待結(jié)果; 無新工作可做; 等待某一進程提供輸入 (IPC)阻塞就緒:當(dāng)所等待的事件發(fā)生時三、 實驗內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴展點等)#include #include #include #include /for sort in vectorusing namespace std;class Pro /process classpublic: char name; string status;vector ru,re,bl; /ru-running,re-ready,bl-blocked/function declarationint h
4、elloUI();int iniQ();int showPCB();int ruTOre();int ruTObl();int blTOre();int neTOre();int ruTOex();int helloUI() /start UI cout Hello!Welcome to come back. endl; cout #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*# endl; cout endl; cout endl; return 0;int iniQ() /initialize the process int i; cout Please en
5、ter processes names and their status. endl; cout endl; for(i=0; i5; i+) /15 process a to o Pro process; char nam; string sta; cout Please enter i processes names. nam; = nam; cout Please enter processes status. endl; cout Status contains r1(running),r2(ready) and b(blocked). sta; proces
6、s.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(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 run
7、ning:; for(i=0; iru.size(); i+) cout ,; cout endl; cout ready:; for(i=0; ire.size(); i+) cout ,; cout endl; cout blocked:; for(i=0; ibl.size(); i+) cout ,; cout endl; return 0;int ruTOre() if(!ru.empty() /runningQueue is being used. re.push_back(ru.front(); /runnings first
8、 process go to readylast ru.erase(ru.begin(); /delete runnings first process ru.push_back(re.front(); re.erase(re.begin(); else cout 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
9、_back(re.front(); re.erase(re.begin(); else cout Error in ruTObl1. endl; else cout Error in ruTObl2. running ru.push_back(re.front(); re.erase(re.begin(); else cout Error in blTOre endl; showPCB(); return 0;int neTOre() int i; cout Please enter processes names and their status. endl; cout endl; Pro
10、process; char nam; string sta; cout Please enter processes names. 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.era
11、se(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 initialize the process. endl; iniQ(); while(1) cout endl; cout Please select t
12、he action to take. endl; cout ready endl; cout blocked endl; cout ready endl; cout ready endl; cout exit act; if(act = 2) ruTOre(); else if(act = 3) ruTObl(); else if(act = 4) blTOre(); else if(act = 5) neTOre(); else if(act = 6) ruTOex(); else cout Error in select.endl; cout # endl; cout endl; retu
13、rn 0;拓展點:五狀態(tài)模型四、 實驗結(jié)果(截圖)創(chuàng)建進程:狀態(tài) runningready:狀態(tài) runningblocked:狀態(tài) blockedready:創(chuàng)建新進程:情況一 有進程正在運行情況二 無進程正在運行終止進程:實驗三 進程同步和通信一、實驗?zāi)康恼{(diào)試、修改、運行模擬程序,通過形象化的狀態(tài)顯示,使學(xué)生理解進程的概念,了解同步和通信的過程,掌握進程通信和同步的機制,特別是利用緩沖區(qū)進行同步和通信的過程。通過補充新功能,使學(xué)生能靈活運用相關(guān)知識,培養(yǎng)創(chuàng)新能力。二、 實驗原理假定.緩沖區(qū)可以容納8個數(shù)據(jù);因為緩沖區(qū)是有限的,因此當(dāng)其滿了時生產(chǎn)者進程應(yīng)該等待;當(dāng)消費者取走一個數(shù)據(jù)后,應(yīng)喚醒
14、正在等待的生產(chǎn)者進程;當(dāng)緩沖區(qū)空時,消費者進程應(yīng)該等待;當(dāng)生產(chǎn)者向緩沖區(qū)放入了一個數(shù)據(jù)時,應(yīng)喚醒正在等待的消費者進程。這就是生產(chǎn)者和消費者之間的同步三、 實驗內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴展點等)基礎(chǔ)內(nèi)容:編寫程序使其模擬兩個進程,即生產(chǎn)者(producer)進程和消費者(Consumer)進程工作;生產(chǎn)者每次產(chǎn)生一個數(shù)據(jù),送入緩沖區(qū)中;消費者每次從緩沖區(qū)中取走一個數(shù)據(jù)。每次寫入和讀出數(shù)據(jù)時,都將讀和寫指針加一。當(dāng)指針到達緩沖區(qū)尾,重新將指針退回起點;/*/* PROGRAM NAME: PRODUCER_CONSUMER */* This program simulates two proc
15、esses, producer which */* continues to produce message and put it into a buffer */* implemented by PIPE, and consumer which continues to get */* message from the buffer and use it. */* The program also demonstrates the synchronism between */* processes and uses of PIPE. */*/#include #include /#inclu
16、de #define PIPESIZE 8#define PRODUCER 0#define CONSUMER 1#define RUN 0 /* statu of process */#define WAIT 1 /* statu of process */#define READY 2 /* statu of process */#define NORMAL 0#define SLEEP 1#define AWAKE 2#include /Process Control block struct pcb char *name; int statu; int time; ; /* times
17、 of execution */ /Buffer struct pipetype char type; /type int writeptr; /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 -
18、number of consumer,buffer. count = 8:too many prodecers; 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 = Producer0; processCONSUMER.name = Consumer0; processP
19、RODUCER.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 run PRODUCER2.n press c to run CONSUMER.n); /PRODUCER1-product 1 new data, PRODUCER2-product 2 new data printf
20、( Press e to exit from the program.n); for(i=0;i1000;i+)in0=N; while(in0=N)scanf(%s,in); if(in0!=e&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 3) /the number of waitting producer over 4,waitqueue=4 pr
21、intf(wrong!n); continue; if(countstatu=READY; 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(countstatu=READY; runc(process,pipe,&pipetb,CONSUMER); countr-; printf(countr
22、=%dn,countr); if(countr=0)pipetb.pointc=NULL; if(in0=c&processCONSUMER.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,PRO
23、DUCER);countp-;printf(countp=%dn,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 3) printf(wrong!n); printf(PRODUCER is waiting, cant be scheduled.n); continue; countp+; printf(countp=%dn,countp); else if(in1 = 1)
24、 /producer1 if(countp 3) printf(wrong!n); printf(PRODUCER is waiting, cant be scheduled.n); continue; countp+; printf(countp=%dn,countp); printf(Look out.n);/printf(PRODUCER is waiting, cant be scheduled.n); if(in0=c&processCONSUMER.statu=WAIT) /consumer and wait if(countr 3) printf(wrong!n); printf
25、(CONSUMER is waiting, cant be scheduled.n); continue; countr+;printf(countr=%dn,countr);printf(Look out!n); /printf(CONSUMER is waiting, cant 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 *t
26、b; pt.statu=RUN; printf(run PRODUCER. 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(time+%dn,pt.time); pt.s
27、tatu=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(countreadptr; pipetb-readptr=0; tb-readptr+; tb-readptr=tb-readptr%8; printf(readptr
28、=%dn,tb-readptr); printf( use %d ,c); count-; printf(count=%dn,count); pt.time+; /printf(time+%dn,pt.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;iPIPESIZE;i+) printf
29、(- ); printf(n |);for(i=0;iPIPESIZE;i+) /pipe insideif(pipei!=0) /pipei has somethingprintf( %2d |,pipei);elseprintf( |); printf(n ); for(i=0;i=1) /number of producer =1 printf(%d PRODUCER wait ,countp); else printf(PRODUCER ready ); if(pCONSUMER.statu=WAIT)|countr=1) printf(%d CONSUMER wait ,countr
30、); else printf(CONSUMER ready ); printf(n); printf(n#n); 拓展點:設(shè)置了等待隊列的長度,并顯示等待隊列內(nèi)部情況四、 實驗結(jié)果(截圖)生產(chǎn)者:情況一 正常生產(chǎn)情況二 超過緩沖區(qū)情況三 超過等待隊列消費者:情況一 正常情況二 無產(chǎn)品可消費又開始生產(chǎn):實驗四 進程的管道通信一、 實驗?zāi)康募由顚M程概念的理解,明確進程和程序的區(qū)別;學(xué)習(xí)進程創(chuàng)建的過程,進一步認識并發(fā)執(zhí)行的實質(zhì);分析進程爭用資源的現(xiàn)象,學(xué)習(xí)解決進程互斥的方法;學(xué)習(xí)解決進程同步的方法;掌握Linux系統(tǒng)進程間通過管道通信的具體實現(xiàn)方法。二、 實驗原理1.基本概念:進程的概念;進程與程
31、序的區(qū)別;并發(fā)執(zhí)行的概念進程互斥的概念;進程通信的基本原理2.系統(tǒng)調(diào)用:設(shè)置系統(tǒng)調(diào)用號:設(shè)置多條系統(tǒng)調(diào)用命令,并賦予每條系統(tǒng)調(diào)用命令一個唯一的系統(tǒng)調(diào)用號處理系統(tǒng)調(diào)用:OS中有一張系統(tǒng)調(diào)用入口表,表中每個表目對應(yīng)一條系統(tǒng)調(diào)用命令,包含該系統(tǒng)調(diào)用自帶參數(shù)的數(shù)目、系統(tǒng)調(diào)用命令處理程序的入口地址等。OS內(nèi)核便是根據(jù)所輸入的系統(tǒng)調(diào)用號在該表中查找到相應(yīng)的系統(tǒng)調(diào)用,進而轉(zhuǎn)入它的入口地址去執(zhí)行系統(tǒng)調(diào)用程序。Linux的系統(tǒng)調(diào)用機制:通過中斷機制實現(xiàn)三、 實驗內(nèi)容(源碼、注釋、基礎(chǔ)內(nèi)容、擴展點等)#include #include #include #include #include int main() i
32、nt 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 while(pid2=fork() = -1); if(pid2 = 0) printf(s2n
33、);/ 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,inpipe,50);/ printf(f2); printf(%s n,inpipe); wait(0); read(fd0,inpipe,50); printf(%s n,inpipe); exit(0); r
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年物流與供應(yīng)鏈管理優(yōu)化合同
- 2024年滬教版七年級物理下冊月考試卷
- 2024年浙教版高二數(shù)學(xué)上冊階段測試試卷
- 2024年離異后子女撫養(yǎng)費用協(xié)議
- 醫(yī)療安全知識在英語繪本教學(xué)中的滲透
- 2025中國鐵路北京局集團招聘全日制普通高校畢業(yè)生140人(二)高頻重點提升(共500題)附帶答案詳解
- 2025中國郵政集團江蘇分公司春季招聘高頻重點提升(共500題)附帶答案詳解
- 2025中國系統(tǒng)校園招聘360人(寒假專場)高頻重點提升(共500題)附帶答案詳解
- 2025中國原子能科學(xué)研究院回旋加速器研究設(shè)計中心校園招聘高頻重點提升(共500題)附帶答案詳解
- 2025中共江蘇省委黨校(江蘇行政學(xué)院)公開招聘專業(yè)技術(shù)人員10人高頻重點提升(共500題)附帶答案詳解
- 電網(wǎng)行業(yè)工作匯報模板22
- 2024年-江蘇省安全員-A證考試題庫及答案
- 2024年青干班培訓(xùn)個人總結(jié)
- 2021~2022學(xué)年廣東廣州越秀區(qū)八年級上學(xué)期期末語文試卷(含答案)
- 固態(tài)電池生產(chǎn)(1GWH)項目可行性研究報告模板-立項拿地
- 中建一期工程履帶吊安拆方案
- 廣東省深圳市坪山區(qū)2024學(xué)年七年級上學(xué)期期末數(shù)學(xué)試題【含答案】
- 2024游樂新“室”界室內(nèi)樂園洞察與趨勢研究報告
- 2024-2025學(xué)年一年級數(shù)學(xué)上冊期末樂考非紙筆測試題(二 )(蘇教版2024秋)
- 辦公樓電氣改造施工方案
- 浙江省衢州市2023-2024學(xué)年高一上學(xué)期期末英語試題(含答案)3
評論
0/150
提交評論