R
Rv5
im trying to write a program that has one parent process and three child
processes. each child process should have a child process of their own.
heres a really slim version of my code:
void main(void)
{
pid_t childpid = fork();
int status;
if (childpid > 0)
{
wait(&status);
printf("parent: pid = %ld\n", (long)getpid());
childpid = fork();
childpid = fork();
}
else if (childpid == 0)
{
printf("child: pid = %ld, parent = %ld\n", (long)getpid(), (long)getppid();
}
exit(0)
}
so as i understand it, the very first line creates the child process.
meanwhile the parent process will have a childpid greater then 0 and it will
print out its info, which it does. the original child too prints out its
info, but i cant understand why the other two calls to childpid=fork() in
the parent block dont seem to create new child processes? at least they dont
print out like the first one does. any ideas? im sure there is just a small
fundamental concept that im not getting that someone can clear up real
quick.
one concept im a little hazy on is where execution begins once the child is
created? does it stop back at the top of main? or does it continue on from
where the fork was called?
processes. each child process should have a child process of their own.
heres a really slim version of my code:
void main(void)
{
pid_t childpid = fork();
int status;
if (childpid > 0)
{
wait(&status);
printf("parent: pid = %ld\n", (long)getpid());
childpid = fork();
childpid = fork();
}
else if (childpid == 0)
{
printf("child: pid = %ld, parent = %ld\n", (long)getpid(), (long)getppid();
}
exit(0)
}
so as i understand it, the very first line creates the child process.
meanwhile the parent process will have a childpid greater then 0 and it will
print out its info, which it does. the original child too prints out its
info, but i cant understand why the other two calls to childpid=fork() in
the parent block dont seem to create new child processes? at least they dont
print out like the first one does. any ideas? im sure there is just a small
fundamental concept that im not getting that someone can clear up real
quick.
one concept im a little hazy on is where execution begins once the child is
created? does it stop back at the top of main? or does it continue on from
where the fork was called?