1

Thank you for reading this, I appreciate a lot! I have this part of code:

for(i=0;i<n;i++)
    {
        printf("dopo %d sec ricevuto kill\n",20-sleep(20));
        kill( pidFiglio[i],SIGKILL);
    }

    for (i=0 ; i<n ; i++)
    {
        //attesa della terminazione di un processo figlio
        pidDaWait = wait( & status );
        //(...)

but i can't understand why the father process is able to wait his sons, after he killed them! The kill function returns 0 so it works, but the father is able to wait "his sons" after. I know that it's a stupid answer, but I'm absolutely new in multiprogramming.

#include <stdio.h>
#include <stdlib.h>     //per la exit()
#include <unistd.h> //per la fork()
#include <sys/types.h> //per la wait()
#include <sys/wait.h>   //per la wait()
#include <signal.h>        //per la signal()

int contSigusr1;

void trapSIGUSR1(int nsignal);

int main( int argc , char* argv[] )
{
    int i;
    int n;
    pid_t pidFiglio[10];
    pid_t pidDaWait;
    int status;     

    contSigusr1=0;
    signal ( SIGUSR1, trapSIGUSR1 );

    printf("argc = %d\n", argc);

    if ( argc < 3 )
    {
        printf("Sintassi errata!\n");
        printf("il programma termina con errore\n");
        exit(1);
    }
    n = atoi( argv[1] );


    for (i=0; i<n; i++)
    {
        pidFiglio[i] = fork();
        if ( pidFiglio[i] == 0 )
        {
            printf("Figlio - getpid = %d\n", getpid());
            printf("Figlio - getppid = %d\n", getppid());


            printf("(%d)%s\n",getpid(),argv[2+i]);

            sleep(i*5);

            kill( getppid() , SIGUSR1);

            sleep(i+1*5);

            printf("Figlio - termina correttamente\n");    

            exit(0);
        } 
    }
    printf("getpid = %d\n", getpid());
    printf("getppid = %d\n",getppid());
    for(i=0;i<n;i++)
    {
        printf("dopo %d sec ricevuto kill\n",20-sleep(20));
        kill( pidFiglio[i],SIGKILL);
    }

    for (i=0 ; i<n ; i++)
    {
        pidDaWait = wait( & status );

        printf("status = %d\n", status);
        printf("status di (%d) con WEXITSTATUS = %d\n",
                   pidDaWait,
                   WEXITSTATUS( status));
        if ( WIFSIGNALED (status) )
            printf("il processo figlio (%d) e` terminato tramite un segnale\n",pidDaWait);
        else
            printf("il processo figlio (%d) e` terminato normalmente\n",pidDaWait);
    }
    printf("il padre termina dopo la terminazione del processo figlio con pid = %d\n",pidDaWait );
    exit(0);
}

void trapSIGUSR1(int nsignal) 
{
    contSigusr1= contSigusr1 +1;
    printf("riceived %d SIGUSR1 = %d\n",nsignal,contSigusr1);
}

Just don't pay attention to the italian words :D Thank you in advance, Have a good day!

ooooozz
  • 11
  • 3
  • `wait` is used to cleanup resources of children, the parent process might still need to diagnose the termination reason of the child so linux keeps them as "zombie" processes. It is the responsibility of the parent process to cleanup the resources of its children using `wait`. – Minn Dec 02 '18 at 11:42
  • So, when i use SIGKILL like a signal, i kill the process but i don't cleanup the resources, and so the father "sees" the son, even it's terminated (or better, the parent sees the resources in order to cleanup them with the wait). Am i right? – ooooozz Dec 02 '18 at 11:46

1 Answers1

3

Childs still exist, but are now zombies.

Childs may terminate their execution, but the task_struct still exists in kernel. The task_struct in kernel holds the return status information from the child (and some other info). The zombie task_struct exists only so that the parent process may get the child return status (via wait(&status) + WEXITSTATUS(status)) and other information about the child, so that parent code may be consistent.

Sending SIGKILL to children or terminating all children using exit() call (like in wikipedia) does not matter - the result is that the child process becomes a zombie. After the parent calls wait and that wait services that child, it's task_struct from kernel get's freed and the status information get's returned to the parent, so he can handle it.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111