Can any one look into this and suggest me with an API.
We have APIs for a process which can create and/or attach a shared memory to its own process. But I don’t find an API to attach a shared memory to one process by other process(for e.g., process A should call one API(like shmat()) to attach the shared memory to process B).
Shared memory doesn’t belong to any particular process (unless you create it with a private
IPC_PRIVATEkey). It belongs to the system.So, when you use
shmgetwith a non-private key (and theIPC_CREATflag), you will either create a shared memory block or attach to an existing one.You need a way for both processes to use the same IPC key and this is often done by using
ftokwhich uses a file specification and an identifier to give you an IPC key for use in theshmgetcall (and other IPC type calls, such asmsggetorsemget).For example, in the programs
pax1andpax2, you may have a code segment like:By having both processes use the same file specification and ID, they’ll get the same shared memory block.
You can use different IDs to give you distinct shared memory blocks all based on the same file (you may, for example, want one for a configuration shared memory block and another for storing shared state).
And, given that it’s your configuration file the IPC key is based on, the chances of other programs using it is minuscule (I think it may be zero but I’m not 100% sure).
You can’t forcefully inject shared memory into a process from outside that process (well, you may be able to but it would be both dangerous and require all sorts of root-level permissions). That would break the protected process model and turn you system into something about as secure as MS-DOS 🙂