Sponsored Content
Top Forums Programming shared memory with linked list?? Post 302432830 by Corona688 on Sunday 27th of June 2010 11:39:15 AM
Old 06-27-2010
I don't agree.
Quote:
Originally Posted by Praveen_218
Somehow , I did not see this idea actually materializing into a feasible C code . I wonder, how to populate the nextNode pointer of a linked list, which could be accessed (for read/write) by multiple concurrent processes and without getting a sig – 11 Smilie .
There's a fundamental trick to avoiding signal 11. Don't use memory you don't have. It's really just that simple.

As for making memory consistent across different processes, there's two ways.
1) Always map the memory in the same place. You can accomplish it with mmap's MAP_FIXED flag, or shmat's shmaddr parameter. This means a little advance planning to avoid busy areas, but can work reasonably well.

2) Don't use pointers. Make the list a big table and use array indexes. Voila, your code no longer cares what area of memory your list gets put in.

Code:
#define LIST_END        (~(0UL))

typedef struct node
{
        unsigned long int next;
        unsigned long int prev;
        struct
        {
                char foo[64];
                int bar;
                double baz;
        } payload;
} node;

#define HEAD (shared_list[0])
...

node *shared_list=mmap(NULL, getpagesize()*16, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
HEAD.next=LIST_END;
HEAD.prev=LIST_END;

Of course it'll need to be mutexed somehow, or used with atomic operations etc.
Quote:
Regarding shmat(), if anyone think he/she may provide us a example of a scalable linked list implementation which is fully shared across different concurrent processes (not threads) , nothing better than this. Request please go ahead and provide us an example.
I've seen a working implementation of memory sharing across separate computers accomplished by setting up a special memory area that always exists in the same place, overloading the new operator, copying back and forth with RPC-like network calls, and mutexing properly. And these objects were real C++ objects with virtual members! A linked list is trivial in comparison.
Quote:
I'm , however, of the opinion that If more than one process is using the shared memory resident linked list, the pointers used in one process through shmat() calls, would not work from one process to the next. That's because different processes might have the shared memory at different places in their respective process address spaces returned by their respective calls to shmat().
You should really read their manual pages sometime. shmat() can be induced to put things where you ask it to, not where it pleases.
Quote:
Apart (even that could had been possible), I do not see any good reason that any practical code would create such a dataStructures within kernel object / area
What makes you think it uses kernel memory?
Quote:
where in you have a cap defined for its maximum size, is this defined by SHMMAX ? That prohibits such implementations from being scalable; a must for any serious implementation.
The compile time limit isn't fixed. Just echo a bigger value into /proc/sys/kernel/shmmax to raise it at runtime.

You can also map entire files and parts of files into memory with mmap, allowing a list even larger than physical memory to be used. This is often how large database implementations access their files. (as well as how nearly all program code is loaded.)

Last edited by Corona688; 06-27-2010 at 12:57 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies

2. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

3. Programming

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... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

4. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

5. UNIX for Dummies Questions & Answers

change the memory address of ld.linux-so in a dynamically linked process

hi, For some special reason , I'd like to control the memory address for the shared libraries in my dynamically linked process. And it is the "ld" which interpret the dynamically linked library, and in my system, the "ld-linux.so.2" is put at 0x00812000. Then I use "prelink -r" command to change... (0 Replies)
Discussion started by: zerocool_08
0 Replies

6. Linux

change the memory address of ld.linux-so in a dynamically linked process

hi, For some special reason , I'd like to control the memory address for the shared libraries in my dynamically linked process. And it is the "ld" which interpret the dynamically linked library, and in my system, the "ld-linux.so.2" is put at 0x00812000. Then I use "prelink -r" command to... (3 Replies)
Discussion started by: zerocool_08
3 Replies

7. UNIX for Dummies Questions & Answers

How to attach a linked list to the shared memory?

Hi all, I have been working on shared memory. I have created the shared memory and a linked list of 5 nodes. Now I want to attach the linked list to shared memory. When we attach a shared memory it returns a void pointer, but here I am in a fix , how to relate this void pointer to linked list.... (1 Reply)
Discussion started by: jimmyuk
1 Replies

8. Programming

How to attach a linked list to the shared memory?

Hi all, I have been working on shared memory. I have created the shared memory and a linked list of 5 nodes. Now I want to attach the linked list to shared memory. When we attach a shared memory it returns a void pointer, but here I am in a fix , how to relate this void pointer to linked list.... (4 Replies)
Discussion started by: jimmyuk
4 Replies

9. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

10. AIX

Create shared libs on AIX (with certain libs which are statically linked)

I want to create a shared lib with certain libs statically linked to it. I can generate a fully shared lib as follows: gcc -maix64 -DHAVE_CONFIG_H -I. -I./src -DHAVE_OPENSSL -I/usr/include/openssl -I/usr/include -I/usr/include/apr-1 -D_LARGEFILE64_SOURCE -I/usr/java8_64/include -shared -o... (0 Replies)
Discussion started by: amandeepgautam
0 Replies
All times are GMT -4. The time now is 07:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy