操作系統(tǒng)進程管理_第1頁
操作系統(tǒng)進程管理_第2頁
操作系統(tǒng)進程管理_第3頁
操作系統(tǒng)進程管理_第4頁
操作系統(tǒng)進程管理_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、深 圳 大 學(xué) 實 驗 報 告課程名稱:操作系統(tǒng)實驗項目名稱:進程管理學(xué)院:計算機與軟件學(xué)院專業(yè):軟件工程指導(dǎo)教師:報告人:學(xué)號:班級:實驗時間:實驗報告提交時間:教務(wù)處制一、實驗?zāi)康呐c要求:實驗?zāi)康模和ㄟ^進程的創(chuàng)建、 撤銷和運行加深對進程概念和進程并發(fā)執(zhí)行的理解,明確進程與程序之間的區(qū)別。實驗要求:1、 掌握在 windows 中編程,在cygwin 中編譯運行的方法2、 閱讀例程,理解函數(shù)fork() 、execl()、exit()、 getpid()和 waitpid() 的功能和用法3、 運行例程,分析例程中關(guān)鍵代碼的功能,給出運行結(jié)果并對運行結(jié)果進行分析說明。4、 模仿例程,編寫一段

2、程序?qū)崿F(xiàn)以下功能:a) 父進程使用系統(tǒng)調(diào)用fork() 創(chuàng)建兩個子進程b) 各個子進程顯示和輸出一些提示信息和自己的進程標(biāo)識符。c) 父進程顯示自己的進程id 和一些提示信息, 然后調(diào)用 waitpid() 等待多個子進程結(jié)束,并在子進程結(jié)束后顯示輸出提示信息表示程序結(jié)束。5、 父進程創(chuàng)建多個 (3 個以上) 進程并發(fā)運行, 控制好各個子進程輸出自己的進程標(biāo)識符和一些提示信息,對程序運行結(jié)果進行分析說明。觀察各個子進程并發(fā)執(zhí)行的順序,輸出結(jié)果是否與設(shè)想中的順序不同,并分析原因。二、方法、步驟:(說明程序相關(guān)的算法原理或知識內(nèi)容,程序設(shè)計的思路和方法,可以用流程圖表述,程序主要數(shù)據(jù)結(jié)構(gòu)的設(shè)計、主

3、要函數(shù)之間的調(diào)用關(guān)系等)方法:此次試驗需要使用到創(chuàng)建子進程的函數(shù)fork () ,必須分清父進程和子進程的區(qū)別和各自的運行條件。要使父進程創(chuàng)建兩個子進程,須在父進程執(zhí)行的代碼中再次創(chuàng)建子進程。用到的函數(shù): getpid() 。execl() waitpid ()getpid()用來取得目前進程的進程識別碼,許多程序利用取到的此值來建立臨時文件,以避免臨時文件相同帶來的問題。execl()用來執(zhí)行參數(shù)path 字符串所代表的文件路徑,接下來的參數(shù)代表執(zhí)行該文件時傳遞過去的argv(0) 、argv1, ,最后一個參數(shù)必須用空指針(null) 作結(jié)束。waitpid() 會暫時停止目前進程的執(zhí)行,

4、直到有信號來到或子進程結(jié)束。如果在調(diào)用waitpid() 時子進程已經(jīng)結(jié)束,則wait() 會立即返回子進程結(jié)束狀態(tài)值。子進程的結(jié)束狀態(tài)值會由參數(shù)status返回,而子進程的進程識別碼也會一快返回。如果不在意結(jié)束狀態(tài)值,則參數(shù) status 可以設(shè)成 null 。參數(shù) pid 為欲等待的子進程識別碼步驟 : 1 啟動 vc+等開發(fā)平臺, 創(chuàng)建我們所需的程序文件并保存到cygwin 的用戶文件夾下。2. 啟動 cygwin , 在命令行中輸入ls, 可看到我們存入的程序文件。在命令行中輸入gcc o xxx.exe xxx.cpp 把程序文件變成cygwin 下可行的文件。可知是否實現(xiàn)了可執(zhí)行文

5、件。3.在 cygwin 命令行下運行 ./xxx. (為之前帶exe后綴的文件)結(jié)果在cygwin上會直接顯示。三實驗過程及內(nèi)容:(對程序代碼進行說明和分析,越詳細越好,代碼排版要整齊,可讀性要高) 第一個程序:hello word:#include int main(void) printf(hello world!n); return 0; hello word 是我們最早接觸的用c 語言編的語句。 因為是用整形int 定義的 main,所以要有返回值,結(jié)尾return 0;是給系統(tǒng)的一個信息,代表程序正常結(jié)束。創(chuàng)建 2個子進程:#include #include #include #i

6、nclude #include #include #include int tprintf (const char*fmt,.); int main(void) int i=0,j=0; pid_t pid; printf(hello from parent process,pid is %d.n,getpid(); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0)/ 子進程執(zhí)行 sleep(1); for(i=0;i3;i+) printf(hello f

