![實驗一:進程創(chuàng)建并發(fā)執(zhí)行_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/a6783974-98f0-45d3-b27b-b6462b8dcdfb/a6783974-98f0-45d3-b27b-b6462b8dcdfb1.gif)
![實驗一:進程創(chuàng)建并發(fā)執(zhí)行_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/a6783974-98f0-45d3-b27b-b6462b8dcdfb/a6783974-98f0-45d3-b27b-b6462b8dcdfb2.gif)
![實驗一:進程創(chuàng)建并發(fā)執(zhí)行_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/a6783974-98f0-45d3-b27b-b6462b8dcdfb/a6783974-98f0-45d3-b27b-b6462b8dcdfb3.gif)
![實驗一:進程創(chuàng)建并發(fā)執(zhí)行_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/10/a6783974-98f0-45d3-b27b-b6462b8dcdfb/a6783974-98f0-45d3-b27b-b6462b8dcdfb4.gif)
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗一:進程創(chuàng)建、并發(fā)執(zhí)行實驗目的 加強對進程概念的理解 進一步了解并發(fā)執(zhí)行的實質(zhì)利用 fork() 考察 fork() 利用 fork()實驗內(nèi)容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)調(diào)用為
2、調(diào)用進程(也稱父進程)創(chuàng)建一子進程。事實上,子進程是父進程 的一個 “復制品 ”。父子進程為獨立進程,平等調(diào)度,用戶空間獨立 。 返回值:調(diào)用成功,則返回兩次。對子進程返回0,對父進程返回子進程號,這也是最kill()功能:語法:方便的區(qū)分父子進程的方法。調(diào)用失敗則返回 -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)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年公司車輛借用管理協(xié)議
- 2025年企業(yè)并購雙方股權策劃合同樣本
- 2025年產(chǎn)品銷售代表合同范本
- 2025年多功能會議室租賃合同樣本
- 2025年企業(yè)人力資源部門員工雇傭協(xié)議
- 2025年個人租賃協(xié)議范本
- 2025年熱固化油墨項目規(guī)劃申請報告
- 2025年應用軟件設計服務項目立項申請報告模范
- 2025年電力系統(tǒng)安全策劃生產(chǎn)責任協(xié)議書
- 2025年金融機構信用借貸合同范文
- 站樁的知識講座
- 革命文物主題陳列展覽導則(試行)
- 醫(yī)學遺傳學第三版課件
- 四川家庭農(nóng)場補貼標準2023年
- 五年級下冊字帖
- 設備采購供貨安裝實施方案
- 初中生物《病毒》說課課件
- 小小銀行家-兒童銀行知識、理財知識培訓
- 物業(yè)公司縮減人員方案范本
- 河湖保護主題班會課件
- 機械基礎知識競賽題庫附答案(100題)
評論
0/150
提交評論