Whether is shared memory only accessed by shmid?

Hello.
Recently I began to get into the unix's advanced IPC topic.
The most efficient method in IPC is the shared memory.
From the unix environment advanced programming book,I know the simplest way to use shm is that client process establishes one shared memory,assigns it an unique key and mount it on client's virtual process space ,then server process acquires the existed shared memory by previous unique key and mount it on server's virtual process space.
Server and client process can communicate through the shared memory by now.
But it seems that both processes can only access the shared memory by the address returned by shmat function call.
I wonder that whether the address returned by shmat is the same in both processes.In other words,if client process transfers the address to server process,can it access the address?
Actually,my project may encounter this situation.
In client process,it need establih a large shared memory and this will be acquired by server.But this large shared memory is managed by client and divided into many small segments.Client wishes that server accesses the specific segment rather than the whole shared memory.So I let the client tells server the specific segment address.
Here,I'm not sure that the specific segment address can be valid in server process address space.
So anyone can help me for this problem?
Very thanks!
Last edited on
I wonder that whether the address returned by shmat is the same in both processes.
Not necessarily, they may be paged into different local addresses, so don't put pointers in there.
@kbw
Thanks for reply.
so the shared memory locates at different virtual process address space.
When I update some segment of the shared memory, how can i efficiently notify the server process where the updated segment is?
Given that indicating the address of the updated segment is impractical,a normal way to indicate it is transferring the offset of the updated segment relative to the start address of whole shared memory.
I wonder that how people efficiently manage the shared memory between different processes as in the same process.
Wish for more crack advice.
Last edited on
When I update some segment of the shared memory, how can i efficiently notify the server process where the updated segment is?
I'm not sure what you mean. If memory is mapped into the address space of two processes, the update will appear immediately. If that process needs to be notified, that's another IPC message.

Given that indicating the address of the updated segment is impractical,a normal way to indicate it is transferring the offset of the updated segment relative to the start address of whole shared memory.
Yes, you'd organize the memory with some kind of data structure, and you always know the start of the memory block, so everything will be relative to than.

I wonder that how people efficiently manage the shared memory between different processes as in the same process.
As described above:
1. The data must be structured and accessed relative to the base address.
2. Don't use absolute pointers in the block, but pointers relative to the base address are (clearly) fine.
@kbw
Thanks for patient and helpful advice.
When I update some segment of the shared memory, how can i efficiently notify the server process where the updated segment is?

I mean a simple way like that client tells server the address and server read data from the received address,but now this can't be achieved and the offset relative to the start address must be computed in nature no matter what other ways.
As described above:
1. The data must be structured and accessed relative to the base address.
2. Don't use absolute pointers in the block, but pointers relative to the base address are (clearly) fine.

The first advice enlightens me.Yes,I have to organize the shared memory by some structures.
Thanks again.
When I update some segment of the shared memory, how can i efficiently notify the server process where the updated segment is?
Using the shared memory block safely and efficiently are add-ons. I mean, they're extra mechanisms you'll need to add.

For example, Advanced UNIX Programming has an example where the shared memory block is coupled with a semaphore to manage concurrency. If you want to add notification, you may well need to add something else, a socket for example.

I mean a simple way like that client tells server the address and server read data from the received address
I'm not clear on what address you mean. You know the start of the block, and you can find out the size with shmctl().

If you mean some location within the block, then ... I dunno, it depends on what you're doing. But perhaps that metadata can be passed with the event notice.
Using the shared memory block safely and efficiently are add-ons. I mean, they're extra mechanisms you'll need to add.
yes, I gradually feel it very interesting and amazing.
If you mean some location within the block, then ... I dunno, it depends on what you're doing. But perhaps that metadata can be passed with the event notice.
yes, I want to indicate the location within the block(now get it that directly specifying the address is impossible). Strongly agree that the metadata of the structured memory block is the key point.And other lightweight IPC(pipe,msg queue and unix socket) can help me for this.
Maybe I have to implement it for exploring more.
Thanks for support and guidance :)
Topic archived. No new replies allowed.