Shared memory means one process will create a mem开发者_开发技巧ory portion oher process can acess.
My question is: how will the other process know the created shared memory shmid
(i.e shmid=shmget(key-t ,size , permission);
)?
To simplify: The process that creates the shared memory defines the name and permissions (process of the logged in user).
Basically you create a file:
file_descriptor = shm_open("/some.shared.memory",
(O_CREAT | O_RDWR),
(S_IREAD | S_IWRITE))
Or if it exists you can open it:
file_descriptor = shm_open("/some.shared.memory",
(O_CREAT | O_EXCL | O_RDWR),
(S_IREAD | S_IWRITE))
Thus, the name "/some.shared.memory" is your way to find the shared segment. (Note the flags)
- Have a look here http://www.kernel.org/doc/man-pages/online/pages/man7/shm_overview.7.html
- Do a Google search on "shm_open example" and you'll find lots of examples.
- The process that creates the file will determine the permissions (owner+group) See the chown command and fchmod functions which you can call to set the permissions. Also have a look at umask.
They need to use the same way to derive the key_t; usually you use the ftok
function where both sides use the same file name and id value, so they end up with the same key.
精彩评论