Hi everyone, programming newbie here =D
I'm in need of a helping hand from you experts; the topic is the "getppid" function
first, this is the simple code i'm running:
Code:
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
int main () {
pid_t child;
printf ("MAIN program process' pid: %d.\n", (int) getpid () );
child = fork () ;
if (child==-1) {
printf ("Error");
exit (0);
}
else {
if (child!==0) {
printf ("PARENT process running, pid is: %d.\n", (int) getpid() );
printf ("PARENT process running, son's pid is: %d.\n", (int) child );
}
else {
printf ("CHILD process running, pid is: %d.\n", (int) getpid() );
printf ("CHILD process running, parent's pid is: %d.\n", (int) getppid() );
}
}
return (0);
}
1. I first wrote this on Windows, and when I tried to compile it a linker error was displayed, claiming "getppid" was an undeclared function. I think that happened because of the way processes work on Windows (no precise hierarchy), am I right or I forgot something?
2. I tried to recompile this on Ubuntu (run on a virtual machine). Here's one of the possibile outputs:
MAIN program process' pid: 1329.
PARENT process running, pid is: 1329.
PARENT process running, pid is: 1330.
type <Return> to continue
CHILD process running, pid is: 1330.
CHILD process running, parent's pid is: 1.
Why is the last result "1" instead of "1329"? Isn't "getppid" supposed to return the pid of the process' parent?
Is that because of the virtual machine (which is still running with Windows)? I mean, if I had run it on a "pure" UNIX envinronment, would the result have been correct? Or, again, did I wrote something the wrong way?
Thanks in advance to everyone who's going to answer =) !
(As you surely noticed, English's not my first language. If everything sounds unclear, point it ut and i'll try to explain myself better!)