2016-09-25 1 views
0

Also versuche ich das Konzept der Enkelkinder zu verstehen. Ich kann eine bestimmte Anzahl von Söhnen (d. H. Brüder) erstellen, aber ich weiß nicht, wie mehrere Generationen erstellt werden. Diese ist das, was ich tat, ein Enkel zu erstellen:Erstellen Sie mehrere Generationen von Kindern Prozess

int main() 
{ 
//Displaying the father 
printf("Initial : Father = %d\n\n", getpid()); 

/****** FORK *******/ 

pid_t varFork, varFork2; 

varFork = fork(); 

if(varFork == -1) //If we have an error, we close the process 
{ 
    printf("ERROR\n"); 
    exit(-1); 
} 
else if (varFork == 0) //if we have a son, we display it's ID and it's father's 
{ 
    printf("SON 1\n"); 
    printf(" ID = %d, Father's ID = %d\n", getpid(), getppid()); 

    varFork2 = fork();//creation of the second fork 

    if(varFork2 == -1) //If we have an error, we close the process 
    { 
     printf("ERROR\n"); 
     exit(-1); 
    } 
    else if (varFork2 == 0) //now we have the son of the first son, so the grandson of the father 
    { 
     printf("\nGRANDSON 1\n"); 
     printf(" ID = %d, Father's ID = %d\n", getpid(), getppid()); 
    } 
    else sleep(0.1);/*we wait 0.1sec so that the father doesn't die before we can 
     display it's id (and before the son process gets adopted by a user process descending from the initial    process)*/ 
} 
else //in the other case, we have a father 
{ 
    sleep(0.1);//again we put the father asleep to avoid adoption 
} 
return 0; 
} 

Wie kann ich X Generationen von Enkeln, X eine globale Variable sein (1son, 1 Enkel, 1 Urenkel, etc.)?

+0

OT: 'sleep()' nimmt eine ganze Zahl. Aufgrund der impliziten Typumwandlung 'sleep (0.1)' ergibt sich 'sleep (0)' – alk

+0

'printf (" Error \ n ")' ist fast immer * falsch *. Und mit "fast immer" meine ich immer. Es gibt einen Grund, dass die Unix-Konvention Prozesse mit 3 offenen Dateideskriptoren startet, und Sie sollten sich angewöhnen, sie alle zu verwenden. Fehlermeldungen sollten sowohl sinnvoll als auch in den richtigen Stream geschrieben sein. 'fprintf (stderr," Error \ n ")' –

+0

Um Nachrichten bedeutungsvoll zu machen, 'man perror' –

Antwort

1

Wie kann ich X Generationen

Vor gabeln, dekrementierte erstellen X und weiter im Innern des Kind Forking bis X ist 0.

Oder innerhalb des Kindes Dekrementieren X und Forking nur fortsetzen, wenn nach Dekrementieren X noch größer ist 0.

Der Code könnte wie folgt aussehen:

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <errno.h> 

void logmsg(const char *, int); 

#define MAX_GENERATIONS (4) 

int main(void) 
{ 
    pid_t pid = 0; 

    for (size_t g = 0; g < MAX_GENERATIONS; ++g) 
    { 
    logmsg("About to fork.", 0); 

    switch (pid = fork()) 
    { 
     case -1: 
     { 
     logmsg("fork() failed", errno); 

     break; 
     } 

     case 0: 
     { 
     logmsg("Hello world. I am the new child.", 0); 

     break; 
     } 

     default: 
     { 
     char s[1024]; 
     snprintf(s, sizeof s, "Successfully created child carrying PID %d.", (int) pid); 
     logmsg(s, 0); 
     g = MAX_GENERATIONS; /* Child forked, so we are done, set g to end looping. */ 

     break; 
     } 
    } 
    } 

    logmsg("Sleeping for 3s.", 0); 
    sleep(3); 

    if (0 != pid) /* In case we forked a child ... */ 
    { 
    logmsg("Waiting for child to end.", 0); 
    if (-1 == wait(NULL)) /* ... wait for the child to terminate. */ 
    { 
     logmsg("wait() failed", errno); 
    } 
    } 

    logmsg("Child ended, terminating as well.", 0); 

    return EXIT_SUCCESS; 
} 

void logmsg(const char * msg, int error) 
{ 
    char s[1024]; 
    snprintf(s, sizeof s, "PID %d: %s", (int) getpid(), msg); 
    if (error) 
    { 
    errno = error; 
    perror(s); 
    exit(EXIT_FAILURE); 
    } 

    puts(s); 
} 

Die Ausgabe sollte ähnlich aussehen:

PID 4887: About to fork. 
PID 4887: Successfully created child carrying PID 4888. 
PID 4887: Sleeping for 3s. 
PID 4888: Hello world. I am the new child. 
PID 4888: About to fork. 
PID 4888: Successfully created child carrying PID 4889. 
PID 4888: Sleeping for 3s. 
PID 4889: Hello world. I am the new child. 
PID 4889: About to fork. 
PID 4889: Successfully created child carrying PID 4890. 
PID 4890: Hello world. I am the new child. 
PID 4890: About to fork. 
PID 4889: Sleeping for 3s. 
PID 4890: Successfully created child carrying PID 4891. 
PID 4890: Sleeping for 3s. 
PID 4891: Hello world. I am the new child. 
PID 4891: Sleeping for 3s. 
PID 4888: Waiting for child to end. 
PID 4890: Waiting for child to end. 
PID 4891: Child ended, terminating as well. 
PID 4890: Child ended, terminating as well. 
PID 4887: Waiting for child to end. 
PID 4889: Waiting for child to end. 
PID 4889: Child ended, terminating as well. 
PID 4888: Child ended, terminating as well. 
PID 4887: Child ended, terminating as well. 

Der Unterschied mit meinem Code oben im Vergleich zu dem Vorschlag, den ich getroffen wird, dass es zählt nach oben zu X.

+0

Ich verstehe es nicht. Wird das nicht einfach Brüder schaffen? Denn wenn wir uns innerhalb des Kindes weiter verzweigen, wird es X-1 Enkel haben, aber alle Enkel werden Brüder sein und keine Urenkel, Ur-Ur-Enkel usw. – Unck

+0

@Unk .: Wenn du jedes Kind (-process) tun lässt nur genau eine Gabel, dann wird jede Generation genau ein Kind haben. – alk

+0

In Ordnung, aber wie ich eine Schleife mit X Wiederholungen erstellen kann, wie kann ein Kind eine Gabel tun? Soll ich 2 Gabel haben? Eine für den Kindprozess und eine für das Enkelkind? Und wiederholen Sie diese Schleife mit dem Enkel, der bei jeder Wiederholung der Vater eines neuen Kindes wird? – Unck

Verwandte Themen