简体   繁体   English

C++ fork() - 如何使用 fork 和 pstree 显示进程树?

[英]C++ fork() - How to display process tree using fork and pstree?

First of all I'm new to Linux and processes, so I couldn't understand the logic of fork() exactly.首先,我是 Linux 和流程的新手,所以我无法准确理解 fork() 的逻辑。 I want to create a process tree from user input and display this tree with using 'pstree'.我想从用户输入创建一个进程树并使用“pstree”显示这个树。 However my code displays the tree more than one times.但是,我的代码不止一次显示树。 I think the reason is fork() copies the 'pstree' command, I couldn't solve this problem.我认为原因是 fork() 复制了“pstree”命令,我无法解决这个问题。 The code is like that:代码是这样的:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string>
#include<sys/types.h>
#include<sys/wait.h>

using namespace std;

int main(int argc, char* argv[])
{

    int forkSize, currentPid,b=0;
    forkSize=atoi(argv[1]);


    int parentPid=getpid();
    
    for(int i=0;i<forkSize;i++)
    {
        
        currentPid = fork();
        
        if (currentPid<0)
        {
            cout<<"Error in fork"<<endl;
            exit(0);
        }
        if(currentPid == 0)
        {   
            cout << "Child process: My parent id = " << getppid() << endl;
            cout << "Child process: My process id = " << getpid() << endl;
        }
        else
        {   
            cout << "Parent process. My process id = " << getpid() << endl;
            cout << "Parent process. Value returned by fork() = " << currentPid << endl;
        }
       
    }
    
    fflush(stdout);
    
    char mychar[500];
    sprintf(mychar, "pstree -p -U %d", parentPid);
    system(mychar);
    fflush(stdout);
    
    cout<<endl;
    
    return 0;
}

When I try this code with input 4, the output looks like that:当我使用输入 4 尝试此代码时,output 看起来像这样:

output output

I can't understand why it displays everything again and again.我不明白为什么它一次又一次地显示所有内容。 Is there a way to display the tree for once?有没有办法一次显示树? I would be really glad if help me about that.如果能帮助我,我会很高兴的。

All the processes eventually get to the code that runs pstree .所有进程最终都会到达运行pstree的代码。 You should check there that you're in the original parent process, and only run pstree in that case.您应该在那里检查您是否处于原始父进程中,并且仅在这种情况下运行pstree

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string>
#include<sys/types.h>
#include<sys/wait.h>

using namespace std;

int main(int argc, char* argv[])
{

    int forkSize, currentPid,b=0;
    forkSize=atoi(argv[1]);

    int parentPid=getpid();
    
    for(int i=0;i<forkSize;i++)
    {
        
        currentPid = fork();
        
        if (currentPid<0)
        {
            cout<<"Error in fork"<<endl;
            exit(0);
        }
        if(currentPid == 0)
        {   
            cout << "Child process: My parent id = " << getppid() << endl;
            cout << "Child process: My process id = " << getpid() << endl;
            sleep(1);
        }
        else
        {   
            cout << "Parent process. My process id = " << getpid() << endl;
            cout << "Parent process. Value returned by fork() = " << currentPid << endl;
        }
       
    }
    
    if (getpid() == parentPid) {
        char mychar[500];
        sprintf(mychar, "pstree -p -U %d", parentPid);
        system(mychar);
    }
        
    return 0;
}

BTW, endl flushes output, you don't need to call fflush(stdout);顺便说一句, endl刷新 output,你不需要调用fflush(stdout); . . Also, that's C, but your code is C++.另外,这是 C,但您的代码是 C++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM