Re: fork question
- From: Eric <nospam@xxxxxxxxx>
- Date: Sun, 22 Jan 2006 13:28:19 -0800
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
.
- Follow-Ups:
- Re: fork question
- From: David Schwartz
- Re: fork question
- References:
- fork question
- From: Eric
- Re: fork question
- From: David Schwartz
- fork question
- Prev by Date: Re: fork question
- Next by Date: Re: fork question
- Previous by thread: Re: fork question
- Next by thread: Re: fork question
- Index(es):
Relevant Pages
|