版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗題目進(jìn)程的創(chuàng)建小組合作否姓名班級學(xué) 號一、實驗?zāi)康牧私膺M(jìn)程的創(chuàng)建。了解進(jìn)程間的調(diào)用以及實現(xiàn)。分析進(jìn)程競爭資源的現(xiàn)象,學(xué)習(xí)解決互斥的方法。加深對進(jìn)程概念的理解,認(rèn)識并發(fā)執(zhí)行的本質(zhì)。二.實驗環(huán)境Windows系統(tǒng)的計算機(jī)一臺,安裝了 Linux虛擬機(jī)三、實驗內(nèi)容與步驟1、fork ()系統(tǒng)調(diào)用的使用例子程序代碼:#include<>#include<sys/>#include<>int glob=3;int main(void)pid_t pid;int 10c=3;printf("before fork();glod=%d,loc=%d.n&qu
2、ot;,glob,loc);if(pid=fork()<0)printf("fork() error. n");exit(0);else if(pid=0)glob+;loc-;printf("child process changes glob and loc: n");elsewait(0);printf("parent process doesn't change the glob and loc:n");printf("glob=%d,loc=%dn",glob,loc);exit(0);運(yùn)行
3、結(jié)果:文件® 編輯®肉標(biāo)。ftMG) 1WWW| coo t'laca I hus i ml drril # g 匚仁raotloca Ih-051 mldmlHt 1 kexebe fore fork()glad-3 +loc-3tcbl Id proce i i change 5 glub ;k! lac:pa reji l proce S5 doc sn ' l change 1he lob and Ine:g I ob= t Jqc=2pa rent proce is doe n I change ihe glob and loc:lob=3 J心亡
4、=3r oo I 'loca I hos i mid nil# |2、理解vofork ()調(diào)用:程序代碼:#include<>#include<sys/>#include<>int glob=3;int main(void)pid_t pid;int loc=3;if(pid=vfork()<0)printf("vfork() errorn");exit(0);else if(pid=0)glob+;loc-;printf("child process changes the glob and locn"
5、;);exit(0);elseprintf ("parent process doesn't change the glob and locn");printf("glob=%d,val=%dn",glob,loc);運(yùn)行結(jié)果:r uo tildes I hdn l ml cirri)# ,ril 史 2.電工電ch l Id prore 5 s change s the g 1-oh and l-o-cpa ren L proce as doe sn t chang史 ihe Rloh and Locf I Q b =4 Aa J=2|mIml
6、dml曲3、給進(jìn)程指定一個新的運(yùn)行程序的函數(shù)exec().程序代碼:代碼:#include<>int main(int argc,char * argv口) int n;char * * ptr;extern char * * environ;for(n=0;n<argc;n+)printf("argv%d:%sn",n,argvn);for(ptr=environ; * ptr!=0;ptr+) printf("%sn",* ptr);exit(0);代碼如下:#include<>#include<sys/>#
7、include<>#include<sys/>char * env_list尸"USER=root","PATH=/root/",NULL;int main()pid_t pid;if(pid=fork()<0)printf("fork error!n");exit(0);else if(pid=0)if(execle("/root/print1","print1","arg1","arg2",(char *)0,env_li
8、st)<0) printf("execle error!n");exit(0);if(waitpid(pid,NULL,0)<0)printf("WAIT ERROR!n");exit(0);if(pid=fork()<0)printf("fork error!n");exit(0);else if(pid=0)if(execlp("print1","print1","arg1",(char *)0)<0) printf("execle er
9、ror!n");exit(0);exit(0);運(yùn)行結(jié)果:ruu tluca & i inldraill A . / f i le4 .cxeargO:piriitarg 1 Largl arg2:arg?LSER-roulPATH=7root|rno t loca Ihos t mldH#JL4、進(jìn)程終止函數(shù)exit()。程序代碼:#include<>main()printf("this is a exit system call! n");exit(0);printf("this sentence never be display
10、en:n");#include<>main()printf("this is a _exit_test system call! n");printf("content in buffer");exit(0);運(yùn)行結(jié)果:rooIhost mldnri# gee ex i i_iest .c -o exi L_Lest_eie r uu lluca I ho Ji L ml JnJ . /ex. i t trat .cxcthis is a exit sysle m ca 11J !T r oo t :k)ca I ho-s l ni
11、ldrri Irunt luca Ihos I mldirj p jtc ent tesL.c -o _c i i i _ tes t exe ruot11iq l unldcd# ./_七工 i l _丘 .亡 xcthis is a _ex i l_lesl pystEm ca I I !口他Me lhn&i ,nUdnjl!5、wait()函數(shù)和 sleep()函數(shù)。程序代碼:#include<>main()int pidl;if(pid1=fork()if(fork()printf("parent's context,n");printf
12、("parent's waiting the childl terminate,n");wait(0);printf("parent's waiting the child2 terminate,n");wait(0);printf("parent terminates,、);exit(0); elseprintf("child2's context,n");sleep(5);printf("child2 terminates,n");exit(0);elseif(pid1=0)p
13、rintf("child1's context,n");sleep(10);printf("child1 terminates,n");exit(0);運(yùn)行結(jié)果:r oo tloca I host MdmJ# . Zf i l-eli .eice ck id s can lex t rchi ld2 s con text *pa reji t1 s con lex tpa reii L1 s vdi l I ing the ch i Idl Irrmina te Fch i Ld2 ir mina ie s bpa ren t' s ai
14、ling t he ch i Id2 terrrim t« r chi l-dl lorminn les ,pa re ji L ier mi ns te s Ptloca I hos t ml dmJ #6、編寫一段程序,父進(jìn)程使用fork()創(chuàng)建兩個子進(jìn)程,利用輸出函數(shù)putchar父進(jìn)程顯示字符"a",兩個子進(jìn)程分別顯示“ b”和“ c”。程序代碼:#include<>#include<sys/>#include<>int main()int pid;if(pid=fork()if(fork()printf("p
15、arent process is n");putchar('A');printf("n");elseprintf("child2 process is n");putchar('C');printf("n");elseif(pid=0)printf("child1 process is n");putchar('B'); printf("n");運(yùn)行結(jié)果:文件(舊漏輯查看出隆瑞3 轉(zhuǎn)到 幫助QDfoq tloca IIioa L mid向
16、# y im kao&h i *roo t Inca Iho ii t mldml* v im kansh i »rootloca Ihos i mldnil# ./kooshi .xechi Id procesi iwHchild2 prncess isCparent pro« ss iwAroo t''loca Iho t mldnj . /kansh i四、實驗過程與分析1、在1例子中,調(diào)用正確完成時,給父進(jìn)程返回的是被創(chuàng)建子進(jìn)程標(biāo)識,給子進(jìn)程自己返回的是0;創(chuàng)建失敗時,返回給父進(jìn)程的是-1。2、在2例子中,vfork ()調(diào)用后需要注意兩點(diǎn):(1)子進(jìn)程先運(yùn)行,父進(jìn)程掛起。子進(jìn)程調(diào)用exec ()或exit ()之后父進(jìn)程的執(zhí)行順序不再有限制。(2)子進(jìn)程在調(diào)用exec ()或exit ()之前。父進(jìn)程被激活,就會造成 死鎖。3、在6例子中,上述程序是父進(jìn)程先創(chuàng)建一個子進(jìn)程,若成功,再創(chuàng)建 另一個子進(jìn)程,之后三個進(jìn)程并發(fā)執(zhí)行。究竟誰先執(zhí)行,是隨機(jī)的。所以 執(zhí)行結(jié)果有多重種。五、實驗總結(jié)1、一個進(jìn)程調(diào)用exec()函數(shù)來運(yùn)行一個新程序。之后該進(jìn)程的代碼段、數(shù)據(jù)段和堆棧段就被新程序的所代替。 新程序從自己的main ()函數(shù)開始執(zhí)行。exec ()函數(shù)有6種不同的形式,任何一個都可以完成 exec()的 功能,只是調(diào)用參
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《我的“鈣”世英雄-碳酸鈣》微課教學(xué)設(shè)計
- 2024萬圣節(jié)鬼混派對游園市集露營派對主題活動策劃案
- 2024哈利波特魔法萬圣節(jié)(魔法萬圣夜主題)活動策劃方案
- 人美版四年級上冊美術(shù)教案(2014年版第七冊)
- 浙美版小學(xué)美術(shù)五年級下冊教案+教學(xué)計劃匯編
- 辦公室主任競爭上崗演講辭
- 天詞匯特訓(xùn)營二期包早讀- day22
- 小學(xué)二年級加減乘除口算題
- 辦文辦會半年工作總結(jié)
- 湖北省黃岡市2023-2024學(xué)年六年級上學(xué)期英語期中試卷(含答案)
- 楊家將:北宋著名抗金將領(lǐng)團(tuán)體
- 材料表面工程學(xué)第5章:氣相沉積技術(shù)
- 室外供水管網(wǎng)接駁施工方案
- 西式面點(diǎn)師(高級)課件 項目4 甜品制作
- 光伏一體化幕墻施工
- 儀 器 設(shè) 備 購 置 申 請 表
- 課程評價方案
- 第3課中國特色社會主義進(jìn)入新時代(高教版2023基礎(chǔ)模塊)
- 危險源辨識清單匯總
- 新生兒溶血病
- 擠擴(kuò)支盤樁技術(shù)研究與工程實踐
評論
0/150
提交評論