Quote:
Originally Posted by
hurricane86
Hi everbody, i have a problem with shared memory and child-processes in C (unix).
I have a server that do forks to create (N) child processes. This processes work with a shared "stuct" thanks to shared memory and a semaphore. The problem is when a child modify the shared memory and the others can't see its changes...but if i done it in the main process it work fine.
finally, the question is:
can a child-process "realloc" a shared memory and the others can see it?
Sorry for my bad english, thanks a lot bye.
Once new processes fork off, their heap and stack spaces are not shared. In fact, neither are their memory mappings -- changing the
mapping of one process won't change the mapping in the others, so you can't enlarge the memory in one and expect the others to mirror it. So somehow, they all have to have the right mapping in the first place.
One way this could work is to
mmap() a file into memory with the MAP_SHARED flag, and give the mapping a much longer length than the length of the file. It will let you do this, it'll just give you a
bus error signal when you use memory outside the bounds of the file. You can extend the size of the file with
truncate() when you need to. You'll need to remember which bits of file/memory are being used by yourself.