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!