I've had to piece this together from documentation, so if you could just confirm my conclusions and correct me (if necessary), that'd be awesome. The only good tutorial I could find on the topic is at fscked.org, and it's full of "TO DO" pages.
If you'll recall from a previous thread, I have a program in which the "server" process spawns a number of "client" threads (this is for a shell - so I'm using the term server in the same sense as X-Server). The server maintains a doubly-linked list to represent all the clients, with each client having a pointer to the node that represents it. Clients can be removed, inserted, and moved around by the server (hence the double-linked list structure).
This wasn't working, and that's how my attention got drawn to the fact that malloc'ed memory isn't shared between processes, even if you fork after malloc'ing it (I was very surprized - is this correct?). So I looked at shared memory, and it appears I have two options: System V's IPC, and BSD's mmap. It's my understanding that mmap is the more popular of the two choices, so I've chosen it (thoughts on this?)
Now the actual calls to mmap and munmap are what's confusing me.
Where myPointer is just like the pointer return by malloc. I'd like this to act just like malloc'ed memory, except for the fact that it is shared (meaning, I don't want to actually have files written to the disk - I just want it in RAM). Other than that, I really don't care where it is, hence the NULL and 0's. Does this call look right? Have I provided the right flags, etc...?Code:myPointer = (myType *)mmap(0, sizeof(myType), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, NULL, 0);
Then when I call munmap, I would do this:
Is THAT correct? All the man pages I found listed the first argument for both methods as being "void *start", but all the examples I found specified 0 as the first parameter for mmap. If that's not right, my question is - how does munmap know what memory to free, since I've created an arbitrary number of clients with mmap, and I just close them one by one as the user dictates.Code:myPointer = (myPointer, sizeof(myType));
Again - just a confirmation / correction is all I'm looking for, but if you have any thoughts on how I could go about this a better way - I'm all ears.