操作系統(tǒng)實(shí)驗(yàn)報(bào)告(進(jìn)程的創(chuàng)建)_第1頁
操作系統(tǒng)實(shí)驗(yàn)報(bào)告(進(jìn)程的創(chuàng)建)_第2頁
操作系統(tǒng)實(shí)驗(yàn)報(bào)告(進(jìn)程的創(chuàng)建)_第3頁
操作系統(tǒng)實(shí)驗(yàn)報(bào)告(進(jìn)程的創(chuàng)建)_第4頁
操作系統(tǒng)實(shí)驗(yàn)報(bào)告(進(jìn)程的創(chuàng)建)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)題目進(jìn)程的創(chuàng)建小組合作否姓名班級學(xué) 號(hào)一、實(shí)驗(yàn)?zāi)康?、 了解進(jìn)程的創(chuàng)建。2、 了解進(jìn)程間的調(diào)用以及實(shí)現(xiàn)。3、 分析進(jìn)程競爭資源的現(xiàn)象,學(xué)習(xí)解決互斥的方法。4、 加深對進(jìn)程概念的理解,認(rèn)識(shí)并發(fā)執(zhí)行的本質(zhì)。二實(shí)驗(yàn)環(huán)境Windows 系統(tǒng)的計(jì)算機(jī)一臺(tái),安裝了Linux虛擬機(jī)三、實(shí)驗(yàn)內(nèi)容與步驟1、fork()系統(tǒng)調(diào)用的使用例子程序代碼:#include<stdio.h>#include<sys/types.h>#include<unistd.h>int glob=3;int main(void) pid_t pid;int loc=3;

2、 printf("before fork();glod=%d,loc=%d.n",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"); else wait(0); printf("parent process doesn't change the glob and loc:n"

3、); printf("glob=%d,loc=%dn",glob,loc); exit(0);運(yùn)行結(jié)果:2、理解vofork()調(diào)用:程序代碼:#include<stdio.h>#include<sys/types.h>#include<unistd.h>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-; pr

4、intf("child process changes the glob and locn"); exit(0); else printf ("parent process doesn't change the glob and locn"); printf("glob=%d,val=%dn",glob,loc);運(yùn)行結(jié)果:3、給進(jìn)程指定一個(gè)新的運(yùn)行程序的函數(shù)exec().程序代碼:printe1.c代碼:#include<stdio.h>int main(int argc,char * argv)int n;ch

5、ar * * 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);file4.c代碼如下:#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<sys/wait.h>char * env_list="US

6、ER=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_list)<0)printf("execle error!n");exit(0);if(wait

7、pid(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 error!n");exit(0);exit(0);運(yùn)行結(jié)果:4、進(jìn)程終止函數(shù)exit()。程序代碼:#includ

8、e<stdio.h>main() printf("this is a exit system call! n"); exit(0); printf("this sentence never be displayen:n"); #include<stdio.h>main() printf("this is a _exit_test system call! n"); printf("content in buffer"); exit(0);運(yùn)行結(jié)果:5、wait()函數(shù)和sleep()函數(shù)。程

9、序代碼:#include<stdio.h>main() int pid1; if(pid1=fork() if(fork() printf("parent's context,n"); printf("parent's waiting the child1 terminate,n"); wait(0); printf("parent's waiting the child2 terminate,n"); wait(0); printf("parent terminates,n")

10、; exit(0); else printf("child2's context,n"); sleep(5); printf("child2 terminates,n"); exit(0); else if(pid1=0) printf("child1's context,n"); sleep(10); printf("child1 terminates,n"); exit(0); 運(yùn)行結(jié)果:6、編寫一段程序,父進(jìn)程使用fork()創(chuàng)建兩個(gè)子進(jìn)程,利用輸出函數(shù)putchar父進(jìn)程顯示字符”a”,兩個(gè)

11、子進(jìn)程分別顯示“b”和“c”。程序代碼:#include<stdio.h>#include<sys/types.h>#include<unistd.h>int main() int pid;if(pid=fork() if(fork() printf("parent process is n"); putchar('A'); printf("n"); else printf("child2 process is n"); putchar('C'); printf(&q

12、uot;n"); else if(pid=0) printf("child1 process is n"); putchar('B'); printf("n"); 運(yùn)行結(jié)果:四、實(shí)驗(yàn)過程與分析1、在1例子中,調(diào)用正確完成時(shí),給父進(jìn)程返回的是被創(chuàng)建子進(jìn)程標(biāo)識(shí),給子進(jìn)程自己返回的是0;創(chuàng)建失敗時(shí),返回給父進(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)程被激活,就會(huì)造成死鎖。3、在6例子中,上述程序是父進(jìn)程先創(chuàng)建一個(gè)子進(jìn)程,若成功,再創(chuàng)建另一個(gè)子進(jìn)程,之后三個(gè)進(jìn)程并發(fā)執(zhí)行。究竟誰先執(zhí)行,是隨機(jī)的。所以執(zhí)行結(jié)果有多重種。五、實(shí)驗(yàn)總結(jié)1、一個(gè)進(jìn)程調(diào)用exec()函數(shù)來運(yùn)行一個(gè)新程序。之后該進(jìn)程的代碼段、數(shù)據(jù)段和堆棧段就被新程序的所代替。新程序從自己的main()函數(shù)開始執(zhí)行。exec()函數(shù)有6種不同的形式,任何一個(gè)都可以完成exec()的功能,只是調(diào)用參數(shù)不同。2、在父子進(jìn)程同步中,當(dāng)一個(gè)進(jìn)程結(jié)束時(shí),產(chǎn)生一個(gè)終止?fàn)顟B(tài)字,然后核心發(fā)一個(gè)SIGCHLD信號(hào)通知父進(jìn)程。因?yàn)樽舆M(jìn)程結(jié)束是異步于父進(jìn)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論