简体   繁体   中英

getppid return wrong parent's pid

I have to build process tree:

Like this.

I used fork command to do it, but every process show same (wrong) ppid: 1528 which is not their parent's pid. On windows i used cLions compiler and it works fine (apart of I), but the tree doesnt appear. On linux i use gcc -o and there is invalid result and as well, no process tree.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {

char buf[40];
sprintf(buf, "pstree -c %d", getpid());

int a, b, c, d, e, f, g, h, i, j, k;

printf("A: Parent of all: %d\n", getpid());
a = fork();

    if (a == 0) {
    printf("B: My pid: %d, parent id: %d\n", getpid(), getppid()); // B
    b = fork();

    if (b == 0) {
        printf("D: My pid: %d, parent id: %d\n", getpid(), getppid()); // D
        d = fork();
        if (d == 0) {
            printf("G: My pid: %d, parent id: %d\n", getpid(), getppid()); // G
        }
        else {
            d = fork();
            if(d==0) {
                printf("H: My pid: %d, parent id: %d\n", getpid(), getppid()); // H
            }
        }
    }
    else{
        b = fork();
        if (b == 0) {
            printf("E: My pid: %d, parent id: %d\n", getpid(), getppid()); // E
            e=fork();
            if(e==0){
                printf("I: My pid: %d, parent id: %d\n", getpid(), getppid()); // I
            }
        }
    }
} else {
    a = fork();
    if (a == 0) {
        printf("C: My pid: %d, parent id: %d\n", getpid(), getppid()); // C
        c=fork();
        if(c==0){
            printf("F: My pid: %d, parent id: %d\n", getpid(), getppid()); // F
            f=fork();
            if(f==0){
                printf("J: My pid: %d, parent id: %d\n", getpid(), getppid()); // J
            }
            else{
                f=fork();
                if(f==0){
                    system(buf);
                    //printf("K: My pid: %d, parent id: %d\n", getpid(), getppid()); // K
                }
            }
        }
    }
}

}

Result on windows:

A: Parent of all: 908
B: My pid: 910, parent id: 908
C: My pid: 911, parent id: 908
D: My pid: 912, parent id: 910
F: My pid: 913, parent id: 911
G: My pid: 914, parent id: 912
E: My pid: 915, parent id: 910
J: My pid: 916, parent id: 913
H: My pid: 917, parent id: 912
I: My pid: 919, parent id: 1
sh: pstree: command not found

Result on linux:

A: Parent of all: 17793
C: My pid: 17795, parent id: 1528
B: My pid: 17794, parent id: 1528
F: My pid: 17796, parent id: 1528
J: My pid: 17798, parent id: 1528
K: My pid: 17799, parent id: 1528
D: My pid: 17797, parent id: 1528
H: My pid: 17804, parent id: 1528
G: My pid: 17803, parent id: 1528
E: My pid: 17802, parent id: 1528
I: My pid: 17805, parent id: 1528

I have no idea what should i add or change to make it works.

what should i add or change to make it works.

The reason why the tree doesnt appear is that the Parent of all for which the process tree is requested has already ended by the time process K gets to execute the system(buf) command. To prevent the parent from disappearing early, insert

    while (wait(NULL) > 0) ;

as the last executable line in the program (at the end of main ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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