實驗二 進程控制

原創作品轉載請注明出處


編寫程序創建進程樹如圖1和圖2所示,在每個進程中顯示當前進程識別碼和父進程識別碼。


1

#include<stdio.h>
int main()
{
    int p1, p2, p3;//b, c, d
    while((p1=fork()) == -1) ;
    if(p1 != 0)
    {
        printf("process a\'s pid is %d, a\'s ppid is %d\n", getpid(), getppid());
    } 
    else 
    {
        while((p2=fork()) == -1) ;
        if(p2 != 0)
        {
            printf("process b\'s pid is %d, b\'s ppid is %d\n", getpid(), getppid());
        }
        else
        {
            while((p3=fork()) == -1) ;
            if(p3 != 0)
            {
                printf("process c\'s pid is %d, c\'s ppid is %d\n", getpid(), getppid());
            }
            else
            {
                printf("process d\'s pid is %d, d\'s ppid is %d\n", getpid(), getppid());
            }
        }
    }
}

2

version 1
#include<stdio.h>
int main()
{
    int p1, p2, p3, p4;//b,c,d,e
    while((p1=fork()) == -1 || (p3=fork()) == -1) ;
    if(p1 > 0 && p3 > 0)
    {
        printf("process a\'s pid is %d, a\'s ppid is %d\n", getpid(), getppid());
    }
    else if(p1 > 0)
    {
        while((p2=fork()) == -1) ;
        if(p2 > 0)
        {
            printf("process b\'s pid is %d, b\'s ppid is %d\n", getpid(), getppid());
        }
        else
        {
            printf("process c\'s pid is %d, c\'s ppid is %d\n", getpid(), getppid());
        }
    }
    else if (p3 > 0)
    {
        while((p4=fork()) == -1) ;
        if(p4 > 0)
        {
            printf("process d\'s pid is %d, d\'s ppid is %d\n", getpid(), getppid());
        }
        else
        {
            printf("process e\'s pid is %d, e\'s ppid is %d\n", getpid(), getppid());
        }
    }
}
version 2

通過對fork返回的值是否為零,判斷是否是在新建立的子進程中。

#include <stdio.h>

int main() {
    int p_b, p_c, p_d, p_e;
    while((p_b=fork()) == -1) ;
    if(0 == p_b) {
        while((p_c=fork()) == -1) ;
        if(0 == p_c) {
            printf("process c\'s pid is %d, c\'s ppid is %d\n", getpid(), getppid());
        } else {
            printf("process b\'s pid is %d, b\'s ppid is %d\n", getpid(), getppid());
        }
    } else {
        while((p_d=fork()) == -1) ;
        if (0 == p_d) {
            while((p_e=fork()) == -1) ;
            if (0 == p_e) {
                printf("process e\'s pid is %d, e\'s ppid is %d\n", getpid(), getppid());
            } else {
                printf("process d\'s pid is %d, d\'s ppid is %d\n", getpid(), getppid());
            }
        } else {
            printf("process a\'s pid is %d, a\'s ppid is %d\n", getpid(), getppid());
        }
    }
}

運行結果:
process a's pid is 3412, a's ppid is 2987
process b's pid is 3413, b's ppid is 3412
process c's pid is 3415, c's ppid is 3413
process d's pid is 3414, d's ppid is 3412
process e's pid is 3416, e's ppid is 3414

相關鏈接:

1.進程管理
2.unistd.h
3.wait

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Linux 進程管理與程序開發 進程是Linux事務管理的基本單元,所有的進程均擁有自己獨立的處理環境和系統資源,...
    JamesPeng閱讀 2,494評論 1 14
  • Charles解決Https亂碼的問題,網上有很多方法了,我安裝的是4.0,參照一一處理,但都不能正常抓取Http...
    rorschachwhy閱讀 327評論 0 0
  • 一、vue模塊化開發 所謂的模塊化開發是指將不同的部分封裝到不同的模塊中,不再將所有的組件、路由等寫在一個頁面中。...
    間陽幕賓閱讀 584評論 0 1
  • 如題 每兩三天再看部電影什么的。 今天買了三小盆多肉和一只水仙,算新開始吧。
    艾瑪魚閱讀 260評論 0 1
  • 好像有人敲院門,孫伯讓好像也清醒了兩秒鐘,接著又睡了。再次醒來是因為聽到咕咚醫生,他撐著椅背爬起來去開門,一個小人...
    姜辣素閱讀 167評論 0 0