![操作系統(tǒng)實驗二.doc_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/27/2376d5bd-8427-4587-b5e8-133f2682f8f4/2376d5bd-8427-4587-b5e8-133f2682f8f41.gif)
![操作系統(tǒng)實驗二.doc_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/27/2376d5bd-8427-4587-b5e8-133f2682f8f4/2376d5bd-8427-4587-b5e8-133f2682f8f42.gif)
![操作系統(tǒng)實驗二.doc_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/27/2376d5bd-8427-4587-b5e8-133f2682f8f4/2376d5bd-8427-4587-b5e8-133f2682f8f43.gif)
![操作系統(tǒng)實驗二.doc_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/27/2376d5bd-8427-4587-b5e8-133f2682f8f4/2376d5bd-8427-4587-b5e8-133f2682f8f44.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、操作系統(tǒng)進程管理實驗實驗題目:(1) 進程的創(chuàng)建編寫一段程序,使用系統(tǒng)調(diào)用 fork( ) 創(chuàng)建兩個子進程。 當此程序運行時,在系統(tǒng)中有一個父進程和兩個子進程活動。 讓每一個進程在屏幕上顯示一個字符: 父進程顯 示字符“ a”;子進程分別顯示字符“b”和字符“ c”。試觀察記錄屏幕上的顯示結果,并分析原因。(2) 進程的控制修改已編寫的程序,將每個進程輸出一個字符改為每個進程輸出一句話, 在觀察程序執(zhí)行時屏幕上出現(xiàn)的現(xiàn)象,并分析原因。(3) 編制一段程序,使其實現(xiàn)進程的軟中斷通信。 要求:使用系統(tǒng)調(diào)用 fork( )創(chuàng)建兩個 子進程,再用系統(tǒng)調(diào)用 signal( ) 讓父進程捕捉鍵盤上來的中斷
2、信號(即按 Del 鍵);當捕 捉到中斷信號后,父進程調(diào)用系統(tǒng)調(diào)用 kill( ) 向兩個子進程發(fā)出信號,子進程捕捉到信號 后分別輸出下列信息后終止: Child process 1 is killed by parent! Child process 2 is killed by parent! 父進程等待兩個子進程終止后, 輸出如下的信息后終止: Parent process is killed! 在 上 面 的 程 序 中 增 加 語 句 signal(SIGINT, SIG_IGN) 和 signal(SIGQUIT, SIG_IGN) ,觀察執(zhí)行結果,并分析原因。(4) 進程的管道通
3、信編制一段程序,實現(xiàn)進程的管道通信。使用系統(tǒng)調(diào)用 pipe( )建立一條管道線;兩個進程 P1 和 P2 分別向管道各寫一句話: Child 1 is sending a message! Child 2 is sending a message! 而父進程則從管道中讀出來自于兩個子進程的信息,顯示在屏幕上。要求父進程先接收子進程P1發(fā)來的消息,然后再接收子進程P2發(fā)來的消息。實驗源程序及報告:( 1 )、進程的創(chuàng)建#include int main(int argc, char *argv)int pid1,pid2; /*fork first child process*/if ( ( p
4、id1=fork() ) 0 )printf( ProcessCreate Failed!);exit(-1);if ( ( pid1=fork() ) = 0 )printf( bn );/*fork second child process*/if ( ( pid2=fork() ) 0 )printf( ProcessCreate Failed!);exit(-1);if ( ( pid2=fork() ) = 0 )printf( cn );/*parent process*/elsewait(NULL);printf( an );exit(0);return 0;(2) 、進程的控制
5、#include int main(int argc, char *argv) int pid1,pid2;/*fork first child process*/if ( ( pid1=fork() ) 0 )printf( ProcessCreate Failed!);exit(-1);if ( ( pid1=fork() ) = 0 )printf( This is my Unix OS program!n );/*fork second child process*/if ( ( pid2=fork() ) 0 )printf( ProcessCreate Failed!);exit(
6、-1);if ( ( pid2=fork() ) = 0 )printf( This is the second Child process!n );/*parent process*/elsewait(NULL);printf( This is the Parent processn );exit(0);return 0;(3) 編制一段程序,使其實現(xiàn)進程的軟中斷通信。#include #include #include #include int wait_flag; void stop( );main( )int pid1, pid2; signal(3, stop);while (pid
7、1 = fork( ) = -1);if ( (pid1 = fork() ) 0)while (pid2 = fork( ) = -1);if ( pid2 = fork() 0 )wait_flag = 1; signal(SIGINT, stop);sleep(5);kill(pid1, 16);kill(pid2,17);wait(0);wait(0);printf(n Parent process is killed.n);exit(0);elsewait_flag = 1;signal(17, stop);byprintf(n Child process 2 is killed p
8、arent.n);exit(0);elsewait_flag = 1;signal(16, stop);printf(n Child process 1 is killed by parent.n);exit(0);void stop( )wait_flag = 0;(4) 進程的管道通信#include #include #include int pid1,pid2;int main()int fd2;char OutPipe100, InPipe100;pipe(fd);if( pid1 = 0)lockf( fd1, 1, 0 );message!n);sprintf(OutPipe,
9、n Child process 1 is sendingwrite( fd1, OutPipe, 50);sleep(5);lockf(fd1, 0 , 0);exit(0);elsewhile(pid2 = fork() = -1);if( pid2 = 0)lockf( fd1, 1, 0 );sprintf(OutPipe, n Child process 2 is sending message!n);write( fd1, OutPipe, 50);sleep(3);lockf(fd1, 0 , 0);exit(0);elsewait(0);read(fd0, InPipe, 50)
10、;printf(%sn, InPipe);wait(0);read(fd0, InPipe, 50);printf(%sn, InPipe);exit(0);return 0;三、程序注釋:(1)進程的創(chuàng)建源代碼:#include int main(int argc, char *argv)int pid1,pid2;/* 調(diào)用 fork 函數(shù)創(chuàng)建進程 1*/if ( ( pid1=fork() ) 0 ) /*fork() 返回值為負數(shù),則創(chuàng)建進程失敗 */printf( ProcessCreate Failed!);exit(-1);/*fork() 返回值為 0,則創(chuàng)建子進程成功且當前進
11、程為子進程 */if ( ( pid1=fork() ) = 0 )子進程 1 輸出 b返回值為負數(shù),則創(chuàng)建進程失敗 */printf( bn ); /*/* 創(chuàng)建子進程 2if ( ( pid2=fork() ) 0 ) /*fork()printf( ProcessCreate Failed!);exit(-1);/* /*fork() 返回值為 0,則創(chuàng)建子進程成功且當前進程為子進程if ( ( pid2=fork() ) = 0 )printf( cn ); /子進程 2 輸出 c ;/*fork() 返回值大于 0, 則當前進程為父進程 */elsewait(NULL);printf
12、( an ); /父進程輸出 a exit(0);return 0;(2)進程的控制源代碼:#include int main(int argc, char *argv)int pid1,pid2;/* 調(diào)用 fork 函數(shù)創(chuàng)建進程 1*/if ( ( pid1=fork() ) 0 ) /*fork() 返回值為負數(shù),則創(chuàng)建進程失敗 */printf( ProcessCreate Failed!);exit(-1);/*fork() 返回值為 0,則創(chuàng)建子進程成功且當前進程為子進程 */if ( ( pid1=fork() ) = 0 )子進程 1 輸出句子 ;返回值為負數(shù),則創(chuàng)建進程失敗
13、*/printf( This is my Unix OS program!n ); /*/* 創(chuàng)建子進程 2if ( ( pid2=fork() ) 0 ) /*fork()printf( ProcessCreate Failed!);exit(-1);/* /*fork() 返回值為 0,則創(chuàng)建子進程成功且當前進程為子進程*/if ( ( pid2=fork() ) = 0 )子進程 2 輸出句子;父進程輸出句子printf( This is the second Child process!n ); /*fork() 返回值大于 0, 則當前進程為父進程 */elsewait(NULL);
14、printf( This is the Parent processn ); /exit(0);return 0;3)編制一段程序,使其實現(xiàn)進程的軟中斷通信。源代碼:(軟中斷用 Ctrl+ | 實現(xiàn))#include #include #include #include int wait_flag;void stop( ); /stop()是自己定義的 Signal() 觸發(fā)的自定義函數(shù)main( )int pid1, pid2; /定義兩個進程號參數(shù)signal(3, stop); /Signal()觸發(fā)軟中斷while (pid1 = fork( ) = -1); /程序等待成功創(chuàng)建子進程
15、事件的發(fā)生if ( (pid1 = fork() ) 0)while (pid2 = fork( ) = -1);if ( pid2 = fork() 0 ) /* 當前進程為父進程,父進程發(fā)出兩個軟中斷信號 Kill 子進程 */wait_flag = 1;signal(SIGINT, stop);sleep(5);kill(pid1, 16);kill(pid2,17);wait(0); / 等待子進程死信號wait(0);printf(n Parent process is killed.n); /接收到子進程死信號后,殺死父進程exit(0);else /*/* 當前進程為子進程,則發(fā)
16、送子進程 Kill 信號,殺死該子進程 2wait_flag = 1;signal(17, stop);printf(n Child process 2 is killed by parent.n);exit(0);else /*/* 當前進程為子進程,則發(fā)送子進程 Kill 信號,殺死該子進程 1*/wait_flag = 1;signal(16, stop);printf(n Child process 1 is killed by parent.n);exit(0);void stop( ) /*/* 自定義函數(shù),供 signal() 調(diào)用wait_flag = 0;(4)進程的管道通信
17、源代碼:#include #include #include int pid1,pid2; / 定義兩個進程號參數(shù)int main()的參數(shù) */int fd2; /* 定義一個數(shù)組作為 sys_pipe() char OutPipe100, InPipe100;pipe(fd); /* 調(diào)用 sys_pipe() 創(chuàng)建管道線 */ while(pid1 = fork() = -1);if( pid1 = 0) /*/* 根據(jù) sys_pipe() 函數(shù)的定義 fd1 用于向管道寫數(shù)據(jù)*/lockf( fd1, 1, 0 ); /*鎖定管道寫入端 fd1*/* 進程 1 向管道寫入要輸出的句子
18、 */sprintf(OutPipe, n Child process 1 is sending message!n);write( fd1, OutPipe, 50);sleep(5);lockf(fd1, 0 , 0);exit(0);elsewhile(pid2 = fork() = -1);if( pid2 = 0)lockf( fd1, 1, 0 ); /*鎖定管道寫入端 fd1*/* 進程 2 向管道寫入要輸出的句子 */函數(shù)寫管道 */sprintf(OutPipe, n Child process 2 is sending message!n); write( fd1, OutPi
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 美食廣場服務員工作總結
- 100萬噸綠色清潔能源頁巖氣液化項目可行性研究報告寫作模板-申批備案
- 2025-2030全球電池保護板行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國工業(yè)級4-苯氧基苯酚行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國桁架式門式起重機行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國AI虛擬人交互一體機行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球心理情感咨詢服務平臺行業(yè)調(diào)研及趨勢分析報告
- 2025年全球及中國工業(yè)絕熱冷卻器行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球重餾分輪胎熱解油行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球消費電子注塑機行業(yè)調(diào)研及趨勢分析報告
- 福建省泉州市晉江市2024-2025學年七年級上學期期末生物學試題(含答案)
- 2025年春新人教版物理八年級下冊課件 第十章 浮力 第4節(jié) 跨學科實踐:制作微型密度計
- 貨運車輛駕駛員服務標準化培訓考核試卷
- 財務BP經(jīng)營分析報告
- 三年級上冊體育課教案
- 2024高考物理二輪復習電學實驗專項訓練含解析
- 2024年全國統(tǒng)一高考英語試卷(新課標Ⅰ卷)含答案
- 高中英語:倒裝句專項練習(附答案)
- 2025屆河北衡水數(shù)學高三第一學期期末統(tǒng)考試題含解析
- 2024信息技術數(shù)字孿生能力成熟度模型
- 交通銀行股份有限公司操作風險管理政策
評論
0/150
提交評論