|
flag can set the access permission of the shared storage segment, can also be set
Two flags: IPC_CREAT and IPC_EXCL.
The successful call of shmget returns a non-negative integer, which is connected to the key
Shared storage segment id. If it is a new shared storage segment, shmget
Set all units in the initial shared storage segment to 0, and create and parameter key at the same time
The connected data structure shmid_ds.
Failure to call shmget will return -1 and set errno to indicate the cause of the error.
The shmctl function performs various operations on the shared storage segment, and its function prototype is as follows
under:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
int shmctl (int shmid, int cmd, struct
shmid_ds * buf);
Explanation:
The cmd parameter specifies one of the following five commands to make it refer to the shmid
Execute on the specified segment:
IPC_STAT takes the shmid_ds structure for this segment and stores it in the structure pointed to by buf.
IPC_SET sets the following three fields in the structure related to this segment by the value in the structure pointed to by buf: shm_perm.uid, shm_perm.gid, and shm_perm.mode
IPC_RMID deletes the shared storage segment from the system. Because each shared storage segment has a connection count (shm_nattch in the shmid_ds structure), the storage segment will not actually be deleted unless the last process using the segment terminates or is disconnected from the segment. Regardless of whether the segment is still in use, the segment identifier is deleted immediately, so you can no longer use shmat to connect to the segment.
SHM_LOCK locks the shared memory segment. This command can only be executed by the super user.
SHM_UNLOCK unlocks the shared storage segment. This command can only be executed by the super user.
Once a shared storage segment is created, the process can call shmat to
Connected to its address space, its function prototype is as follows:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
void * shmat (int shmid, void * addr, int flag);
Description:
The address of the calling process where the shared memory segment is connected to addr
The parameters are related to whether the SHM_RND bit is specified in the flag.
(1) If addr is 0, this segment is connected to the first available address selected by the kernel.
(2) If addr is not 0, and SHM_RND is not specified, this segment is connected to the address specified by addr.
(3) If addr is not 0, and SHM_RND is specified, this segment is connected to the address indicated by (addr- (addr mod SHMLBA)).
If the SHM_RDONLY bit is specified in the flag, read-only
Connect this segment. Otherwise, connect this segment by reading and writing. shmat return value
Is the actual address connected to the segment, or -1 if an error occurs.
When the process no longer needs a shared storage segment, you can call the shmdt function
Separate it from the address space of the process. The function prototype is as follows:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
int shmdt (void * addr);
Description:
The addr parameter is the return value from the previous call to shmat. Note that this
This function is just a shared memory segment that is no longer empty with the address of the calling process
Connected, it does not actually delete the shared storage segment itself, delete the shared storage
The storage section is the function of the shmctl function IPC_RMID command. |
|