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
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
Ich testete es auch auf [coliru] (http://coliru.stacked-crooked.com/a/55a061ea3b2c1c6a). Das gleiche Ergebnis - es funktioniert wie erwartet. – Scheff
Ich führe dieses Programm auf Oracle VM Ubuntu. Kommt es deswegen vor? –