What is the shared memory in Linux inter-process communication? Shared Memory Function instances, linux functions
I. What is shared memory?
Shared memory is the fastest and most useful communication method between two processes.
Since the communication methods we introduced earlier must be passed between the kernel and the user space, the shared memory is that the process directly maps the memory to the address space of the process, therefore, process A and process B can directly view the operations performed by the other Party on this region.
Because the process can directly read and write memory without any data copying, This is different from the communication between the processes described above, so it is very efficient, in other words, processes no longer transmit data to each other by executing system calls that enter the kernel.
Ii. Shared Memory Structure
Similar to the previous process communication, let's take a look at the following shared memory structure.
struct shmid_ds {struct ipc_perm shm_perm; /* Ownership and permissions */size_t shm_segsz; /* Size of segment (bytes) */time_t shm_atime; /* Last attach time */time_t shm_dtime; /* Last detach time */time_t shm_ctime; /* Last change time */pid_t shm_cpid; /* PID of creator */pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */shmatt_t shm_nattch; /* No. of current attaches */ ...};
3. Shared Memory Function (1) shmget
The shmget function is used to create shared memory.
Function prototype
int shmget(key_t key, size_t size, int shmflg);
Parameters
Key_t key: the first parameter is the same as the key in the message queue, indicating the name of the shared memory. Size_t size: the second parameter refers to the size of the int shmflg: permission, with nine flag characters. It can be IPC_CREAT | 0644. The returned value is
A non-negative integer (the identifier of the shared memory) is returned.
(2) shmat
Hanging the shared memory in your own address space
void *shmat(int shmid, const void *shmaddr, int shmflg);
Return Value
0 is returned for success, and-1 is returned for failure.
Shmid: Shared Memory flag. Shmaddr: Specifies the link address. Generally, NULL is used to automatically select an address. Shmflg: Mark. Two possible values are SHM_RND and SHM_RDONLY. Generally, 0 is used.
(3) shmdt
We need to note that the deletion of shared memory can be deleted only when there is no human ing. It is different from message queue, and cannot be deleted when someone maps.
Delete unused shared memory from your address space.
int shmdt(const void *shmaddr);
Parameters:
Pointer returned by shmdt. Return Value
0 is returned for success and-1 is returned for failure.
(4) shmctl
Shmctl is used to control the shared memory.
nt shmctl(int shmid, int cmd, struct shmid_ds *buf);
Parameters
Shmid: The identifier of the shared memory returned by shmget cmd: the action to be taken buf: the return value of the data structure that points to a mode that saves the shared memory and the access permission
0 is returned for success and-1 is returned for failure.
Cmd has the following three commands to choose from, which is similar to the ctl operation of message queue.
IPC_STAT: Set the data in the shmid_ds structure to the current associated value of shared memory IPC_SET: with sufficient permissions, set the current value of the shared memory to the value IPC_RMID in the shmid_ds data structure to delete the shared memory.
4. How to read shared memory
Read the process space directly because the memory is mapped to the process address space.