Re: fork question



David Schwartz wrote:

>
> "Eric" <nospam@xxxxxxxxx> wrote in message
> news:EeKdnbFBwIw0aU7enZ2dnUVZ_tOdnZ2d@xxxxxxxxxxxxxx
>
>> if i use fork in my userland app, how do share a global variables between
>> the parent and all the children? I need to have this variable created
>> such that if anyone (parent or child) changes it all the other children
>> and the parent can see the change. My only idea is a mmap'd space, I
>> could let each
>> process mmap the same space and use it for various shared variables, kind
>> of like this:
>> parent: create typedef of layout of mapped space
>> parent: sp=mmap new shared space based on size of layout
>> parent: sp->var1 = value
>> parent: sp->var2 = value
>> child1: sp=mmap existing shared space
>> child1: sp->var1 = value
>> child1: if(sp->var2 == value)...
>> child2: sp=mmap existing shared space
>> child2: sp->var1 = value
>> child2: if(sp->var2 == value)...
>> Is there another better way?
>
> Have the parent create a shared memory area before it 'fork's.
> However,
> you seem to be missing the important detail that if two processes are
> going to share a memory area, they must synchronize their accesses to it
> (or use atomic operations).
>
> DS
Wow, fast answer, thanks.
Well, most of it is that the child reads parent set data, but also, i have a
couple of shared ints that can be set by anyone which are used as yes/no
flags. once set, they never change but are read (ie checked) fairly often
by the reset of the processes.

Another q.
I'm creating the mmap'd are like this (shortened for clarity):

Global declarations:
void *mptr;
typedef struct _TSharedAreaMap {
struct info sinfo[MAX_SINFOs];
int Flag1;
volatile int AbortOps;
}TSharedAreaMap;
TSharedAreaMap *SharedAreaMap;

Mapping function:
fd = open(SharedBackingFile, O_RDWR);
mptr = mmap(0, Len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
SharedAreaMap = (TSharedAreaMap *)mptr;

Parent and child both call this but only the parent creates the backing
file. Is that corect?

Thanks
Eric

.



Relevant Pages

  • Re: Update Parent-Child-Grandchild Table
    ... hierarchy with three levels? ... I am also trying to update the parent at level 2 and> the child at level 3. ... > Parent INT NOT NULL, ...
    (microsoft.public.sqlserver.programming)
  • Re: fork question
    ... > Well, most of it is that the child reads parent set data, but also, i have ... There's no problem with the child making its own copy ... > volatile int AbortOps; ... > TSharedAreaMap *SharedAreaMap; ...
    (comp.os.linux.development.apps)
  • Re: About sleep() in child process
    ... int main{ ... beacuse of the scheduler.Due to which the loop in the parent is ... Child: PID is 11059 ... Parent: PID is 11058 ...
    (comp.lang.c)
  • Re: Quicksort
    ... replying to appeared to be missing a level of quote indicators. ... parent =; ... child = parent * 2; ... void Swap(int *arr, int i, int j) ...
    (comp.programming)
  • Re: unix pipe problems
    ... that's one way for the parent to tell the child it's finished sending. ... child reads from the pipe it will have wordsize initialized. ... int main ...
    (comp.unix.programmer)