forking in c

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?
 
J

Joona I Palaste

Rv5 said:
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:

ISO standard C has no concept of "process". Please ask in
comp.unix.programmer. Thanks.
 
T

Tom St Denis

Joona I Palaste said:
ISO standard C has no concept of "process". Please ask in
comp.unix.programmer. Thanks.

Wow that was a quick reply. Like 40 seconds apart according to my server.

Tom
 
L

Lew Pitcher

Followups set to comp.unix.programming

Rv5 wrote:

A lot of stuff that's off-topic in comp.lang.c
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)

Bad! Bad coder! main() returns int, always. This should be
int 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();
Bad coder! You didn't compile /this/ code, did you? You have a syntax error.
}
exit(0)
Bad coder! Again with the syntax errors.
}

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?

Outside of failures in fork(), the parent /is/ creating new child processes
(two child, and one grandchild) at that point. The problem is that you don't
see them because they terminate very quickly
at least they dont
print out like the first one does. any ideas?

The two child processes you talk of don't have any executable printf()
statements.
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?
No.

or does it continue on from where the fork was called?

Yes, exactly.

The first fork() spawns a child process that continues executing (with a
zero return value) from the first fork() statement. The parent also
continues executing (with a different return value) from the first fork
statement.

The 1st child runs into the 1st if statement, evaluates false, and drops
into the else clause. It runs into the 2nd if statement, evaluates true, and
drops into the true side of the statement. It prints it's values, and
drops out of both if statements. The next statement the child executes is
the exit(0) statement.

The parent, otoh, runs into the 1st if statement, evaluates true, and drops
into the true side of the statement. It prints it's values, and forks a 2nd
child.

This fork() spawns a child process that continues executing (with a zero
return value) from the 2nd fork() statement. The parent also continues
executing (with a different return value) from the 2nd fork statement.

The 2nd child runs into the 3rd fork statement, and forks a child (its first
child, which is also the grandchild of the original parent process).

The 1st grandchild process continues executing (with a zero return value)
from the 3rd fork() statement. It's parent (the 2nd child process) also
continues executing (with a different return value) from the 3rd fork statement.

The grandchild process drops out of the if statements, and executes the
exit(0) statement.

The 2nd child process drops out of the if statements, and executes the
exit(0) statement.

The parent process runs into the 3rd fork statement, and forks a child (its
third child.

The 3rd child process continues executing (with a zero return value) from
the 3rd fork() statement. It's parent (the parent process) also continues
executing (with a different return value) from the 3rd fork statement.

The 3nd child process drops out of the if statements, and executes the
exit(0) statement.

The parent process drops out of the if statements, and executes the exit(0)
statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top