Home > Back-end >  Child processes are sharing the same variable in memory which is created after fork() is called (C)
Child processes are sharing the same variable in memory which is created after fork() is called (C)

Time:02-08

I am working on a script where fork() is called in a for loop to create many children, these children do a random number of loops and then print the name of the child

I'm having some odd behavior where the memory address that i is at is shared for all of the child processes even though the variable is created after the fork()

Is this the intended behavior? And if so is there any way for these variables to be stored at different memory indexes as I do not want another child process affecting another

Here is some example output from the print that is commented out in the childCall function

In child 1 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
In child 2 i is located at 0xbef49508
Child 2
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
In child 3 i is located at 0xbef49508
Child 3
In child 1 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 4 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
Child 4
In child 5 i is located at 0xbef49508
Child 1
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
In child 5 i is located at 0xbef49508
Child 5

Below is the for loop in the parent function that creates the child processes and below that is the childCall function

Thank you!

for(int i = 0; i<children; i  )
    {
        if(fork() == 0) //If I am the child
        {
            childCall(i   1);
            return 0;
        }
    }

The childCall function is as follows

void childCall(int children)
{
    for(int i = 0; i<children; i  )
    {   
        //printf("In child %d i is located at %p\n", children, &i);
        int newrand = rand() % 2; //This implementation is because when I was declaring a random value before the loop all of the children shared the same random value, due to pseudorandomness!
        //printf("%d is the random number\n", newrand);
        i -= newrand;
    }
    printf("Child %d\n", children);
}

CodePudding user response:

Every child created by fork gets its own independent virtual memory space. So while the variables are at the same address within each process's virtual memory, the fact that they are separate processes means that they are separate and modifications in one process have no effect on any other process.

  •  Tags:  
  • Related