![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ELF format shared library? | wntrmead | High Level Programming | 11 | 11-16-2007 02:19 PM |
| shared library | areef4u | UNIX for Advanced & Expert Users | 1 | 08-01-2006 03:14 AM |
| Shared memory shortage but lots of unused memory | cjcamaro | UNIX for Advanced & Expert Users | 1 | 10-13-2004 06:10 PM |
| Shared Library Problem | vinz | HP-UX | 1 | 08-27-2004 11:57 AM |
| Shared Library | rajashekaran | High Level Programming | 2 | 08-03-2002 02:59 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Shared memory in shared library
I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between instances of the shared library. However, what I do need to worry about is initializing the memory once and never again.
I would love all the shared libraries to have a library scoped variable, but I don't think that's possible. If it is, let me know...but I know normally each process gets its own copy of the shared library's data segment. I don't think there is a way to flag a portion of that data segment as shared across all instances of the library. So, I guess I'll need to have the library access the DB in shared memory. However, the first instance of the library to be started up needs to create the shared memory, attach to it, and load it all before any other possible instances can use it. How do I guarentee that this create/initialize happens once before the shared memory is used? At first, I thought the non-existance of the shared memory segment would do it...but there exists a race condition between create and initialize where-in another instance of the library would see the shared memory but not be able to access it because it has not yet been inited. I know I can store posix mutex's in shared memory...so I guess I can use one of them. However, the other problem exists when the shared memory segment is "left over" from previous runs. What happens then? So...all library's "detach" from the segment, but none of them destroy it. Now what...the next time the library starts fresh it should re-init the shared memory, but won't. Hummm...any ideas? Some direction? What is the common method of providing shared memory syncronization? Should I use a system semaphore? Wouldn't it suffer from the same "warm" start problem (detached processes from shared library but memory/semaphores still resident). This will be written for AIX 5, BTW, for those interested. |
|
||||
|
You could have an additional process who's job it is to create and shutdown the shared memory. This process could have some form of IPC to so the other processes can attach and detach, when the last goes, the manager process can clean up and die.
You could have the manage process fork/exec'd by the client libraries when they start so it does not have to prestart, and have setuid bit to run with the appropriate rights other than the first user to start up. If the clients connect to the manager process with a UNIX domain socket then the manager could use poll() on all connections to monitor that the processes are alive, if they exit uncleanly the socket connection will still die. |
|
||||
|
Quote:
Hummm.... Another shared memory question: Storing pointers in shared memory (of course to other areas of shared memory), can this be done? I think the answer lies in how you attach the shared memory to the process, no? Looking at the shmat call, the second parameter specifies the memory address. I assume this is the "base address" given to the process for the shared memory segment. I would guess that pointers will only be valid throughout all applications accessing the shared memory iff all applications specify this parameter similarly when attaching the segment. Is this correct? If the above is correct, then I would guess that I may compete with other applications for the address I want to attach to (being a library and all) and that I may not easily be able to guarantee that I can get the address I want. To prevent this, it seems the OS allows one to pass NULL to this parameter and the OS will chose an available address to map to. In which case, I could not really store pointers in shared memory, rather I must store offsets and let the application compute the actual pointer value by adding its individual "base address." This is obviously performance draining.... So...how is that "obstacle" usally overcome? |
|
||||
|
Quote:
2. attach the shared memory as early as possible in the process startup to prevent that memories use by other later activities. 3. if you fork, you will have two processes using the shared memory at the same address, I can't confirm if when you exec() the shared-memory actually gets detached. Would be worth finding out? |
|
||||
|
Quote:
Humm...maybe it should be configurable...doesn't seem fair, however, to make the user set a value. Grrrrrrrr... Always a trade-off...make it fast, or make it reliable.... The reliable route would be to store all "pointers" as offsets...or page/offset pair. But, that means that the application must always translate my pointer type into the actual pointer.... Slow...tedious, error-prone. Grrrrrrrrrrrrr!!!!!!!!! |
|
||||
|
Quote:
Essentially, I was thinking I could create a version of malloc that operated within a shared memory region and then use it to allocate items in the DB dynamically to be stored in a chained hash table. The "performance hit" on pointers is that I need to store the "pointer" to the bucket that I allocated (via my malloc routine) in shared memory somehow. Either that pointer is a native pointer into shared memory, or it is an offset into shared memory that every time application code goes to access a pointer it'll need to perform a conversion routine against it to acquire its position independant address. This would be required for either the array or non-native pointer methods. I guess I could tell the application (in the non-native pointer method) that the shared memory is a huge array of characters and access pointers through an "index" into the character array cast to the appropriate data type...but this seems just as ugly. Maybe creating an intermediate "malloc" library against a shared memory segment is silly...but I don't know of a better way to store various dynamically sized data into any memory segment without wasting space on static sized buckets. Last edited by DreamWarrior; 05-29-2007 at 02:18 PM.. |
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|