Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda