734 views
0 votes
0 votes
#include  <stdio.h>
#include  <sys/types.h>

#define   MAX_COUNT  200

void  ChildProcess(void);                /* child process prototype  */
void  ParentProcess(void);               /* parent process prototype */
void  main(void)
{
      pid_t  pid;
     
      pid = fork();
      if (pid == 0)
         { int x1= 112;
          
             float x2 = 10.4;
            printf("the address is %p x1\n",&x1);
             printf("the address is %p x2\n",&x2);
ChildProcess();}
      else
        {float x3 =45.5;
        
        float x4 = 10.4;
       
        printf("the address is %p x3\n",&x3);
        printf("the address is %p x4\n",&x4);
ParentProcess();}
    
}

void  ChildProcess(void)
{
       printf("   *** Child process  ***\n");

}

void  ParentProcess(void)
{
      printf("*** Parent*****\n");

}

1 Answer

0 votes
0 votes
It happens because of the concept  of Virtual memory. Each of the process has its own view of the memory and cannot access the memory of other process.

When you exectue the command fork(), the system copies the address space of the parent to that of child, because of this the virtual address remains the same but the underlying physical address will be different.

When we print the address of a variable using the '&' operaor, that prints the virtual address and not the physical address and hence you see the same address of the two variables.

Related questions

0 votes
0 votes
1 answer
2
Erwin Smith asked Apr 11, 2023
758 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...
1 votes
1 votes
2 answers
4
♥_Less asked Jan 12, 2018
1,750 views
What is the number of child process created ?Answer given was 63, BUT i am getting 9 !