Liebe Forenmitglieder,
wie im Betreff schon erwähnt habe ich Probleme, 2 Kindprozesse zu erstellen und in dieser dann Funktionen aufzurufen. Klingt eigentich sehr einfach nur macht es beim Ausführen nicht das, was ich erhofft habe und hoffe, ihr könnt mir da eventuell weiterhelfen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | int main(int argc, char * argv[]) { pid_t pid[MAX_CHILD_PROC] = {0}; pid_t pid_end[MAX_CHILD_PROC] = {0}; /* Process creation */ if ((pid[MAX_CHILD_PROC - 1] = fork()) < 0){ perror("error fork() / 1. Child"); return EXIT_FAILURE; } else if (pid[MAX_CHILD_PROC - 1] == 0) { child_proc_1_handle(); /* 1. Child process */ exit(0); } if ((pid[MAX_CHILD_PROC] = fork()) < 0){ perror("error fork() / 2. Child"); return EXIT_FAILURE; } else { child_proc_2_handle(); /* 2. Child process */ exit(0); } for(uint8_t i = 1; i <= MAX_CHILD_PROC; i++ ){ /* waits for the termination of the child proceses */ pid_end[i] = wait(NULL); printf("Child with the ID: %6d is terminated \n", pid_end[i]); } return EXIT_SUCCESS; } void child_proc_1_handle (void){ printf("Within the Child Process 1!\n"); printf("Child 1: PID = %6d, PPID = %6d\n", getpid(), getppid()); return; } void child_proc_2_handle (void){ printf("Within the Child Process 2!\n"); printf("Display 2: PID = %6d, PPID = %6d\n", getpid(), getppid()); return; } |
Beim Ausführen springt er mir zuerst in den child_proc_2_handle, danach wieder und zum schluss erst in den child_proc_1_handle. Die Ausgabe in der for Schleife gibt er mir garnicht aus, wobei ich mir ehrlich gesagt nicht sicher bin, ob man so auf die Termination mehrerer Prozesse wartet.
Hier ist nochmal ausgabe:
Within the Child Process 2! Child 2: PID = 2740, PPID = 2712
Within the Child Process 2! Child 2: PID = 2742, PPID = 2740
Within the Child Process 1! Child 1: PID = 2741, PPID = 2740
EIgentlich eh so wie es sein sollte, nur dass mir der Child Prozess 2 noch 2 Enkelprozesse erzeugt. Ich würde halt gerne wolen dass der Elternprozess zwei erzegt und nicht der Child Prozess, 2 Enkelprozesse erzeugt.
Ich hoffe Ihr könnt mir da weiter helfen. Lg