0

I have shared Memory like this

struct MEMORY {
char * type;
int number;
}

now in code I make it shared everything works probably but other process can't see what pointer points to how can I use pointer in shared memory?

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
Mohammad Karmi
  • 1,403
  • 3
  • 26
  • 42
  • 1
    take a look. http://stackoverflow.com/questions/2489908/is-it-possible-to-store-pointers-in-shared-memory-without-using-offsets this can help you. – doniyor Apr 10 '13 at 18:02
  • 1
    What did you try? What other process is doing with it? Did you read http://advancedlinuxprogramming.com/ and did you read some good C programming book? – Basile Starynkevitch Apr 10 '13 at 18:03
  • the problem that the pointer points in to something outside the shared memory everythings go well as if I make fork() the parent process and the child must have shared memory but the pointer doesnt work – Mohammad Karmi Apr 10 '13 at 18:06
  • Each process has its own address space, and you need very special syscalls to have parts of them be shared, and you also need a way to synchronize.... What is your overall goal (why do you want to share memory)? What is your application? Show more code relevant to your question.... – Basile Starynkevitch Apr 10 '13 at 18:27

1 Answers1

0

You need to make sure that the shared memory is attached at the same address in the address space of all the processes. Otherwise, as you can imagine, the pointer values end up meaning different things in different processes.

What are you using for shared memory? mmap or shm? It is the first argument in the mmap call.

If you cannot assure same address space in all processes, the other way to go is to us offsets only. Each process simply offsets from the base address where the shared memory is attached.

EDIT: Ah ... maybe what you are saying is that "char* type" is some arbitrary pointer. Remember that the other processes can only see what is in the shared memory. All other memory locations (pointer values) are inaccessible. So, for this pointer to work, it needs to be to something that is in shared memory, not just any arbitrary pointer. That, and you need to assure that the shared memory is attached at the same address in all processes.

Ziffusion
  • 8,779
  • 4
  • 29
  • 57