forking in c

Discussion in 'C Programming' started by Rv5, Nov 4, 2003.

  1. Rv5

    Rv5 Guest

    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?
     
    Rv5, Nov 4, 2003
    #1
    1. Advertising

  2. Rv5 <> scribbled the following:
    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I wish someone we knew would die so we could leave them flowers."
    - A 6-year-old girl, upon seeing flowers in a cemetery
     
    Joona I Palaste, Nov 4, 2003
    #2
    1. Advertising

  3. Rv5

    Tom St Denis Guest

    "Joona I Palaste" <> wrote in message
    news:bo921e$gss$...
    > Rv5 <> scribbled the following:
    > > 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.


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

    Tom
     
    Tom St Denis, Nov 4, 2003
    #3
  4. Rv5

    Lew Pitcher Guest

    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.


    --
    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Nov 4, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SRam

    Forking server

    SRam, Aug 21, 2003, in forum: Perl
    Replies:
    1
    Views:
    3,027
  2. SRam

    Forking Server

    SRam, Sep 15, 2003, in forum: Perl
    Replies:
    0
    Views:
    562
  3. Matt Stevens

    Forking HTTP Daemon - Problem

    Matt Stevens, Dec 8, 2003, in forum: Perl
    Replies:
    1
    Views:
    688
    Jim Gibson
    Dec 9, 2003
  4. Ed
    Replies:
    0
    Views:
    709
  5. James

    forking and using MySQL

    James, May 15, 2004, in forum: Perl
    Replies:
    1
    Views:
    543
    Mothra
    May 19, 2004
Loading...

Share This Page