代码之家  ›  专栏  ›  技术社区  ›  SOHAIB ANWAAR

Fedora Linux中的进程

  •  0
  • SOHAIB ANWAAR  · 技术社区  · 8 年前

    我是fedora的新手,只是使用c++代码在fedora中创建流程。我想从父进程生成2个进程。我是在代码中这样做的,但当创建进程2并检查其父id时,它与原始父id不同,可以从某种程度上看出此代码显示此行为的原因,谢谢。

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <spawn.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    using namespace std;
    
    int main()
    {
    
    cout<<"Begning of the program"<<endl;
    int counter=0;
    pid_t child1=fork();
    
    
    
    if(child1==0)
    {
    
        cout<<"Child1 Process"<<endl;
        cout<<"Process ID: "<<getpid()<<endl;
        cout<<"Parrent ID: "<<getppid()<<endl;
    
    
    }
    else if(child1>0)
    {
        pid_t child2=fork();
         if(child2>0)
         {
            cout<<"Parrent of Child1 and Child2"<<endl;
            cout<<"Process ID: "<<getpid()<<endl;
            cout<<"Parrent ID: "<<getppid()<<endl;
    
         }
        else if(child2==0)
         {
            cout<<"Child2 Creadted"<<endl;
            cout<<"Process ID: "<<getpid()<<endl;
            cout<<"Parrent ID: "<<getppid()<<endl;
    
         }
    
        else
        {
            cout<<"Process Failed"<<endl;
        }
    
    }
    else
    {
            cout<<"Process fail"<<endl;
    
    }
    
    cout<<"End "<<endl;
    
    return 0;
    
    }
    

    结果:

       Begning of the program
    Parrent of Child1 and Child2
    Child1 Process
    Process ID: 2186
    Parrent ID: 2059
    End 
    Process ID: 2187
    Parrent ID: 2186
    End 
    Child2 Creadted
    Process ID: 2188
    Parrent ID: 1287
    End
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   3CxEZiVlQ    8 年前

    在分叉子进程输出其父进程id之前,真正的父进程(其中 fork() 已调用)已退出。子进程被重新附加到组父进程,该pid由子进程输出。

    你可以打电话 pstree -p 查看哪个进程是1303。

    我建议更换管路

    cout<<"Process ID: "<<getpid()<<endl;
    cout<<"Parrent ID: "<<getppid()<<endl;
    

    具有

    cout << getpid() << ": Parent ID: "<< getpid() << endl;
    

    这将有助于分离可能的混合输出(因为输出顺序尚未确定)输出示例:

    2186: Parent ID: 2059
    
        2
  •  0
  •   Sohaib Anwaar    8 年前

    您应该像我在代码中所做的那样,在创建进程2之后应用wait。这样父级在子级2执行之前保持活动状态。在您的r代码中,父级在创建child2后死亡,这使child2成为一个僵尸进程。

     #include <iostream>
        #include <string.h>
        #include <stdio.h>
        #include <spawn.h>
        #include <unistd.h>
        #include <sys/wait.h>
    
        using namespace std;
    
        int main()
        {
    
        cout<<"Begning of the program"<<endl;
        int counter=0;
        pid_t child1=fork();
    
    
    
        if(child1==0)
        {
    
            cout<<"Child1 Process"<<endl;
            cout<<"Process ID: "<<getpid()<<endl;
            cout<<"Parrent ID: "<<getppid()<<endl;
    
    
        }
        else if(child1>0)
        {
            pid_t child2=fork();
            wait(NULL);
             if(child2>0)
             {
                cout<<"Parrent of Child1 and Child2"<<endl;
                cout<<"Process ID: "<<getpid()<<endl;
                cout<<"Parrent ID: "<<getppid()<<endl;
    
             }
            else if(child2==0)
             {
                cout<<"Child2 Creadted"<<endl;
                cout<<"Process ID: "<<getpid()<<endl;
                cout<<"Parrent ID: "<<getppid()<<endl;
    
             }
    
            else
            {
                cout<<"Process Failed"<<endl;
            }
    
        }
        else
        {
                cout<<"Process fail"<<endl;
    
        }
    
        cout<<"End "<<endl;
    
        return 0;
    
        }