2017-03-04 5 views
1

Ich versuche, das Konzept des Prozesses zu verstehen. Also schrieb ich ein Programm wie folgt aus:Warum sind parent.getpid() und child.getppid() unterschiedlich

#include<stdio.h> 
#include<sys/types.h> 
#include<unistd.h> 
int main() { 
    pid_t pid; 
    pid = fork(); 
    if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid()); 
    else 
    printf("This is the parent process. My pid is %d and my child's id is %d.\n", getpid(), pid); 
} 

ich dieses Programm erwartet etwas wie

This is the parent process. My pid is 2283 and my child's id is 2284. 
This is the child process. My pid is 2284 and my parent's id is 2283. 

drucken würde aber stattdessen druckt diese

This is the parent process. My pid is 2283 and my child's id is 2284. 
This is the child process. My pid is 2284 and my parent's id is 1086. 

Am Ende der zweiten Zeile Die Eltern-PID des Kindes unterscheidet sich von der PID des Elternprozesses. Warum passiert das? Gibt es etwas, das mir fehlt?

Vielen Dank im Voraus

+0

Ich habe Ihre Probe getestet und kann das von Ihnen beschriebene Verhalten nicht reproduzieren. Auf meinem System funktioniert es wie Sie (und ich) erwarten. Ich habe Windows 10, cygwin, gcc 5.4.0. Seltsam ... – Scheff

+0

Ich testete es auch auf [coliru] (http://coliru.stacked-crooked.com/a/55a061ea3b2c1c6a). Das gleiche Ergebnis - es funktioniert wie erwartet. – Scheff

+0

Ich führe dieses Programm auf Oracle VM Ubuntu. Kommt es deswegen vor? –

Antwort

1

Der Hauch von Tony Tannous war richtig: Das Kind länger leben als die Eltern. Wenn der Elternteil seinen Kinderprozess beendet, wird er "aufgelegt", d. H. Er wird zum Kindprozess des Init-Prozesses.

Ich modifizierte den Beispielcode von OP, um den Kindprozess zu zwingen, länger als der Elternprozess zu leben.

#include<stdio.h> 
#include<sys/types.h> 
#include<unistd.h> 

int main() 
{ 
    pid_t pid; 
    pid = fork(); 
    if (pid == 0) { 
    sleep(1); /* 1 s */ 
    printf(
     "This is the child process." 
     " My pid is %d and my parent's id is %d.\n", getpid(), getppid()); 
    } else { 
    printf(
     "This is the parent process." 
     " My pid is %d and my child's id is %d.\n", getpid(), pid); 
    } 
    return 0; 
} 

Zusammengestellt und mit gcc auf Cygwin getestet:

$ gcc -o test-pid-ppid test-pid-ppid.c 

$ ./test-pid-ppid 
This is the parent process. My pid is 10748 and my child's id is 10300. 

$ This is the child process. My pid is 10300 and my parent's id is 1. 

In meinem Test dies aufgrund der spezifischen PID 1 liegt auf der Hand (die PID der Init-Prozess wird in der Regel). Ich bin ein wenig überrascht über PID 1086 im OP beobachtet, aber:

  1. Es gibt keine Spezifikation (ich weiß), dass init Prozess muss PID bekommen 1 - es ist nur normal.
  2. Das OP wurde auf einer VM ausgeführt. Dort werden können, sind die Dinge etwas anders gemacht als sonst ...

meine Überzeugung bezüglich, dass ein Verlassen Prozess alle seine Kinder töten würde ich weiter untersucht und fanden diese: Is there any UNIX variant on which a child process dies with its parent?. Kurz gesagt: Mein Glaube war falsch. Danke für diese Frage, die mich zur Erleuchtung zwang.

Verwandte Themen