1

I want to know by the example below if the child's address space affect the parent's address space. So will the parent get the changed value of the child to 15 or it will get the value 5. I learnd that child's address space is not affected by its parent's one but what will be the output in this case? Will the output of value be 20 since it the value is incremented by 15 on child's part?

int value = 5;
int main(){
   pid_t pid;
   pid = fork();

   if (pid == 0){
    value +=15;
    return 0;
   }

   else if(pid > 0){
    wait(NULL);
    printf("Parent: value = '%d', value);    // Line A
    return 0;
   }
}
Konstantin
  • 133
  • 1
  • 8
Bre
  • 79
  • 2
  • 7

1 Answers1

0

Due to the definition of how fork does work in UNIX-like (see https://en.wikipedia.org/wiki/Fork_(system_call)) systems, definitely output will be 5, because child receives just a copy of parent's virtual memory. You may also refer to man(2) fork: https://linux.die.net/man/2/fork.

Konstantin
  • 133
  • 1
  • 8