Einzelnen Beitrag anzeigen
Ungelesen 29.04.14, 17:23   #2
spartan-b292
Echter Freak
 
Benutzerbild von spartan-b292
 
Registriert seit: Mar 2010
Ort: /home/spartan-b292
Beiträge: 2.856
Bedankt: 1.701
spartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punktespartan-b292 leckt gerne myGully Deckel in der Kanalisation! | 230828 Respekt Punkte
Standard

Zitat:
Wieso kann dieser dann 0 sein? Hat ein Kind-Prozess etwa keine PID?
Aus der manpage von fork:

Zitat:
On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately.
Zitat:
2. Wo fängt der Kind-Prozess an und wo hört dieser auf?
Der Kind Prozess beginnt nach der fork() Anweisung und endet wenn die Anweisungen durchlaufen wurden.

Zitat:
3. Ist der Eltern-Prozess der Aufruf meines Programms?
fork manpage
Zitat:
The child's parent process ID is the same as the parent's process ID.
4.) Vermutlich weil du was falsch gemacht hast.

Beispiel:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
 for (int i = 1; i <= 2; i++) fork();
 printf("Hallo von Prozess %d\n", getpid());
}
Jeder Kindprozess hat lokale Kopien der Variablen und beinflusst nicht die Variablen des Eltern-Prozess oder der anderen Kind-Prozesse.

Du erhälst 4 Ausgaben.
Der Eltern-Prozess (P0) durchläuft die Schleife 2 mal, und erzeugt damit 2 Kindprozesse (P1 und P2). Die Kind Prozesse erhalten ihre Kopie der Variable i diese ist für P1 i=1 und für P2 i=2. Also durchläuft P1 die Schleife 2 mal und P2 durchläuft die Schleife 1 mal.

Jeder Kind-Prozess beginnt direkt nach der fork() anweisung, was im Fall der Schleife i++ ist.
P2 erhöht i jetzt von 2 auf 3. Was zum Abbruch der Schleife führt, also werden keine weiteren Prozesse erzeugt.

P1 erhäht i von 1 auf 2. Also wird noch ein Prozess (P1-1) erzeugt. Danach wird i von 2 auf 3 erhöht was zum Abbruch der Schleife führt.

P1-1 erhöht seine Kopie von i von 2 auf 3 und beendet somit die Schleife.

Also haben wir am Ende der Schleife 3 Kind-Prozesse, P1,P2 und P1-1.

Zitat:
Wie kann ich explizit sagen, dass ein Kind-Prozess beendet wird?
Wenn du aus dem Eltern-Prozess einen Kind-Prozess beenden willst kannst du das z.B. mit kill(pid,SIGKILL) machen.

Zitat:
6. Wann kann es zu einem Fehler bei der erstellung eines Prozesses kommen?
Aus der Fork-manpage

Zitat:
ERRORS

EAGAIN fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.

EAGAIN It was not possible to create a new process because the caller's RLIMIT_NPROC resource limit was encountered. To exceed this limit, the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.

ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight.

ENOSYS fork() is not supported on this platform (for example, hardware without a Memory-Management Unit).
__________________
"They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety"
spartan-b292 ist offline   Mit Zitat antworten