



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗一:進(jìn)程創(chuàng)建、并發(fā)執(zhí)行實驗?zāi)康?加強對進(jìn)程概念的理解 進(jìn)一步了解并發(fā)執(zhí)行的實質(zhì)利用 fork() 考察 fork() 利用 fork()實驗內(nèi)容1、2、3、函數(shù)創(chuàng)建子進(jìn)程。 函數(shù)創(chuàng)建的子進(jìn)程與父進(jìn)程之間的同名變量是否為臨界資源。 函數(shù)編寫一個程序,要求父進(jìn)程創(chuàng)建兩個子進(jìn)程,父進(jìn)程、子進(jìn)程并發(fā) 執(zhí)行,輸出并發(fā)執(zhí)行的消息。實驗環(huán)境PC + Linux Red Hat 操作系統(tǒng) GCC四、實驗原理及實驗思路fork()功能:創(chuàng)建一個新的進(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)程。事實上,子進(jìn)程是父進(jìn)程 的一個 “復(fù)制品 ”。父子進(jìn)程為獨立進(jìn)程,平等調(diào)度,用戶空間獨立 。 返回值:調(diào)用成功,則返回兩次。對子進(jìn)程返回0,對父進(jìn)程返回子進(jìn)程號,這也是最kill()功能:語法:方便的區(qū)分父子進(jìn)程的方法。調(diào)用失敗則返回 -1 給父進(jìn)程,子進(jìn)程不生成 殺死執(zhí)行中的進(jìn)程說明: 返回值:等待到一個子進(jìn)程返回時,返回值為該子進(jìn)程號,同時 返回狀態(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;七、運行結(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等.壓縮文件請下載最新的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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《天子傳奇win98版》劇情攻略
- 項目團支部介紹課件
- 韶關(guān)學(xué)院工程力學(xué)課件
- 2025年輕水堆核電站及配套產(chǎn)品項目合作計劃書
- xx河流排水防澇設(shè)施建設(shè)項目規(guī)劃設(shè)計方案(模板范文)
- 細(xì)胞生物學(xué)測試試題庫含答案
- 2025年增味劑項目發(fā)展計劃
- 現(xiàn)代商場超市連鎖店星級服務(wù)培訓(xùn) 第三章 商品管理技能培訓(xùn)
- 衛(wèi)星互聯(lián)網(wǎng)行業(yè)市場分析1
- 衛(wèi)生部突發(fā)中毒事件衛(wèi)生應(yīng)急預(yù)案
- SL-T+291-2020水利水電工程鉆探規(guī)程
- 大族激光切割機說明書
- 民宿托管運營合同模板
- 2024鄭州鐵路職業(yè)技術(shù)學(xué)院教師招聘考試筆試試題
- DL5009.2-2013 電力建設(shè)安全工作規(guī)程 第2部分:電力線路-www.biao-zhun.cn
- CHT 9008.3-2010 基礎(chǔ)地理信息數(shù)字成果1:500 1:1 000 1:2 000數(shù)字正射影像圖(正式版)
- 四川省成都市2024年七年級下學(xué)期期末數(shù)學(xué)試題附答案
- 體育館安全協(xié)議書
- 科目余額表模板
- 人口社會學(xué)(楊菊華 第二版) 課件 第8-14章 婚姻家庭-人口特征與民生發(fā)展
- 《字體設(shè)計》課程標(biāo)準(zhǔn)
評論
0/150
提交評論