shared memory with linked list??


 
Thread Tools Search this Thread
Top Forums Programming shared memory with linked list??
# 8  
Old 06-25-2010
Quote:
Originally Posted by Praveen_218
I do not think that this scenario is possible on protected memory managed systems ...

...

However, the same is possible (and very much possible) possible on a flat memory managed systems like VxWorks. As a food for though just think how?
That makes no sense - the underlying memory model is hidden from the application programmer, so it doesn't matter. All the application sees is a flat address space, whether you're accessing it through virtual memory or not.

So think how you would do it for VxWorks... the same would work on any Linux variant (probably). Either way, creating data structures in shared memory is possible.


Quote:
You can not perform memory operations like you do on normal program variables (be it a heap, stack, .bss, .data/ro etc).
Like &<my_variable>
Sorry, but that's just plain incorrect. Where on earth did you get that from? When you get shared memory, you get a pointer to it by calling shmat(2). Shared memory is just normal memory. Otherwise, what would be the point...?

You can do anything to shared memory you can do to normal memory in your r/w data section, including populating it with a linked list.
# 9  
Old 06-25-2010
Just to be sure - John is correct.

In the case of read/write shared memory you can do anything to shared memory that you can do in process-private read/write memory. Period.

How else do you think an application like Oracle could use a single "SGA" (system global area) to serve hundreds of user client processes simultaneously?
# 10  
Old 06-27-2010
Smilie Really good discussions so far.

Quote:
Originally Posted by JohnGraham
This is entirely possible.

  • Create shared memory region
  • Place the head of the linked list there at a known location (e.g. at the start of the shared region), being careful to make sure that both the linked list itself and the data referenced by it are in the shared memory, not on your heap/stack.

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 .


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'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().

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 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.

Thanks anyway for all the opinions expressed, whatever it is Smilie .
# 11  
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..
# 12  
Old 07-05-2010
shared memory with linked list?

In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.
Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues, hash tables, symbolic expressions, and skip lists.
The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.
On the other hand, linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations - such as obtaining the last node of the list, or finding a node that contains a given datum, or locating the place where a new node should be inserted - may require scanning most of the list elements.
Linked lists can be implemented in most languages. Languages such as Lisp and Scheme have the data structure built in, along with operations to access the linked list. Procedural languages, such as C, or object-oriented languages, such as C++ and Java, typically rely on mutable references to create linked lists.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question