// JP opened flex table

Click to See Complete Forum and Search --> : forking and memory related question


ankursaxena
May 6th, 2004, 04:07 PM
Hi, I had a small question about forking a process.

I am reading someone's code who in his application creates a chunk of memory and say allocates it to variable ptr, now after that is done, he forks about 50 times and runs a function call passing the ptr pointer to it in the child processes...

now my question is, for all the 50 child processes...the ptr will be pointing to a different address in memory?(since they should get their own data segment) or is it the same mem?

Also if one of the child processes do a free on the ptr that was passed to the function call running there, will it affect any of the other processes??

I was a little confised on this.

thanx for all the help in advance.

Ankur

OReubens
May 6th, 2004, 04:26 PM
EAch process runs in it's own memory space. Programs do not 'see' eachothers data. A pointer that's valid in one program may not point to usefull data in another program.

However, there are ways in which a process can read memory from a specific location in that other process's memory space. There is however usually a performance penalty involved in this, as requesting memory that's owned by another process
(or memory that's shared amon processes) is 'slow'.

Another process can't just 'free' the pointer that points to memory in another process. There is the VirtualFreeEx() function, but that would require the memory to have been allocated with VirtualAlloc in the first place.

In either case, allocating memory, then spawning 50 child processes to do something with the data seems like a very inefficient solution to whatever the problem may be.

//JP added flex table