0

I recently learned about the functioning of fork() in c++. I came across 2 questions which i can't find any reasons about.

  1. Here although i am getting the same address of a in both child and parent but the values are different can you please explain reason for this behaviour.
  2. Is there any way such that making any change in parent gets reflected in child or vice versa without using files to communicate between the two
#include <bits/stdc++.h>
#include<unistd.h>
#include<stdlib.h>
using namespace std;

signed main() {
    int *a=(int *)malloc(sizeof(int));
    *a=5;
    cout<<a<<endl;
    int pid=fork();
    if(pid!=0)
    {
        sleep(2);
        *a+=2;
        cout<<a<<" "<<*a<<" "<<"PARENT"<<endl;
    }
    else
    {
        *a+=1;
        cout<<a<<" "<<*a<<" "<<"CHILD"<<endl;
    }

}

This is the output of above code.

0x561d5c19beb0
0x561d5c19beb0 6 CHILD
0x561d5c19beb0 7 PARENT

I was expecting the answer to be

0x561d5c19beb0
0x561d5c19beb0 6 CHILD
0x561d5c19beb0 8 PARENT

OR

0x561d5c19beb0
<different_value> 6 CHILD
0x561d5c19beb0 7 PARENT

But the answer seems completely unintuitive and illogical.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
raj
  • 1
  • 4
  • 2
    1. Because memory spacees of each *processes* are isolated using *virtual memory* tecnology. – MikeCAT Mar 12 '23 at 15:08
  • thats virtual memory for you – Daniel A. White Mar 12 '23 at 15:09
  • @MikeCAT Can you guys elaborate but what it is and isn't it the same position in ram – raj Mar 12 '23 at 15:11
  • @DanielA.White Can you guys elaborate but what it is and isn't it the same position in ram – raj Mar 12 '23 at 15:11
  • @raj See the answers to [this question](https://stackoverflow.com/questions/14347206/what-are-the-differences-between-virtual-memory-and-physical-memory). Essentially, the OS works with the CPU's memory management unit to set up a translation table for each process that maps the process's virtual memory addresses to physical memory addresses. This makes it appear to each process that it has access to all of the available system memory (plus a swap/page file, if in use) and keeps them from writing over each other (by accident or on purpose). – Miles Budnek Mar 12 '23 at 16:03
  • Should be taged C, not C++. – Red.Wave Mar 12 '23 at 17:14

2 Answers2

0

Here although i am getting the same address of a in both child and parent but the values are different can you please explain reason for this behavior.

Memory spaces of different processes are generally separate. You can't usually access memory of one process from another process. Typically memory is also isolated for security purposes on the CPU level by having the actual memory addresses visible to the process be virtual and transparently mapped to distinct real memory addresses, controlled by the operating system.

Is there any way such that making any change in parent gets reflected in child or vice versa without using files to communicate between the two

Your operating system will have functionality to make certain memory regions shared between multiple processes. You would need to use those. For linux see e.g. this question or for a more portable C++ library see e.g. boost::interprocess.

But if you actually want memory to be shared in general, then don't use fork to create multiple processes. Use multiple threads in the same process instead.

user17732522
  • 53,019
  • 2
  • 56
  • 105
0

First fork is not part of C++, it is part of Unix (e.g. UNIX, Linux, BSD).

Fork makes a copy of the process (your OS knows a cheap/fast way to do this). Each process parent, and child, continue after the copy.

Imagine putting a sheet of paper on a photocopier. Then giving the output to a new person. They can write on their copy, you can write on yours.

If you need to share memory (usually not a good idea: mutation is bad, mutation with concurrency is very bad), then use threads, not processes.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52