2016-12-03 2 views
0

Je veux créer un fils (1) avec fork() et ce fils a besoin de créer un autre fils (2).Comment utiliser fork() pour créer un processus fils sur un enfant?

fils (1) et le père doit attendre la fin de leur fils donne une info. Je veux tous à printf leur PID.

Ceci est mon code:

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

int main(){ 

    int pid; //i know thats not good 

    if((pid = fork()) == 0) { //this isnt good either 
    printf ("SON %d\n",getpid()); 

    } else { 
    // sleep(1) not necessary 
    printf ("Thats the Father\n"); 
    printf ("PID of my Son PID %d\n",pid); 
    } 
} 

trouvé plusieurs infos pour créer plusieurs enfants sur 1 père, mais je ne sais pas, comment créer un nouvel enfant d'un enfant.

+1

processus sont neutres. 'fork 'crée un" enfant ", pas un" fils ". –

Répondre

1

J'ai trouvé plusieurs informations pour créer plusieurs enfants sur 1 père mais je ne sais pas, comment créer un enfant à partir d'un enfant.

C'est exactement comme la façon dont vous créez le premier processus fils. Vous devez à nouveau fork() dans le processus enfant pour créer un autre processus. Utilisez wait(2) pour attendre les processus fils.

Prenons l'exemple (sans vérification d'erreur):

#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
int main(void){ 

pid_t pid; 

if((pid = fork()) == 0) { 
    printf ("Child process: %d\n", (int)getpid()); 
    pid_t pid2; 

    if ((pid2 = fork()) == 0) { 
     printf("Child's child process: %d\n", (int)getpid()); 
    } else { 
     int st2; 
     wait(&st2); 
    } 
} else { 
    printf ("Parent process: %d\n", (int)getpid()); 
    int st; 
    wait(&st); 
} 

return 0; 
} 
0

Merci pour la grande réponse. Je l'ai su.

mon code actuel:

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

int main(void){ 

int pid; 
int pid2; 
int st; 
int st2; 

if((pid = fork()) == 0) { 
    printf ("Kind: %d\n",getpid()); 

    if ((pid2 = fork()) == 0) { 
     printf("Kindes Kind process: %d\n",getpid()); 
    } else { 
       wait(&st2); 

    } 
} else { 
    printf("Ich warte auf meinen Sohn\n"); //WAITING FOR MY SON 
    wait(&st); 
    printf("mein Sohn ist fertig\n"); // MY SON IS RDY 
     printf ("Vater process: %d\n", getpid()); 
    printf("Vater: Status = %d\n",st); //MY STATUS AS A FATHER 
} 

return 0; 
} 

Mon résultat:

Ich warte auf meinen Sohn //waiting for my son 
Kind: 2175 //pid children 
Kindes Kind process: 2176 // childrens child pid 
mein Sohn ist fertig //my son finished 
Vater process: 2174 //father pid 
Vater: Status = 0 //father status 

Mon seul problème est que le PID du premier enfant imprime d'abord. Je veux que l'enfant attende son propre enfant et qu'il imprime son propre pid.

Comme:

waiting for my Son(2) 
pidchild2 
my son2 finished 
status 
waiting for my son 
pidchild1 
my son1 finished 
father pid 
status 

modifier:

a eu, je pense que

juste eu à printf au-delà du WAIT2. était évident

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

int main(void){ 

int pid; 
int pid2; 
int st; 
int st2; 

if((pid = fork()) == 0) { 


    if ((pid2 = fork()) == 0) { 
     printf("Kindes Kind process: %d\n",getpid()); 
    } else { 
    printf("Ich warte auf meinen Sohn1\n"); 
       wait(&st2); 
    printf("mein Sohn ist fertig2\n"); // MY SON IS RDY 
    printf ("Kind: %d\n",getpid()); 
    printf("Sohn1: Status = %d\n",st2); //MY STATUS AS A FATHER 

    } 
} else { 
    printf("Ich warte auf meinen Sohn\n"); //WAITING FOR MY SON 
    wait(&st); 
    printf("mein Sohn ist fertig\n"); // MY SON IS RDY 
     printf ("Vater process: %d\n", getpid()); 
    printf("Vater: Status = %d\n",st); //MY STATUS AS A FATHER 
} 

return 0; 
}