浅探 进程的家族关系
生活随笔
收集整理的這篇文章主要介紹了
浅探 进程的家族关系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
進程的創建:編制一段程序,使用系統調用fork( )創建兩個子進程,在該程序運行時,在系統中就有一個父進程和兩個子進程在活動。讓每一個進程在屏幕上顯示一個字符:父進程顯示字符a,子進程分別顯示字符 b和字符c。試觀察、記錄并分析屏幕上,進程運行的情況。(提示:運行過程中配拍攝進程的家族關系。)
代碼:
#include<stdio.h> int main(){int p1,p2;while(( p1 = fork())== -1 );if(p1 == 0 ){printf("First Son's Name:c PID:%d is father 's PID:%d\n",getpid(),getppid());sleep(5);}else{waitpid(0,NULL,0);while((p2 = fork()) == -1 );if(p2 == 0) {printf("Second Son's Name:b PID:%d his father's PID:%d\n",getpid(),getppid());sleep(15);}else{waitpid(0,NULL,0);printf("second son's father PID: %d , father's father is called grandfather PID:%d \n",getpid(),getppid());}} }?
運行結果
?
? 家族分析:
? First Son’s Name:c PID**:7725 is father 's PID:7724
? Second Son’s Name:b PID:7827 his father’s PID:7724
? second son’s father PID: 7724 , father’s father is called grandfather PID:1416
? 由輸出結果來看:已經一目了然了
總結
以上是生活随笔為你收集整理的浅探 进程的家族关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2005年注册码
- 下一篇: displaytag分页中文处理