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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

3、nt signo); pid 為要被殺死的進程 id,signo 可以置為 SIGINT 或 SIGTERM 。stat_loc 帶回子進程的五、流程圖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;七、運行結果及其分析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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論