Loading ...
Sorry, an error occurred while loading the content.

fork() - understanding the memory space of parent and child

Expand Messages
  • vin_vrs
    My queries are : 1. how the fork actually works regarding the memory space of child and parent 2. the below code shows same address for a auto variable var ,
    Message 1 of 4 , Apr 3, 2009
    • 0 Attachment
      My queries are :
      1. how the fork actually works regarding the memory space of child and parent
      2. the below code shows same address for a auto variable "var" , but different values in case of parent and child.
      3. how could same address contain two different values.

      /* the code may look big, but its relatively simple, plz
      * do look into it
      */
      /* headers included */
      int main( int argc, char* argv[] ){
      pid_t pid;
      int var = 0;

      printf("before fork address of var = %x\n", &var);

      if( (pid = fork()) < 0 )
      perror("perror");
      if( pid == 0 ){

      printf("child process running pid = %d \n",getpid());
      var++;
      printf("child address of &var = %x\n", &var);
      printf("child value of var = %d\n", var);
      }
      else{
      wait();
      printf("parent process running = %d \n",getpid());
      printf("parent address of &var = %x\n", &var);
      printf("parent value of var = %d\n", var);
      }

      exit(0);

      }

      /* sample output */

      [root@localhost process]# cc p2.c -o p2.out
      [root@localhost process]# ./p2.out
      before fork address of var = bff5a700
      child process running pid = 3918
      child address of &var = bff5a700
      child value of var = 1
      parent process running = 3917
      parent address of &var = bff5a700
      parent value of var = 0


      Thanks in advance,
      Vinod
    • Joe Cheri Ross
      I am just making an assumption. May be only the offset is same; physical addresses may be different. Regards, Joe. ________________________________ From:
      Message 2 of 4 , Apr 3, 2009
      • 0 Attachment
        I am just making an assumption. May be only the offset is same; physical addresses may be different.

        Regards,
        Joe.



        ________________________________
        From: vin_vrs <vinod.chum@...>
        To: linux-bangalore-programming@yahoogroups.com
        Sent: Friday, 3 April, 2009 5:52:52 PM
        Subject: [blug-prog] fork() - understanding the memory space of parent and child


        My queries are :
        1. how the fork actually works regarding the memory space of child and parent
        2. the below code shows same address for a auto variable "var" , but different values in case of parent and child.
        3. how could same address contain two different values.

        /* the code may look big, but its relatively simple, plz
        * do look into it
        */
        /* headers included */
        int main( int argc, char* argv[] ){
        pid_t pid;
        int var = 0;

        printf("before fork address of var = %x\n", &var);

        if( (pid = fork()) < 0 )
        perror("perror" );
        if( pid == 0 ){

        printf("child process running pid = %d \n",getpid() );
        var++;
        printf("child address of &var = %x\n", &var);
        printf("child value of var = %d\n", var);
        }
        else{
        wait();
        printf("parent process running = %d \n",getpid() );
        printf("parent address of &var = %x\n", &var);
        printf("parent value of var = %d\n", var);
        }

        exit(0);

        }

        /* sample output */

        [root@localhost process]# cc p2.c -o p2.out
        [root@localhost process]# ./p2.out
        before fork address of var = bff5a700
        child process running pid = 3918
        child address of &var = bff5a700
        child value of var = 1
        parent process running = 3917
        parent address of &var = bff5a700
        parent value of var = 0

        Thanks in advance,
        Vinod





        Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/

        [Non-text portions of this message have been removed]
      • Sridhar.V.Iyer
        ... Pick any OS related book(Silberchantz is well known) for an in depth intro. ... Those are two different variables in two different address spaces. fork
        Message 3 of 4 , Apr 30, 2009
        • 0 Attachment
          On Fri, 2009-04-03 at 12:22 +0000, vin_vrs wrote:
          > My queries are :
          > 1. how the fork actually works regarding the memory space of child and
          > parent
          Pick any OS related book(Silberchantz is well known) for an in depth
          intro.
          > 2. the below code shows same address for a auto variable "var" , but
          > different values in case of parent and child.
          > 3. how could same address contain two different values.
          Those are two different variables in two different address spaces. fork
          makes a parent copy as child(i.e each of them has a copy of the variable
          var). vfork() will exhibit the behavior you might be looking for.

          - Sridhar
        • Rajesh T N
          Hi Vinod, calling fork or any family of fork calls will call the clone system call with different parameters. Most implementations of pthreads also call the
          Message 4 of 4 , May 2, 2009
          • 0 Attachment
            Hi Vinod,

            calling fork or any family of fork calls will call the "clone" system call
            with different parameters. Most implementations of pthreads also call the
            clone system call. In case of fork, the data section,stack/heap are copied.
            only the text section is common since it is supposed to be execute only
            whereas the other section contents could be modified. There is this COW
            (copy on write) feature wherein the data/stack/heap sections are replicated
            the first time any of its contents are modified in any of the 2 processes.
            In the below code, You are creaing a child process using the fork() lib call
            and waiting in the parent for the child to complete execution. In the child,
            from the function "main", you are modifying the contents of local (stack)
            variable "var" which means COW would occur and the stack is replicated.
            Which means that address (physical) of "var" is now "different" in the two
            processes but the virtual address can be same and hence the value of var in
            only the child is incremented. When I say "different". I'am referring to
            the physical memory address. You must remember that you are running the
            processes in different virtual memory spaces. So, the address "bff5a700" is
            the virtual address. But the physical address of "var" will be different for
            the two processes. The OS and MMU together make it transparent to the user
            space processes. To the user space, the entire memory address space is
            dedicated to itself. The actual virtual-physical address mapping is handled
            by the MMU/OS together.
            Hope this answers your Q

            Regards,
            -Rajesh TN



            On Fri, Apr 3, 2009 at 5:52 PM, vin_vrs <vinod.chum@...> wrote:

            > My queries are :
            > 1. how the fork actually works regarding the memory space of child and
            > parent
            > 2. the below code shows same address for a auto variable "var" , but
            > different values in case of parent and child.
            > 3. how could same address contain two different values.
            >
            > /* the code may look big, but its relatively simple, plz
            > * do look into it
            > */
            > /* headers included */
            > int main( int argc, char* argv[] ){
            > pid_t pid;
            > int var = 0;
            >
            > printf("before fork address of var = %x\n", &var);
            >
            > if( (pid = fork()) < 0 )
            > perror("perror");
            > if( pid == 0 ){
            >
            > printf("child process running pid = %d \n",getpid());
            > var++;
            > printf("child address of &var = %x\n", &var);
            > printf("child value of var = %d\n", var);
            > }
            > else{
            > wait();
            > printf("parent process running = %d \n",getpid());
            > printf("parent address of &var = %x\n", &var);
            > printf("parent value of var = %d\n", var);
            > }
            >
            > exit(0);
            >
            > }
            >
            > /* sample output */
            >
            > [root@localhost process]# cc p2.c -o p2.out
            > [root@localhost process]# ./p2.out
            > before fork address of var = bff5a700
            > child process running pid = 3918
            > child address of &var = bff5a700
            > child value of var = 1
            > parent process running = 3917
            > parent address of &var = bff5a700
            > parent value of var = 0
            >
            > Thanks in advance,
            > Vinod
            >
            >
            >



            --
            ______________________________

            Regards
            Rajesh T.N.


            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.