實(shí)驗(yàn)一:進(jìn)程創(chuàng)建并發(fā)執(zhí)行_第1頁
實(shí)驗(yàn)一:進(jìn)程創(chuàng)建并發(fā)執(zhí)行_第2頁
實(shí)驗(yàn)一:進(jìn)程創(chuàng)建并發(fā)執(zhí)行_第3頁
實(shí)驗(yàn)一:進(jìn)程創(chuàng)建并發(fā)執(zhí)行_第4頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)一:進(jìn)程創(chuàng)建、并發(fā)執(zhí)行實(shí)驗(yàn)?zāi)康?加強(qiáng)對(duì)進(jìn)程概念的理解 進(jìn)一步了解并發(fā)執(zhí)行的實(shí)質(zhì)利用 fork() 考察 fork() 利用 fork()實(shí)驗(yàn)內(nèi)容1、2、3、函數(shù)創(chuàng)建子進(jìn)程。 函數(shù)創(chuàng)建的子進(jìn)程與父進(jìn)程之間的同名變量是否為臨界資源。 函數(shù)編寫一個(gè)程序,要求父進(jìn)程創(chuàng)建兩個(gè)子進(jìn)程,父進(jìn)程、子進(jìn)程并發(fā) 執(zhí)行,輸出并發(fā)執(zhí)行的消息。實(shí)驗(yàn)環(huán)境PC + Linux Red Hat 操作系統(tǒng) GCC四、實(shí)驗(yàn)原理及實(shí)驗(yàn)思路fork()功能:創(chuàng)建一個(gè)新的進(jìn)程語法: #include <unistd.h>#include <sys/types.h>pid_t fork();說明:本系統(tǒng)調(diào)用為

2、調(diào)用進(jìn)程(也稱父進(jìn)程)創(chuàng)建一子進(jìn)程。事實(shí)上,子進(jìn)程是父進(jìn)程 的一個(gè) “復(fù)制品 ”。父子進(jìn)程為獨(dú)立進(jìn)程,平等調(diào)度,用戶空間獨(dú)立 。 返回值:調(diào)用成功,則返回兩次。對(duì)子進(jìn)程返回0,對(duì)父進(jìn)程返回子進(jìn)程號(hào),這也是最kill()功能:語法:方便的區(qū)分父子進(jìn)程的方法。調(diào)用失敗則返回 -1 給父進(jìn)程,子進(jìn)程不生成 殺死執(zhí)行中的進(jìn)程說明: 返回值:等待到一個(gè)子進(jìn)程返回時(shí),返回值為該子進(jìn)程號(hào),同時(shí) 返回狀態(tài)信息(參考 exit )。若無子進(jìn)程,則返回值為 -1。#include <sys/types.h> #include <signal.h> void kill(pid_t pid,i

3、nt signo); pid 為要被殺死的進(jìn)程 id,signo 可以置為 SIGINT 或 SIGTERM 。stat_loc 帶回子進(jìn)程的五、流程圖Izi六、源代碼Lab1-1 :#include <unistd.h> #include <sys/types.h>#include <stdio.h> main()pid_t child;printf( “ Forkingn.".);child =fork();if (child<0)perror("Fork failed!n"); exit(1);else if (ch

4、ild=0)printf("I'm the child!n");elseprintf("I'm the parent!n");printf("Why I'm printed twice?n"); Lab1-2 :#include <unistd.h>#include <sys/types.h>#include <stdio.h>main()pid_t child;int a=0;printf("Forking.n"); child=fork();if (c

5、hild<0)perror("Fork failed!n"); exit(1);else if (child=0)a+; printf("Child:a=%dn",a);elsea+;printf("Parent:a=%dn",a);Lab1-3 :#include "unistd.h"#include "sys/types.h"#include "signal.h"#include "stdio.h" int main(int argc,char*

6、 argv) pid_t child1_pid,child2_pid;int i = 15;/*fork*/printf("first forkn");child1_pid = fork(); if(child1_pid < 0)printf("fork() fail!n");return -1;else if(child1_pid = 0)printf("this is the first child process n"); while(1)sleep(1);printf("the first child proc

7、 waiting to be killedn"); elseprintf("this is farther process, after first forkn"); /*fork*/ printf("second forkn"); child2_pid = fork(); if(child2_pid < 0) printf("fork() fail!n");return -1;else if(child2_pid = 0)printf("this is the second child process n&

8、quot;); while(1)sleep(1);printf("the second child proc waiting to be killedn"); elseprintf("this is farther process, after second forkn");while(i > 0)printf("after %d second,all proc will be killedn",i); sleep(2);i -= 2; /*kill*/ printf("kill the first child procn"); kill(child1_pid,SIGINT); printf("kill the second child procn"); kill(child2_pid,SIGINT); return 0;七、運(yùn)行結(jié)果及其分析Lab1-1 :輸出:Forking. I'm the child!八、Lab1-2 :輸出:Lab1-3 :輸出:Why I'm printed twice?Forking.Child a1first forkthis is the first child processthe f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論