Reputation: 75
I want to use fork() to create a child that continues create another child (grandchild of the parent process). But I cannot seem to create a grandchild with this code. Is there anything wrong here?
#include <stdio.h>
#include <unistd.h>
void child_A_proc()
{
while (1)
{
fprintf(stdout, "%s", "A");
fflush(stdout);
}
}
void parent_proc()
{
while (1)
{
write(1, "B", 1);
}
}
void child_of_child_A_proc()
{
while (1)
{
fprintf(stdout, "%s", "C");
fflush(stdout);
}
}
int main(void)
{
if (fork() == 0){
child_A_proc();
if (fork() == 0)
child_of_child_A_proc();
else
child_A_proc();
}
else
parent_proc();
return 0;
}
Upvotes: 0
Views: 56
Reputation: 43197
Check for calling functions that do not return.
if (fork() == 0){
child_A_proc();
/* unreachable */
if (fork() == 0)
child_of_child_A_proc();
else
child_A_proc();
}
Upvotes: 3