




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、標(biāo)準(zhǔn)操作系統(tǒng)實(shí)驗(yàn)報(bào)告(LinUX版)網(wǎng)絡(luò)142 潘豹142999文案實(shí)驗(yàn)一觀察LinUX進(jìn)程狀態(tài)一、實(shí)驗(yàn)?zāi)康脑诒緦?shí)驗(yàn)中學(xué)習(xí)LinUX操作系統(tǒng)的進(jìn)程狀態(tài),并通過(guò)編寫(xiě)一些簡(jiǎn)單代碼來(lái)觀察各種情況 下,LinUX進(jìn)程的狀態(tài),進(jìn)一步理解進(jìn)程的狀態(tài)及其轉(zhuǎn)換機(jī)制。二、實(shí)驗(yàn)環(huán)境硬件環(huán)境:計(jì)算機(jī)一臺(tái),局域網(wǎng)環(huán)境;軟件環(huán)境:LinUX UbUntu 操作系統(tǒng),gcc編譯器。(四)查看不可中斷阻塞”狀態(tài)(D)創(chuàng)建一個(gè) C程序,如Uninter_status.c,讓其睡眠30s代碼:#in ClUdev Uni std.h>#in cUde<stdio.h>int mai n()int i=0,j=
2、0,k=0;for(i=0;i<1000000;i+) for(j=0;j<1000000;j+)k+;k-;實(shí)驗(yàn)結(jié)果:0 StUdentt: 桌面StUdetlab-soft:IS“exanples i desktop 模扯 視類 圖月 下H 音樂(lè) 桌面StUdentlab-soft:$ Cd 桌面StUdenttab-soft:桌面$ gcc 1 * 1 .c -o 1.1StudentfLab -SOft:-/桌面$ ,/1.1 & PS1 3821PIDTTYTlMECMD3757pts/109:00:00bash3821ptl1*13822pts/1pStuden
3、tlab-SOft:桌而$ |(二)查看“暫停”狀態(tài)(T)運(yùn)行rUn_StatUS進(jìn)程,其進(jìn)入 R狀態(tài): 代碼同上:(三)查看 可中斷阻塞”狀態(tài)(S)創(chuàng)建一個(gè) C程序,如interrUptiblie_StatUS.c ,讓其睡眠 30s編譯鏈接,后臺(tái)運(yùn)行該程序(后接 &符號(hào)),并使用PS命令查看運(yùn)行狀態(tài) 代碼:#in cUde< Uni std.h>#in cUde<stdio.h> int mai n()SleeP(30); return;實(shí)驗(yàn)結(jié)果:pan©Pdn-Y400; Zpanpan-V4G0 gcc 1.2»C -o 1÷
4、;21,2,Ct Tn function 'nain":1.2»c :6:3 ; warning- * returnj With no VaILlet in functior returning non -VOid return;PanePan-Y40: $ 1.2 SPS1 452Z PID TT¥TlWE CMDash 2PS4566pt2fl00:00:004522 pts2盹:盹:朋4523 pts/20e:ee;eePanePan-Y4iS (四)查看不可中斷阻塞”狀態(tài)(D)創(chuàng)建一個(gè) C程序,如Uninter StatUS.c,讓其睡眠30s編譯
5、鏈接,后臺(tái)運(yùn)行該程序(后接 &),并使用PS命令查看運(yùn)行狀態(tài) 代碼:#in clude< Uni std.h>#in ClUdeVStdio.h>int mai n()if(vfork()=0) Seep(300); return;實(shí)驗(yàn)結(jié)果:靜第芒 pan©Pan-Y400: «par0pa*gcc 1.4, C LO 1 i 4PanePan-Y,40:*> 1,4 aPSL 4445PlD TTYTlMECMD4425 pts/19&6Qbash4445 pts/19&0:00;01,44446 pts/19&9:
6、&0:60PS4447 pts/1999:09:601.4PanSPan-Y,4G = I(五)查看 僵尸”進(jìn)程(Z)創(chuàng)建一個(gè)C程序,如ZOmbie_StatUS.c,在其中創(chuàng)建一個(gè)子進(jìn)程,并讓子進(jìn)程迅速結(jié)束, 而父進(jìn)程陷入阻塞編譯鏈接,后臺(tái)運(yùn)行該程序(后接&),并使用PS命令查看運(yùn)行狀態(tài)(30s內(nèi))代碼:#in clude< Uni Std.h>#in cldUe<Stdio.h>int mai n()if(fork()SleeP(300); 實(shí)驗(yàn)結(jié)果:令魯嵋 Pan(Pan-Y400:Parl> Y400 :»$ gcc 1,5, C
7、1÷5P30p3-Yh4O:-S1.5 &1 4232PID TT¥TlME4214 pts/O4232 pts/O;00:fid4233 pts/O4234 pts90:00:60paSpan-Yh40z-SIPSCMDZsh1.5PS1-5 <defunct>實(shí)驗(yàn)二觀察LinUX進(jìn)程/線程的異步并發(fā)執(zhí)一、實(shí)驗(yàn)?zāi)康耐ㄟ^(guò)本實(shí)驗(yàn)學(xué)習(xí)如何創(chuàng)建 LinUX進(jìn)程及線程,通過(guò)實(shí)驗(yàn),觀察 LinUX進(jìn)程及線程的異步 執(zhí)行。理解進(jìn)程及線程的區(qū)別及特性, 進(jìn)一步理解進(jìn)程是資源分配單位,線程是獨(dú)立調(diào)度單位。二、實(shí)驗(yàn)環(huán)境硬件環(huán)境:計(jì)算機(jī)一臺(tái),局域網(wǎng)環(huán)境;軟件環(huán)境:LinUX
8、 UbUntu 操作系統(tǒng),gcc編譯器。三、實(shí)驗(yàn)內(nèi)容和步驟1進(jìn)程異步并發(fā)執(zhí)行編寫(xiě)一個(gè)C語(yǔ)言程序,該程序首先初始化一個(gè) count變量為1 ,然后使用fork函數(shù)創(chuàng) 建兩個(gè)子進(jìn)程, 每個(gè)子進(jìn)程對(duì) CoUnt力口 1后,顯示"I amson, count=x ”或“I amdaughter, CoUnt=x ” ,父進(jìn)程對(duì) count 力口 1 之后,顯示"I am father, count=x” ,其中 X 使用 count值代替。最后父進(jìn)程使用 WaitPid等待兩個(gè)子進(jìn)程結(jié)束之后退出。編譯連接后,多次運(yùn)行該程序,觀察屏幕上顯示結(jié)果的順序性,直到出現(xiàn)不一樣的情 況為止,并
9、觀察每行打印結(jié)果中count的值。代碼:#in clude< Uni std.h>#in clude<stdio.h>int mai n()pid_t SOn _pid,daughter_pid; int coun t=1;SOn _pid=fork();if(son _pid=O)COUn t+;Prin tf("i am son,coun t=%d n",co Un t);elsedaughter_pid=fork();if(daughter_pid=0)COUn t+;Prin tf("i am daughter,co Un t=%d
10、 n",co Un t);elseCOUn t+;Prin tf("i am father,co Un t=%dn",co Un t);WaitPid(SOn_pid,NULL,0);WaitPid(daughter_pid,NULL,O);Q令* PanbaaideCiPad-y4CO:屮車(chē)面 pabotdepad<y4B0: -S Cd 虞直 pt>aoidcapad-y466; gcc X ÷ C o X x.c: In function rnin,ix,C!ZSr5t earning: Vmpltctt deckaratto Of f
11、unction jwatt>td , - WtrnPItC-Lt-fuctt o-dcclarationWAltPId (SOnVPid a Ml LL , O );JRPMIbllOQideflPI>d-y400: 桌面$ JXI an father f COUnt=2i an sonlcont-2i. an daughter,cot-2PanbdO Idea pad-y3D0:$ . xi an Ilather,cout-21. art son COUritI an daughter jc0t=2 panbotdcapad-y4Q6: -/桌面帛 x i an father,C
12、OUt2 t an daughtrcojt=2 t an SOnJCOUnt=2Danbg Id 已日 utad -桌面 $ 2、線程異步并發(fā)執(zhí)行編寫(xiě)一個(gè)C語(yǔ)言程序,該程序首先初始化一個(gè) CoUnt變量為1,然后使用Pthread_Create 函數(shù)創(chuàng)建兩個(gè)線程, 每個(gè)線程對(duì)CoUnt力口 1后,顯示"I amson, count=x "或“I amdaughter, CoUnt=x ” ,父進(jìn)程對(duì) count 力口 1 之后,顯示"I am father, count=x” ,其中 X 使用 count值代替。最后父進(jìn)程使用Pthread_join等待兩個(gè)線程結(jié)
13、束之后退出。編譯連接后,多次運(yùn)行該程序,觀察屏幕上顯示結(jié)果的順序性,直到出現(xiàn)不一樣的情 況為止,并觀察每行打印結(jié)果中CoUnt的值。代碼:#in ClUdeV Uni std.h>#in CIUdeVStdio.h>#i nclude<pthread.h>void *daughter(void *num)int* a=(i nt *)num;*a+=1;Prin tf("i am daughter,co Un t=%d n",*a);void *son( void *num)int* a=(i nt *)num;*a+=1;Prin tf("
14、;i am son,coun t=%d n",*a);int mai n()Pthread_t SOn _tid,daughter_tid;int coun t=1;Pthread_Create(&son _tid,NULL,so n,&coun t);Pthread_Create(&daughter_tid,NULL,daughter, &coun t);COUn t+;Prin tf("i am Pare nt,co Un t:=%dn",co Un t);PthreadOin (son_tid,NULL);PthreadLjO
15、i n(daughter_tid,NULL);return 0;實(shí)驗(yàn)結(jié)果:6 * Penbaideapad-y4QC:亠/臬tfjtpccNFtocf.c: j.!*rat*中:bc(, text+xbj :對(duì),thred-cretefIeh文的引鋼b÷cU , tet+ecej: TPthrad-cret,未定文的引一毋b÷ct(. rext+fcI 對(duì)*rhr羽(CjOJLfI "耒宦義前引用b.ct(. et+xied):對(duì)*|rthrei3_Jntni未定義的引用 colltct2: error: Id rffttrred T exit StatUSPnb
16、ftoQidtoped400r而$ gcc -02 -WalI -o b b.c -Ipthreadbc: Ir function daughter,:b.c:»:l: WOriIirq: COntrQl reaches tnd of o votd function retur' type b+匸;In function * SPn t;b«c:lS;l: »dnlnq: ctrg I -EdthEL end f 4hi -VOid function -WretLr n- type pa DbacHBtdra pad 4ft:, /bR M PArentf
17、Count:?t a daughterPCaUht>41 SOn cont=3jp3nb.o<ldm呂tl卓面S . /b1 Jn SCIn ,count=2i an Parent,cut:=3jl An daughterpcout=4PaflhaoUBaMi->V400:*ASS 實(shí)驗(yàn)三使用信號(hào)量進(jìn)行互斥與同步一、實(shí)驗(yàn)?zāi)康谋緦?shí)驗(yàn)介紹在LinUX中使用信號(hào)量進(jìn)行進(jìn)程同步、互斥的方法。讀者可以通過(guò)實(shí)驗(yàn)進(jìn)一 步理解進(jìn)程間同步與互斥、臨界區(qū)與臨界資源的概念與含義,并學(xué)會(huì)LinUX信號(hào)量的基本使用方法。二、實(shí)驗(yàn)環(huán)境硬件環(huán)境:計(jì)算機(jī)一臺(tái),局域網(wǎng)環(huán)境;軟件環(huán)境:LinUX UbUntu
18、操作系統(tǒng),gcc編譯器。三、實(shí)驗(yàn)內(nèi)容和步驟三、實(shí)驗(yàn)內(nèi)容和步驟(一)參考:PoSlX 以及 SyStem VSyStem V: UniX眾多版本中的一支,最初由 AT&T定義,目前為第四個(gè)版本,其中定 義了較為復(fù)雜的 API。POSIX : POrtabIe OPerating SyStem Interface , IEEE 為了統(tǒng)一 UniX 接口而定義的標(biāo)準(zhǔn), 定義了統(tǒng)一的 API接口。LinUX即支持SyStem API ,又支持 POSIX API(二)實(shí)驗(yàn)步驟step1:通過(guò)實(shí)例查看不使用互斥時(shí)的情況(假設(shè)文件命名為n o_sem.c)編譯鏈接,同時(shí)運(yùn)行兩個(gè)進(jìn)程,顯示結(jié)果代碼:
19、#in ClUdevStdio.h>#in cUde<Stdlib.h>int main (i nt argc,char *argv)Char message='x'int i=0;if(argc>1)message=argv10;for(i=0;i<10;i+)Prin tf("%c",message); fflush(stdout);sleep(ra nd()%3);Prin tf("%c",message); fflush(stdout);sleep(ra nd()%2);sleep(10);exit;
20、實(shí)驗(yàn)結(jié)果:協(xié)魯® pan®Pan-Y400:卄/事面PBnePallgedttpapA-40:gcc x.c -Q X5cc: error: x.c:沒(méi)有那個(gè)文件或目錄guc:. (. rr r o ipdt filesCOnP,Llatic te'iratedPanPan-Y4QQ:-$ Cd 面 panpan-Y4:*/面S get *.C -0 X x*c: In fuction *ai,x.c:13:2: Warning: implicit declaration Of function 'SIeeP' -WiPlPliCit -functi
21、on '(JeclerattQnSl PepCdIfil);Apap*Y4B!*面S x & ./x oi Z一 _step2:使用信號(hào)量來(lái)對(duì)臨界資源進(jìn)行互斥(假設(shè)文件命名為With_sem.c)編譯鏈接,同時(shí)運(yùn)行兩個(gè)進(jìn)程。觀察X和O的出現(xiàn)規(guī)律,并分析原因。代碼:#in clude<stdio.h>#in clude<stdlib.h>#in ClUde<systypes.h>#in clude<sys/ipc.h>#in clude<semaphore.h>#in clude<fc ntl.h>#in c
22、lude<sys/stat.h>int main (i nt argc,char *argv)Char message='x'int i=0;if(argc>1)message=argv10;Sem_t *mutex=sem_ope n("mysem",O_CREAT,0666,1);for(i=0;i<10;i+) sem_wait(mutex);Prin tf("%c",message); fflush(stdout);sleep(ra nd()%3);Prin tf("%c",messag
23、e); fflush(stdout);Sem_post(mutex); sleep(ra nd()%2);sleep(10);Sem_close(mutex);Sem_u nil nk("mysem"); exit(0);實(shí)驗(yàn)結(jié)果:PanOPan-Y400: EFjll面x,c:: In function >vn'x÷c: 22:2; arn<5 : inplicit declaration Of function *SIeeP, l - WinPlci t-fctinr 'declarationsieep(rad0V3);:C
24、47; tcxt+xj1b): ,6CH OlKnf 未定義的引用-'- 一丄一.,-S-誦;C text+Gdfr)I 5,senpost,耒定叉的引用'未宦義的弓用 x*c: C < xt4izd: ,sen ULnk'未定文的引用 collect?: error T Id retr ed 1 exit StdtUStpcCAkHkx?.¢1fnai*中; I _x.c; ”=_ F ,c : C + tet+67× ,aeFt-Wsit*未定義的弓 M.cI.tt+l23 對(duì)*5F-ClS?PanPar-Y4C0: Ti5 cc Lpth
25、reod .c -o X gcc-Ipthcead:未找到命會(huì)Panpar*-V4Q0:-/貞T1$ gcc x<c -IPthreaCl o K .c: In function falt:k<:22:2: MJrning: VnpKcit decIaratLor Of function , SIeeP , l WinPIiCit-function -IjeeIJratlOnJSIJeeP(rand O K3 JPMeP>n-Y4S0i-面$ & x oJ×1 9692XXCOXXOCXXOXOOXXOOXXOOXXOXXOOXXDOKCl)+ 巳完成Pan
26、$Pan-Y40OrJS |step3 :使用信號(hào)量來(lái)模擬下象棋紅黑輪流走子的情況編寫(xiě)兩個(gè)C語(yǔ)言程序black_chess.c以及red_chess.c,分別模擬下象棋過(guò)程中紅方走子 和黑方走子過(guò)程。走子規(guī)則:紅先黑后,紅、黑雙方輪流走子,到第10步,紅方勝,黑方輸。代碼: 紅色棋#in clude<stdio.h> #in clude<stdlib.h>#in ClUde<systypes.h>#in clude<sys/ipc.h>#in clude<semaphore.h>#in clude<fc ntl.h>#in
27、 ClUde<sysstat.h>int main (i nt argc,char *argv)int i=0;Sem_t *hei=sem_ope n("chess_black_sem",O_CREAT,0666,1);Sem_t *ho ng=sem_ope n("chess_red_sem",0_CREAT,0666,0); for(i=0;i<10;i+)Sem_wait(hei);if(i!=9)Prin tf("Red CheSS had moved,black,chess go!n");elsePrin
28、 tf("Red CheSS win!n");fflush(stdout);Sem_post(h On g);sleep(10);Sem_close(hei);Sem_close(h On g);Sem_ Un Ii nk("chess_red_sem");Sem_ Un Ii nk("chess_black_sem");exit(0);黑色棋:#in clude<stdio.h>#in clude<stdlib.h>#in clude<sys/types.h>#in clude<sys/ip
29、c.h>#in clude<semaphore.h>#in clude<fc ntl.h>#in clude<sys/stat.h>int main (i nt argc,char *argv)int i=0;Sem_t *hei=sem_ope n("chess_black_sem",O_CREAT,0666,1);Sem_t *ho ng=sem_ope n("chess_red_sem",0_CREAT,0666,0); for(i=0;i<10;i+)Sem_wait(h On g);if(i!=9
30、)Prin tf("Black CheSS had moved,red CheSS go!n");elsePrintf("Black CheSS win!n");fflush(stdout);Sem_post(hei);sleep(10);Sem_close(hei);Sem_close(h On g);Sem_ Un Ii nk("chess_red_sem");Sem _un Ii nk("chess_black_sem"); exit(0);實(shí)驗(yàn)結(jié)果:pan®Pan-Y400:呼臬面 pan0pan
31、*Y466:*/桌面$ red ./black 1 3677Red chess had moved,black,chess go! BIark chess had moved, red Che-SS go! Red CheSS had movedJbla匚k*chef55 go! 6Lack 匚hess had PlOVedJred CheSS go! Red CheSS had moved J black,chess go! BIaCk CheSS had (TlOVed,red 匚hess go! Red CheSS had moved ,black,che go! BIaCk chess
32、 had moved電d CheSS go! Reel CheSS hd IlIQVed J black,ches5 go ! BlaCk CheSS had PlOVedJred CheSS go! Red CheSS had movedfblack,chess go! BIaCk 匚hes had movedFred chess go! Red CheSS had movedtbiack,chess go! BLaCk chess had moved,red ChfrSS go! Red CheSS hd moved,black,chess go! BIaCk 匚hess had oved
33、,red CheSS go! Red CheSS had moved t black, ch ess go! BlaCk CheSS had moved,red CheSS goJ Red chess Win!Black CheSS Wini1+已完成./ redPan(JPan-?4fie:/桌面$ I實(shí)驗(yàn)四作業(yè)調(diào)度算法模擬、實(shí)驗(yàn)?zāi)康?1) 掌握周轉(zhuǎn)時(shí)間、等待時(shí)間、平均周轉(zhuǎn)時(shí)間等概念及其計(jì)算方法。(2) 理解五種常用的進(jìn)程調(diào)度算法(FCFS、SJF、HRRF、HPF、RR),區(qū)分算法之間 的差異性,并用C語(yǔ)言模擬實(shí)現(xiàn)各算法。(3) 了解操作系統(tǒng)中高級(jí)調(diào)度、中級(jí)調(diào)度和低級(jí)調(diào)度的區(qū)別和聯(lián)系。、實(shí)
34、驗(yàn)環(huán)境硬件環(huán)境:計(jì)算機(jī)一臺(tái),局域網(wǎng)環(huán)境;軟件環(huán)境:LinUX UbUntu操作系統(tǒng),gcc編譯器。、實(shí)驗(yàn)內(nèi)容和步驟本實(shí)驗(yàn)所選用的調(diào)度算法均默認(rèn)為非搶占式。實(shí)驗(yàn)所用的測(cè)試數(shù)據(jù)如下表所示。本實(shí)驗(yàn)所用的測(cè)試數(shù)據(jù)如下表所示表實(shí)驗(yàn)測(cè)試數(shù)據(jù)作業(yè)Id到達(dá)時(shí)間執(zhí)行時(shí)間優(yōu)先權(quán)180050028153013830252483520258451526700101782050作業(yè)的數(shù)據(jù)結(jié)構(gòu):typedef StrUCt nodeint number; / 作業(yè)號(hào)int reach_time;作業(yè)抵達(dá)時(shí)間 int need_time;作業(yè)的執(zhí)行時(shí)間 int privilege;/作業(yè)優(yōu)先權(quán) float excellen
35、t; 響應(yīng)比 int Start_time;/作業(yè)開(kāi)始時(shí)間 int Wait_time;/ 等待時(shí)間 int visited;/作業(yè)是否被訪問(wèn)過(guò) bool isreached;/作業(yè)是否已經(jīng)抵達(dá) job;代碼:#i nclude <stdio.h>#in clude <stri ng.h> #i nclude <stdlib.h> #in clude<stdbool.h>/最大作業(yè)數(shù)量ConStint MAXJoB=50;/作業(yè)的數(shù)據(jù)結(jié)構(gòu)typedef StrUCt nodeint number;/ 作業(yè)號(hào)int reach_time;作業(yè)抵達(dá)時(shí)
36、間int need_time;/作業(yè)的執(zhí)行時(shí)間int privilege;/作業(yè)優(yōu)先權(quán)float excellent;/ 響應(yīng)比int Start_time;/作業(yè)開(kāi)始時(shí)間int Wait_time;/ 等待時(shí)間int visited;/作業(yè)是否被訪問(wèn)過(guò)bool isreached;/作業(yè)是否抵達(dá)job;job jobs50;/ 作業(yè)序列 int quantity;/ 作業(yè)數(shù)量/初始化作業(yè)序列void in itial_jobs()int i;for(i=0;i<MAXJOB;i+)jobsi. nu mber=0; jobsi.reach_time=0; jobsi.privilege
37、=0; jobsi.excelle nt=O; jobsi.start_time=0; jobsi.wait_time=0; jobsi.visited=0; jobsi.isreached=false;qua ntity=O;/重置全部作業(yè)信息Void resetnfo()int i;for(i=0;i<MAXJOB;i+) jobsi.start_time=0;jobsi.wait_time=0;jobsi.visited=0;/查找當(dāng)前CUrrent_time已到達(dá)未執(zhí)行的最短作業(yè),若無(wú)返回-1 int fin dminjob(job jobs,i nt CoUnt)int min
38、job=-1;/=jobs0. need_time;int min IoC=-1;int i;for(i=0;i<co Un t;i+)if(mi nloc=-1)if( jobsi.isreached=true && jobsi.visited=0)minjob=jobsi. need_time;min loc=i;else if(minjob>jobsi. need_time&&jobsi.visited=0&&jobsi.isreached=true)minjob=jobsi. need_time;min loc=i;retur
39、n mi nloc;/查找最早到達(dá)作業(yè),若全部到達(dá)返回-1.int fin drearlyjob(job jobs,i nt CoUnt)int rearlyloc=-1;int rearlyjob=-1;int i;for(i=0;i<co Un t;i+)if(rearlyloc=-1)if(jobsi.visited=0)rearlyloc=i;rearlyjob=jobsi.reach_time;else if(rearlyjob>jobsi.reach_time&&jobsi.visited=0)rearlyjob=jobsi.reach_time;rea
40、rlyloc=i;return rearlyloc;ZZ讀取作業(yè)數(shù)據(jù)void readJobdata()FlLE *fp;Char fname20;int i;ZZ輸入測(cè)試文件文件名Prin tf("please in PUt job data file n amen");SCa nf("%s",fname);if(fp=fope n(fname,"r")=NULL)Prin tf("error, ope n file failed, PIeaSe CheCk file name:' n");elseZZ依
41、次讀取作業(yè)信息while(!feof(fp)if(fscanf(fp,"%d %d %d %d",&jobsquantity.number,&jobsquantity.reach_time,&jobsquantity. n eed_time,&jobsqua ntity.privilege)=4)qua ntity+;ZZ打印作業(yè)信息Prin tf("output the orig in job data' n");Printf("'n");Prin tf("tjoblDtre
42、achtimet needtimetprivilegen");for(i=0;i<qua ntity;i+)Prin tf("t%-8dt%-8dt%-8dt%-8d n",jobsi. number,jobsi.reach_time,jobsi. need_time,jobsi .privilege);/FCFSvoid FCFS()int i;int CUrre nt_time=0;in t loc;int total_Waitime=0;int total_r OUn dtime=0;/獲取最近到達(dá)的作 業(yè)loc=fi ndrearlyjob(jobs
43、,qua ntity);/輸出作業(yè)流Printf("nnFCFS 算法作業(yè)流 n");Printf("n");Prin tf("tjoblDtreachtimetstarttimetwaittimetro Un dtimen");CUrre nt_time=jobsloc.reach_time;/每次循環(huán)找出最先到達(dá)的作業(yè)并打印相關(guān)信息for(i=0;i<qua ntity;i+)if(jobsloc.reach_time>curre nt_time) jobsloc.start_time=jobsloc.reach_ti
44、me;CUrre nt_time=jobsloc.reach_time;elsejobsloc.start_time=CUrre nt_time;jobsloc.wait_time=CUrre nt_time-jobsloc.reach_time;Prin tf("t%-8dt%-8dt%-8dt%-8dt%-8dn",loc+1,jobsloc.reach_time,jobsloc.start_time,jobs loc.wait_time,jobsloc.wait_time+jobsloc .n eed_time);jobsloc.visited=1;CUrre nt_
45、time+=jobsloc .n eed_time;total_Waitime+=jobsloc.wait_time;total_r OUn dtime=total_r OUn dtime+jobsloc.wait_time+jobsloc .n eed_time;ZZ獲取剩余作業(yè)中最近到達(dá)作業(yè)loc=fi ndrearlyjob(jobs,qua ntity);Printf("總等待時(shí)間 %8d 總周轉(zhuǎn)時(shí)間:%-8dn",tOtaLWaitime,total_roundtime);Printf(“平 均 等 待 時(shí) 間 :%4.2f 平 均 周 轉(zhuǎn) 時(shí)間:%4.2fn&q
46、uot;,(float)tOtaLWaitime/(quantity),(float)tOtaLroUndtime(quantity);/短作業(yè)優(yōu)先作業(yè)調(diào)度void SFJSChdUlejob(job jobs,i nt CoUnt)int mai n()in itial_jobs();readJobdata();FCFS();reset_ji nfo();SFJSChdUlejob(jobs,qua ntity);SyStem("pause");return 0;實(shí)驗(yàn)結(jié)果:ll>d-r*tvP r - J LU 31CmI *a*Mfr-Y4M 1gcc aX -o
47、 aPaIltPall-Y4t-jK$ ÷aPIeaSe input job dntE file nameSSQUtPUt the Origin iob ClataJOblDreachtt IlgSoo81Sdttn503025201519prttlrege012221820C5S作業(yè)流JObID總等待:3Ie 平均等待時(shí)間: Eh: 1: pae:reachte reo 8Q& 815 820 830 釘5 845Starttlrle79aoe850ae8BSl693G總同轉(zhuǎn)時(shí)q = us44.29平均周轉(zhuǎn)時(shí)間:斫,43WaItttne&S3S備 SS7505rou
48、nctinel56S 砧809S16HOt found實(shí)驗(yàn)五 LinUX內(nèi)存分配一、實(shí)驗(yàn)?zāi)康?、 了解LinUX內(nèi)存的分配和虛擬內(nèi)存管理的原理,學(xué)習(xí)如何使用malloc動(dòng)態(tài)申請(qǐng)內(nèi)存, 了解動(dòng)態(tài)申請(qǐng)與靜態(tài)申請(qǐng)的區(qū)別。2、深入理解LinUX的內(nèi)存布局:代碼段、數(shù)據(jù)段、 BSS段、堆棧段、堆二、實(shí)驗(yàn)環(huán)境硬件環(huán)境:計(jì)算機(jī)一臺(tái),局域網(wǎng)環(huán)境;軟件環(huán)境:LinUX UbUntu 操作系統(tǒng),gcc編譯器。三、實(shí)驗(yàn)內(nèi)容和步驟第一部分:編程分析 LinUX中內(nèi)存動(dòng)態(tài)申請(qǐng)與內(nèi)存靜態(tài)申請(qǐng)的區(qū)別要求:1、 編寫(xiě)一個(gè)LinUX C程序,在該程序中使用定義一個(gè)全局變量,以及使用malloc申請(qǐng)一段內(nèi)存(可大于物理內(nèi)存大小,
49、理論上在32位系統(tǒng)中可以申請(qǐng)小于 3G空間,但由于malloc 要求申請(qǐng)連續(xù)的空間,所以能申請(qǐng)到的空間一般在2G以下)。2、 打印出全局變量的起始地址,以及malloc分配的內(nèi)存的地址;3、觀察運(yùn)行結(jié)果,并分析說(shuō)明結(jié)果的地址是否是物理地址。代碼:#in ClUdevStdio.h>#in clUde<stdlib.h>in t Pin t128;int mai n()char* PbUf=(Char *)malloc(sizeof(char)*1024*1024*1024);if(p in t)Prin tf("the address of Pint is %xn
50、",p in t);if(pbuf)Prin tf("the address of PbUf is %xn ",pbuf);if(pbuf)free(pbuf);PbUf=NULL;return 0;實(shí)驗(yàn)結(jié)果:PanPan-Y400:彳桌面PaPan-Y4:-$ 9cc z÷c -G Zgcc;z.c:沒(méi)有那于文杵或目錄9cc; f*l C rrr : no InPUIt filescpilti teriatd,panpan-Y40O Cd 臬面PanePan*Y46:gcc ZkC -a Zztc: In function dnair,ZkC:9:1
51、E w rrtr g: for31 f%x, exPeCtS arqunent Of type CUnSLgned intj f but arquent 2 hNE type ,lnt *, -wfr13t=PrIntf(Ilth總 JdcIr*ss Of Ptrt t xn, JPtntJAZ.c: 13:10: drig : FOrnat H ! , eIPeCti a rgert f type i uigMl Lftt* I but srgiEent 2 has type 'MmL *, *Wfor*t=JPrtntf("the address Of PbUf is x
52、n, fpbuf);APanPBn-Yqee tHS JZthe address Of Plnt is 61D6Othe ddd es Of PbLlF IS 24cellp2<鋰pn¥40B桌而,第二部分:進(jìn)一步了解LinUX內(nèi)存的分配和虛擬內(nèi)存管理的原理、了解LinUX的內(nèi)存布局:代碼段、數(shù)據(jù)段、BSS段、堆棧段、堆。要求:1、編寫(xiě)一個(gè)LinUX C程序,在該程序中定義初始化全局變量、未初始化全局變量、初始化靜態(tài)變量、未初始化靜態(tài)變量、局部變量、使用malloc分配的局部變量2、打印出各種變量的的起始地址代碼:#i nclude <stdio.h>#includ
53、e <stdlib.h> /定義兩個(gè)初始化的全局變量int data_varO = 10;int data_var1 = 10; /定義兩個(gè)未初始化的全局變量int bss_varO;int bss_var1;int mai n()/分別定義一個(gè)初始化和一個(gè)未初始化的靜態(tài)變 量StatiC int data_var2 = 10;StatiC int bss_var2;定義兩個(gè)局部變 量int StaCk_var0 = 1;int StaCk_var1 = 1;int StaCk_var2 = 1;Printf("TEXT Segmentn");Prin tf(&
54、quot;Address of main: %pn", main);Printf("DATA Segmentn");Prin tf("Address of data_var0: %pn", &data_varO);Prin tf("Address of data_var1: %pn", & data_var1);Prin tf("Address of data_var2: %pn", &data_var2);Printf("BSS Segmentn");Prin
55、 tf("Address of bss_varO(BSS Segme nt): %pn", & bss_var0);Prin tf("Address of bss_VarI(BSS Segme nt): %pn", & bss_var1);Prin tf("Address of bss_var2(BSS Segme nt): %pn", & bss_var2);Printf("STACK Segmentn");Prin tf("Address of StaCk_var0: %pn&
56、quot;, &stack_var0);Prin tf("Address of StaCk_var1: %pn", &stack_var1);Printf("Address of StaCk_var2: %pn", &stack_var2); / 使用 malloc 分配三個(gè)大小為1024B的內(nèi)存char* heap_var0 = (Char*)malloc(1024);char* heap_var1 = (Char*)malloc(1024);char* heap_var2 = (Char*)malloc(1024); / 使用 malloc 分配三個(gè)大小為 512MB 的內(nèi)存char* mmap_var
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 國(guó)際貿(mào)易實(shí)務(wù)模擬試題(附答案)
- 養(yǎng)殖業(yè)廢棄物處理服務(wù)合同
- 社區(qū)合作社種植產(chǎn)品收購(gòu)與銷(xiāo)售協(xié)議
- 企業(yè)年度銷(xiāo)售代理協(xié)議
- 家用電器購(gòu)買(mǎi)安裝合同
- 2025陜西西鳳露酒有限公司招聘(38人)筆試參考題庫(kù)附帶答案詳解
- 2025年山東土地資本投資集團(tuán)有限公司春季社會(huì)招聘(10人)筆試參考題庫(kù)附帶答案詳解
- 2025年中石化蕪湖石油分公司招聘8人筆試參考題庫(kù)附帶答案詳解
- 2025中儲(chǔ)糧集團(tuán)黑龍江分公司招聘165人查看職位筆試參考題庫(kù)附帶答案詳解
- 紡織品設(shè)計(jì)師證書(shū)考試解析及試題
- 鑄就數(shù)字堅(jiān)盾網(wǎng)絡(luò)安全技術(shù)知到課后答案智慧樹(shù)章節(jié)測(cè)試答案2025年春青島工學(xué)院
- (高清版)JTGT 3650-01-2022 公路橋梁施工監(jiān)控技術(shù)規(guī)程
- 中國(guó)歷史地理智慧樹(shù)知到期末考試答案章節(jié)答案2024年北京大學(xué)
- MOOC 跨文化交際通識(shí)通論-揚(yáng)州大學(xué) 中國(guó)大學(xué)慕課答案
- API520-安全閥計(jì)算PART1(中文版)
- 八年級(jí)期末質(zhì)量分析-課件
- 10000中國(guó)普通人名大全
- 費(fèi)森4008s常見(jiàn)故障排除
- 積極心態(tài)與消極心態(tài)
- 特種設(shè)備檢查記錄
- 《土地寶懺》2019版定稿
評(píng)論
0/150
提交評(píng)論