Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
c fork's child ppid does not match parent's pid

I'm totally new to C. I tried the following code, expecting that the child's ppid would match the parent's pid, but this is not the case.

int main() {


    int pid;

    printf("I'm process with pid=%d\n", getpid());

    switch (pid = fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
        default:
            printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
    }

    exit(0);

}
> gcc -o fork fork.c 
> ./fork 
I'm process with pid=16907
I'm the parent process: pid=16907, ppid=6604
I'm the child process: pid=16908, ppid=1 // <-- expected ppid=16907, why 1?
>

What did I do wrong ?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It is likely the parent process has already exited and no longer exists. You could try some delay in the parent.

over 4 years ago · Santiago Trujillo Relatório

0

'init' which is the root process running in a linux system has pid 1 .

When a process's parent gets terminated before itself(i.e. the child) , the child becomes an 'orphan' process and is taken up by the root process or the process just above the hierarchy of the process which created it(parent process) .

Hence , here it is taken up by and executed under init which has pid = 1 . So, delay your parent process for solution.

over 4 years ago · Santiago Trujillo Relatório

0

Like others have mentioned, looks like the parent process has terminated while the child process is still under execution making it(the child process) an orphan. Adding delay before exiting may work.

But an elegant way of doing it is, the parent process has to wait till the child process terminates.

This can be achieved by calling waitpid() with the pid of the child (the value returned by fork() ). When the control comes out of this function, you can be sure that the child process has terminated. Also, waitpid() returns the status of the process termination. Based on the status that it returns, you can get to know the normal/abnormal child termination.

Here is the code that does it:

int main() {


    int pid;
    int status = 0;

    printf("I'm process with pid=%d\n", getpid());

    switch (pid = fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            printf("I'm the child process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
        default:
            waitpid(pid, &status, 0);
            if(WIFEXITED(status)
            {
                printf("Normal termination of child process %d\n", pid);
            }
            else
            {
                printf("Abormal termination of child process %d\n", pid);
            }
            printf("I'm the parent process: pid=%d, ppid=%d\n", getpid(), getppid());
            break;
    }

    exit(0);

}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda