shared memory system v
#include <sys/types.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);
Created: association process with the shared memory
-
key_t key: non-zero digit hexadecimal.
There are two general ways to set it up.
The first: the function call fotk
Chapter II: Direct use IPC_PRIVATE
-
size: shared memory size
-
shmflg:
- IPC_CREAT
- IPC_EXCL
- User, group of users, the other users permission to this memory sheet, nine bit represented, such as 664
-
Return Value: returns the success of this identification number of the shared memory; failure returns -1, errno is set.
#include <sys/types.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
- shmid: created by shmget function, which is the function's return value shmget
- shmaddr:
- NULL: to apply the kernel memory space
- Non-NULL: own use malloc to open up a space for the shared memory shmid and associate this address. However, if not a multiple of 4K, the kernel will adjust up or down.
- shmflg:
- SHM_RND: reading and writing
- SHM_RDONLY: Read-only
- return value:
- Success: Returns memory address
- Failure: return (void *) -1
#include <sys/types.h>
#include <sys/shm.h>
int shmdt(const void *shmaddr);
Association to cancel the process and the shared memory
- shmaddr: shmat return value
- Return Value: 0 success; failure -1, and set errno.
#include <sys/types.h>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
Shared memory operations, more cmd different shared memory for different operations.
- shmid: created by shmget function, which is the function's return value shmget
- cmd:
- IPC_STAT: get the state shared memory
- IPC_RMID: Remove shared memory tag (memory when the shared reference count becomes 0, deleted)
- IPC_SET: set the properties of shared memory (permission to modify, amend shmid, etc.)
- and many more
- buf: shmid_ds structure
- Return value: when cmd is IPC_RMID time: 0 success; failure -1, and errno
[] Command ipcs can view the status of shared memory
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00007fff 65536 ys 664 256 0
0x00007ffe 98305 ys 664 256 0
0x0000555e 131074 ys 664 256 0
0x00000011 229379 ys 664 256 3
- key: the first argument specified shmget
- shmid: shmget return value of the function
- owner: belongs to which user-created
- perms: The shared memory access
- bytes: size
- The number of shared memory using this process: nattch
- status: status of shared memory