Re: forking in c

From: Rv5 (rolney_at_dslextreme.com)
Date: 11/04/03


Date: Tue, 4 Nov 2003 14:02:58 -0800

Thanks to all who replied. I understand much better now. The program is
almost working. It works about half the time just as I expect it to.
Sometimes however, the very last child it prints out says it has a parent id
of 1, instead of xxxx like the rest of them do. Here is my code, is there
any little improvements you think I can make?
Once again, Im supposed to get 3 children with the same parent, and then
each child is supposed to have 1 child of its own. 3 children, 3
grandchildren.

void main(void)
{
    pid_t childpid = fork();
    int status;
    int i;

    if (childpid)
    {
        wait(&status);
        printf("parent: pid = %ld\n", (long)getpid());
        for (i = 0; i < 2; i++)
        {
            childpid = fork();
            if (!childpid)
                break;
        }
    }

    if (!childpid)
    {
        childpid = fork();
        wait(&status);
        if (childpid)
            printf("child: pid = %ld, parent = %ld\n", (long)getpid(),
(long)getppid());
        else
            printf("grandchild: pid = %ld, parent = %ld\n", (long)getpid(),
(long)getppid());
    }
    exit(0)
}

Just a couple of quick notes. if there are minor syntax errors I apologize.
This is not a copy and paste of my code, but me manually typing whats on one
machine to another. It does compile and run. As for the void main(void), i
know thats bad, but for whatever reason thats the way the teacher did it in
his example. We questioned him, but he said it will still run, so I'm just
following his example as long as he'll be the one grading. Wouldnt do it on
my own though.

Thanks a bunch guys. I understand much better then I even did just this
morning.

"Lew Pitcher" <Lew.Pitcher@td.com> wrote in message
news:cpUpb.742$143.35111@news20.bellglobal.com...
> Followups set to comp.unix.programmer (was comp.unix.programming, but no
> such newsgroup exists on my ISPs server)
>
> 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.
>
>
> --
> Lew Pitcher, IT Consultant, Application Architecture
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed here are my own, not my employer's)
>
>



Relevant Pages

  • Re: The DeleteCommand affected 0 records
    ... The Delete/UpdateCommand affected 0 records ... Parent and child. ... in the parent, the child row is also marked as deleted. ... recieve an error when the dataadapter of the childtable executes the ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Basic Inter process communication question
    ... I have some general questions about UNIX/Solaris IPC. ... child process and the wait for the child process to complete. ... The twist is that I don¹t want the memory of the parent process to be ...
    (comp.unix.programmer)
  • Re: problem with WaitForSingleObject on WM5
    ... signaled after child exit and it wait for long until new child create. ... BTW is there other way for parent process to know about child exit? ... I want to wait for child process shutdown, ...
    (microsoft.public.windowsce.embedded)
  • Re: Blocking system calls, and pthreads
    ... > Maybe it's just easier than forking and having a child process ... > that has all heap allocations and open files from the parent, ... > be used by the child. ...
    (comp.unix.programmer)
  • Re: anonymous pipes
    ... redirecting input and output of the child process i have to use two ... parent say's "hello child" then child should respond "hello ... multiple times and then child should respond multiple times) ...
    (microsoft.public.vc.mfc)