7、rom the child process %d.%d timesn,getpid(),i+1); sleep(1); else if(pid!=-1)/ 父進程 tprintf(parent forked one child process-%d.n,pid); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) sleep(1); for(i=0;itm_hour,tstruct-tm_min,tstruct-tm_sec,getpid(); va_sta

8、rt(args,fmt); return vprintf(fmt,args); 在父進程下創(chuàng)建子進程時,父進程與子進程各自獨立執(zhí)行。執(zhí)行fork 調(diào)用,創(chuàng)建了一個新的進程,這個進程共享父進程的數(shù)據(jù)和堆??臻g等,這之后的代碼指令為子進程創(chuàng)建了一個拷貝。fock 調(diào)用是一個復(fù)制進程,fock 不象線程需提供一個函數(shù)做為入口,fock調(diào)用后,新進程的入口就在fock 的下一條語句。子進程運行時的時候,并非調(diào)用和執(zhí)行父進程的數(shù)據(jù)。子進程的數(shù)據(jù)和堆棧空間和父進程是獨立的,而不是共享數(shù)據(jù)。運行截圖實驗 3:進程中調(diào)用外部命令:#include #include #include #include #inc

9、lude #include #include int tprintf(const char*fmt,.); int main(void) pid_t pid; pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) sleep(5); tprintf(hello from child process!n); tprintf(i am calling exec.n); execl(/bin/ps,-a,null); tprintf(y ou should never

10、 see this because the child is already gone.n); else if(pid!=-1) tprintf(hello from parent,pid %d.n,getpid(); sleep(1); tprintf(parent forked process %d.n,pid); sleep(1); tprintf(parent is waiting for child to exit.n); waitpid(pid,null,0); tprintf(parent had exited.n); else tprintf(everything was do

11、ne without error.n); return 0; int tprintf(const char*fmt,.) va_list args; struct tm *tstruct; time_t tsec; tsec=time(null); tstruct=localtime(&tsec); printf(%02d:%02d:%02d:%5d|,tstruct-tm_hour,tstruct-tm_min,tstruct-tm_sec,getpid(); va_start(args,fmt); return vprintf(fmt,args); 其中 execl()用來執(zhí)行參數(shù)

12、path 字符串所代表的文件路徑,接下來的參數(shù)代表執(zhí)行該文件時傳遞過去的argv(0) 、argv1, ,最后一個參數(shù)必須用空指針(null) 作結(jié)束。此程序是調(diào)用linux/unix下的進程管理命令ps a,最后一個值必須為null 。運行截圖:創(chuàng)建多個子進程:#include #include #include #include #include #include #include int tprintf (const char*fmt,.); int main(void) int i=0,j=0; pid_t pid; printf(hello from parent process,p

13、id is %d.n,getpid(); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) sleep(1); for(i=0;i3;i+) printf(hello from the child process %d.%d timesn,getpid(),i+1); sleep(1); else if(pid!=-1) tprintf(parent forked one child process-%d.n,pid); pid=fork(); printf

14、(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) sleep(1); for(i=0;i3;i+) printf(hello from child process %d.%d timesn,getpid(),i+1); sleep(1); else if(pid!=-1) tprintf(parent forked one child process-%d.n,pid); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(

15、),getppid(); sleep(1); if(pid=0) sleep(1); for(i=0;itm_hour,tstruct-tm_min,tstruct-tm_sec,getpid(); va_start(args,fmt); return vprintf(fmt,args); 運行截圖:父進程創(chuàng)建多個子進程并且子進程各自運行3 個外部調(diào)用命令:#include #include #include #include #include #include #include int tprintf (const char*fmt,.); int main(void) int i=0,j=

16、0; pid_t pid; printf(hello from parent process,pid is %d.n,getpid(); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) tprintf(i am calling exec.n); execl(/bin/ps,-a,null); sleep(1); else if(pid!=-1) tprintf(parent forked one child process-%d.n,pid); pid=f

17、ork(); printf(process %d | my parent pid is %d.n,getpid(),getppid(); sleep(1); if(pid=0) sleep(1); tprintf(i am calling exec.n); execl(/bin/ls,-l,null); sleep(1); else if(pid!=-1) tprintf(parent forked one child process-%d.n,pid); pid=fork(); printf(process %d | my parent pid is %d.n,getpid(),getppi

18、d(); sleep(1); if(pid=0) sleep(1); tprintf(i am calling exec.n); execl(/bin/uname,-a,null); sleep(1); else if(pid!=-1) tprintf(hello from parent,pid %d.n,getpid(); sleep(1); tprintf(parent forked process %d.n,pid); sleep(1); tprintf(parent is waiting for child to exit.n); waitpid(pid,null,0); tprint

19、f(parent had exited.n); else tprintf(everything was done whitout error.n); return 0; int tprintf(const char*fmt,.) va_list args; struct tm *tstruct; time_t tsec; tsec=time(null); tstruct=localtime(&tsec); printf(%02d:%02d:%02d:%5d|,tstruct-tm_hour,tstruct-tm_min,tstruct-tm_sec,getpid(); va_start(args,fmt); return vprintf(fmt,args); 創(chuàng)建了第一個子進程后,子進程與父進程相繼運行

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論