return of fork()? - Minix

This is a discussion on return of fork()? - Minix ; Hi everyone. I am still struggling at the very start. I wrote this simple c++ toy program to test function fork(). The program runs but the output shows that I dont properly understand what the fork() call does. I only ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: return of fork()?

  1. return of fork()?

    Hi everyone.
    I am still struggling at the very start.
    I wrote this simple c++ toy program to test function fork(). The program
    runs but the output shows that I dont properly understand what the
    fork() call does.

    I only expected 2 lines of output showing the process ID of the parent
    and child.

    --------------------------------------

    code:

    #include
    #include #include

    using std::cout;
    using std::endl;

    int main()
    {
    pid_t origprocess = getpid();
    cout << "This program has a process ID of " << origprocess << endl;

    // fork creates a copy
    pid_t copyp;
    copyp = fork();
    cout << "The copy has a process ID of " << copyp << endl;

    return 0;
    }

    ----------------

    output:
    This program has a process ID of 634
    The copy has a process ID of 635
    $ The copy has a process ID of 0;

    ----------------

    I dont understand why the copy has a process ID of 635 and also 0?

    can you please explain this for me in simple terms?

    Thanks.

  2. Re: return of fork()?

    neil wrote:
    > Hi everyone.
    > I am still struggling at the very start.
    > I wrote this simple c++ toy program to test function fork(). The program
    > runs but the output shows that I dont properly understand what the
    > fork() call does.
    >
    > I only expected 2 lines of output showing the process ID of the parent
    > and child.
    >
    > --------------------------------------
    >
    > code:
    >
    > #include
    > #include > #include
    >
    > using std::cout;
    > using std::endl;
    >
    > int main()
    > {
    > pid_t origprocess = getpid();
    > cout << "This program has a process ID of " << origprocess << endl;
    >
    > // fork creates a copy
    > pid_t copyp;
    > copyp = fork();
    > cout << "The copy has a process ID of " << copyp << endl;
    >
    > return 0;
    > }
    >
    > ----------------
    >
    > output:
    > This program has a process ID of 634
    > The copy has a process ID of 635
    > $ The copy has a process ID of 0;
    >
    > ----------------
    >
    > I dont understand why the copy has a process ID of 635 and also 0?
    >
    > can you please explain this for me in simple terms?
    >
    > Thanks.


    The fork() call creates a process. The return from fork() is 0 in the
    child and the child's process number in the parent.

    Both the child and the parent do the cout. You're getting exactly the
    result you should. The child is printing 0 and the parent is printing 635.

    -Rich

  3. Re: return of fork()?

    Hi Neil:

    1) When you call fork, a new process is created.
    2) The new process is an exact copy of the process that made the call
    to fork.
    3) The return parameter of fork will be 0 within the child process and
    it will be equal to the newly created child process' id within the
    parent process.
    4) This means, that you can use fork's return value in order to execute
    one section or the other; e.g.:

    if ((result = fork()) == 0) then
    "I am the child process " << endl;
    else
    "I am the parent process and my child has a process ID of " << result
    << endl;

    Regards,
    Martin

    neil wrote:
    > Hi everyone.
    > I am still struggling at the very start.
    > I wrote this simple c++ toy program to test function fork(). The program
    > runs but the output shows that I dont properly understand what the
    > fork() call does.
    >
    > I only expected 2 lines of output showing the process ID of the parent
    > and child.
    >
    > --------------------------------------
    >
    > code:
    >
    > #include
    > #include > #include
    >
    > using std::cout;
    > using std::endl;
    >
    > int main()
    > {
    > pid_t origprocess = getpid();
    > cout << "This program has a process ID of " << origprocess << endl;
    >
    > // fork creates a copy
    > pid_t copyp;
    > copyp = fork();
    > cout << "The copy has a process ID of " << copyp << endl;
    >
    > return 0;
    > }
    >
    > ----------------
    >
    > output:
    > This program has a process ID of 634
    > The copy has a process ID of 635
    > $ The copy has a process ID of 0;
    >
    > ----------------
    >
    > I dont understand why the copy has a process ID of 635 and also 0?
    >
    > can you please explain this for me in simple terms?
    >
    > Thanks.



  4. Re: return of fork()?

    Thanks, but I am still missing something.!! It does NOT make sense to me
    to say fork creates 'a new process'. The way I understand things from
    this output is that a fork()call is actually creating 2 processes:

    ----------------------
    Original Process

    Parent Process Child Process

    ----------------------
    Is this right?
    This would make grammatical sense also because a 'fork' traditionally is
    a two-pronged thing.

    Can you confirm or explain this for me please?

    thanks

  5. Re: return of fork()?

    neil wrote:
    > Thanks, but I am still missing something.!! It does NOT make sense to me
    > to say fork creates 'a new process'. The way I understand things from
    > this output is that a fork()call is actually creating 2 processes:
    >
    > ----------------------
    > Original Process
    >
    > Parent Process Child Process
    >
    > ----------------------
    > Is this right?
    > This would make grammatical sense also because a 'fork' traditionally is
    > a two-pronged thing.
    >
    > Can you confirm or explain this for me please?
    >
    > thanks


    Martin's example was a good one. Usually if(fork() == 0) is used to
    determine whether the parent or child is running.

    The only thing your missing is that fork() doesn't create 2 processes.
    The parent already exists. It is the process that executes the fork()
    call. The fork call creates a child process. The parent and child then
    both run the same code until something makes them do something differemt
    (like the if() check).

    In a command shell, for example, the parent is the shell. When you time
    a command it is executed as a child process:

    .... // parent running
    if (fork() == 0) {
    // This is the child.
    exec
    } else {
    // This is the parent.
    wait(); // The parent waits for the command to finish.
    }

    That's not real code, of course, but I hope it helps.

    -Rich


  6. Re: return of fork()?

    Richard Pennington wrote:
    > neil wrote:
    >> Thanks, but I am still missing something.!! It does NOT make sense to
    >> me to say fork creates 'a new process'. The way I understand things
    >> from this output is that a fork()call is actually creating 2 processes:
    >>
    >> ----------------------
    >> Original Process
    >>
    >> Parent Process Child Process
    >>
    >> ----------------------
    >> Is this right?
    >> This would make grammatical sense also because a 'fork' traditionally
    >> is a two-pronged thing.
    >>
    >> Can you confirm or explain this for me please?
    >>
    >> thanks

    >
    > Martin's example was a good one. Usually if(fork() == 0) is used to
    > determine whether the parent or child is running.
    >
    > The only thing your missing is that fork() doesn't create 2 processes.


    Well how come in my original toy program I got 3 *different pid*
    outputs? First the pid of the original process. Then second another
    different pid then third a pid that was equal to 0?

    Basically what I think is that the 'Original Process' is different from
    the 'Parent Process'. If they are the same, my program should have shown
    the same pid in the first 2 outputs - it didnt, it showed two different
    pids.!!


    > The parent already exists. It is the process that executes the fork()
    > call. The fork call creates a child process. The parent and child then
    > both run the same code until something makes them do something differemt
    > (like the if() check).



  7. Re: return of fork()?

    neil wrote:
    > Richard Pennington wrote:
    >> neil wrote:
    >>> Thanks, but I am still missing something.!! It does NOT make sense to
    >>> me to say fork creates 'a new process'. The way I understand things
    >>> from this output is that a fork()call is actually creating 2 processes:
    >>>
    >>> ----------------------
    >>> Original Process
    >>>
    >>> Parent Process Child Process
    >>>
    >>> ----------------------
    >>> Is this right?
    >>> This would make grammatical sense also because a 'fork' traditionally
    >>> is a two-pronged thing.
    >>>
    >>> Can you confirm or explain this for me please?
    >>>
    >>> thanks

    >>
    >> Martin's example was a good one. Usually if(fork() == 0) is used to
    >> determine whether the parent or child is running.
    >>
    >> The only thing your missing is that fork() doesn't create 2 processes.

    >
    > Well how come in my original toy program I got 3 *different pid*
    > outputs? First the pid of the original process. Then second another
    > different pid then third a pid that was equal to 0?




    output:
    This program has a process ID of 634
    ---> Parent PID
    The copy has a process ID of 635
    ---> Child PID (as printed by father)
    $ The copy has a process ID of 0;
    ---> Child PID (as printed by child)

    fork() returns the PID of the child to the father and 0 to the child.

    > Basically what I think is that the 'Original Process' is different from
    > the 'Parent Process'. If they are the same, my program should have shown
    > the same pid in the first 2 outputs - it didnt, it showed two different
    > pids.!!


    Parent and child share the same code, but use different data area.

    Ciao
    Giovanni
    --
    A computer is like an air conditioner,
    it stops working when you open Windows.
    Registered Linux user #337974 < http://giovanni.homelinux.net/ >

  8. Re: return of fork()?


    neil writes:

    > Richard Pennington wrote:
    > > neil wrote:
    > >> Thanks, but I am still missing something.!! It does NOT make sense
    > >> to me to say fork creates 'a new process'. The way I understand
    > >> things from this output is that a fork()call is actually creating 2
    > >> processes:
    > >>
    > >> ----------------------
    > >> Original Process
    > >>
    > >> Parent Process Child Process
    > >>
    > >> ----------------------
    > >> Is this right?
    > >> This would make grammatical sense also because a 'fork'
    > >> traditionally is a two-pronged thing.
    > >>
    > >> Can you confirm or explain this for me please?
    > >>
    > >> thanks


    > > Martin's example was a good one. Usually if(fork() == 0) is used to

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > > determine whether the parent or child is running.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > > The only thing your missing is that fork() doesn't create 2
    > > processes.

    >
    > Well how come in my original toy program I got 3 *different pid*
    > outputs? First the pid of the original process. Then second another
    > different pid then third a pid that was equal to 0?


    'man fork', especially the section on return values. Think it through.

    > Basically what I think is that the 'Original Process' is different
    > from the 'Parent Process'. If they are the same, my program should
    > have shown the same pid in the first 2 outputs - it didnt, it showed
    > two different pids.!!


    So if you (think you) know better, why do you even ask? Bah! Did you
    even read the text you quoted (esp. the parts I undelined)?

    > > The parent already exists. It is the process that executes the
    > > fork() call. The fork call creates a child process. The parent and
    > > child then both run the same code until something makes them do
    > > something differemt (like the if() check).


    Perhaps 'man getpid' would also be useful reading in your case. If you
    actually think you're right with "the 'Original Process' is different
    .... it showed two different pids" I suggest you try the experimental
    proof (using getpid()). Experiments and reading it up are both valid
    methods to learn something. Try one of them.

    Regards -- Markus

  9. Re: return of fork()?


    Giovanni writes:

    > neil wrote:
    > > Richard Pennington wrote:
    > >> neil wrote:
    > >>> Thanks, but I am still missing something.!! It does NOT make sense
    > >>> to me to say fork creates 'a new process'. The way I understand
    > >>> things from this output is that a fork()call is actually creating
    > >>> 2 processes:
    > >>>
    > >>> ----------------------
    > >>> Original Process
    > >>>
    > >>> Parent Process Child Process
    > >>>
    > >>> ----------------------
    > >>> Is this right?
    > >>> This would make grammatical sense also because a 'fork'
    > >>> traditionally is a two-pronged thing.
    > >>>
    > >>> Can you confirm or explain this for me please?
    > >>>
    > >>> thanks
    > >>
    > >> Martin's example was a good one. Usually if(fork() == 0) is used to
    > >> determine whether the parent or child is running.
    > >>
    > >> The only thing your missing is that fork() doesn't create 2 processes.

    > > Well how come in my original toy program I got 3 *different pid*
    > > outputs? First the pid of the original process. Then second another
    > > different pid then third a pid that was equal to 0?

    >
    >
    >
    > output:
    > This program has a process ID of 634
    > ---> Parent PID
    > The copy has a process ID of 635
    > ---> Child PID (as printed by father)
    > $ The copy has a process ID of 0;
    > ---> Child PID (as printed by child)
    >
    > fork() returns the PID of the child to the father and 0 to the child.
    >
    > > Basically what I think is that the 'Original Process' is different
    > > from the 'Parent Process'. If they are the same, my program should
    > > have shown the same pid in the first 2 outputs - it didnt, it showed
    > > two different pids.!!

    >
    > Parent and child share the same code, but use different data area.


    Basically yes, but only if there is a seperate I+D setup. If code and
    data live in the same segment and there is no virtual memory
    management (i.e. paging and perhaps a copy-on-write) then the code
    will also have to be copied. This is the way it worked on PDP/11s with
    small memory and Unix V7 or the BSD2 backports. This is also the way
    it worked in Minix 1.x and 2.x (especially on home computers like the
    Atari).

    Regards -- Markus




  10. Re: return of fork()?


    Hi Neil.

    neil writes:

    > Thanks, but I am still missing something.!! It does NOT make sense to
    > me to say fork creates 'a new process'. The way I understand things
    > from this output is that a fork()call is actually creating 2 processes:
    >
    > ----------------------
    > Original Process
    >
    > Parent Process Child Process
    >
    > ----------------------
    > Is this right?


    No.

    This is right:

    Parent_Process Pid1
    |
    fork()
    |\_
    | \
    | \
    | \
    | \
    Parent Child
    Process Process

    Pid1 Pid2


    > This would make grammatical sense also because a 'fork' traditionally
    > is a two-pronged thing.


    You can see the 'fork'-structure above? The process is forked in two.

    "Make grammatical sense" is of course magic thinking. If fork would be
    called start_the_missiles() it still wouldn't destroy the world. Many
    names are historic and don't make common sense all the time. fork(),
    though, does.

    But I seriously suggest you read that up in some book (like OSDI, page
    22ff in my copy of OSDI/2). fork() is really elementary and if you
    don't read your text books (first!) you're probably out of luck asking
    here. At least with me :-). Can't speak for the rest.

    >
    > Can you confirm or explain this for me please?


    Unconfirm. Explanation above.

    Regards -- Markus

  11. Re: return of fork()?

    > Basically what I think is that the 'Original Process' is different from
    > the 'Parent Process'. If they are the same, my program should have shown
    > the same pid in the first 2 outputs - it didnt, it showed two different
    > pids.!!


    No! You got it wrong. Run this and you'll see what I mean:


    #include
    #include #include

    using std::cout;
    using std::endl;

    int main()
    {
    pid_t origprocess = getpid();
    cout << "Hi! I am a process. My id is " << origprocess
    << ". I will now give birth to a child!" << endl;

    pid_t result;
    if ((result = fork()) == 0) then
    cout << "I am the child process; fork() returned "
    << result << " to me, but that does NOT mean my id is "
    << result << "!! My id is " << getpid()
    << "!!! My parent has an id of " << origprocess << endl;
    else
    {
    origprocess = getpid();
    cout << "Do you remember me? I am a process. My id STILL is "
    << origprocess << ". I am now a parent of 1! My child has an id
    of "
    << result << "." << endl;
    }
    return 0;
    }


  12. Re: return of fork()?

    I finally think I have got this into my thick head:

    The output that my program gave was:
    1. The return from get pid for the original process - which has nothing
    at all to do with fork()!!

    2. The pid of the newly created process &

    3. The null value 0 returned by the function within the child process.

    thanks

+ Reply to Thread