Shared memory allows multiple processes to access the same memory space. it is the fastest available IPC format. It is designed to reduce the running efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes. Other processes can "connect" the same shared memory segment to their own address space. All processes can access the addresses in the shared memory. If a process writes data to the shared memory, the changes will be immediately seen by other processes that access the same shared memory. The use of shared memory greatly reduces
Shared memory allows multiple processes to access the same memory space. it is the fastest available IPC format. It is designed to reduce the running efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes. Other processes can "connect" the same shared memory segment to their own address space. All processes can access the addresses in the shared memory. If a process writes data to the shared memory, the changes will be immediately seen by other processes that access the same shared memory. The use of shared memory greatly reduces the memory consumption in the process of large-scale data processing. However, there are many traps in the use of shared memory. if you do not pay attention to it, the program may crash.
Exceeds the size limit of shared memory?
On a linux server, the total size of shared memory is limited. The size is defined by the SHMMAX parameter (in bytes ), you can determine the value of SHMMAX by executing the following command:
# cat /proc/sys/kernel/shmmax
If the total size of the shared memory created on the machine exceeds this limit, the following information may occur when the standard error perror is used in the program:
unable to attach to shared memory
Solution:
1. set SHMMAX
The default value of SHMMAX is 32 MB. Set the SHMMAX parameter to 2 GB using one of the following methods:
By directly changing the/proc file system, you can change the SHMMAX default settings without restarting the machine. I am using the following command to put it in the/> etc/rc. local startup file:
# echo "2147483648" > /proc/sys/kernel/shmmax
You can also use the sysctl command to change the SHMMAX value:
# sysctl -w kernel.shmmax=2147483648
Finally, by inserting this kernel parameter into the/etc/sysctl. conf startup file, you can make this change permanently valid:
# echo "kernel.shmmax=2147483648" >> /etc/sysctl.conf
2. set SHMMNI
Now let's look at the SHMMNI parameter. This kernel parameter is used to set the maximum number of shared memory segments within the system range. The default value of this parameter is 4096. This value is sufficient and does not need to be changed.
You can determine the value of SHMMNI by executing the following command:
# cat /proc/sys/kernel/shmmni
4096
3. set SHMALL
Finally, let's look at the SHMALL shared memory kernel parameters. This parameter controls the total amount of shared memory that the system can use at one time (in pages ). In short, the value of this parameter should always be at least:
ceil(SHMMAX/PAGE_SIZE)
The default SHMALL size is 2097152. you can use the following command to query the SHMALL:
# cat /proc/sys/kernel/shmall
2097152
The default SHMALL settings should be enough for us.
Note: On the i386 platform, the page size of Red Hat Linux is 4096 bytes. However, you can use bigpages to configure a larger memory page size.
What are the problems with multiple shmat operations?
When a shared memory segment is created for the first time, it cannot be accessed by any process. To make the shared memory area accessible, you must use the shmat function to attach it to your own process space, so that the process is connected to the shared memory. This function is declared in linux/shm. h:
#include
#include
void *shmat(int shmid, const void *shmaddr, int shmflg);
The shmid parameter is the return value of shmget () and an identifier;
The shmflg parameter indicates the access permission. if it is set to 0, no access restriction is set. Several permissions are defined in:
#define SHM_RDONLY 010000 /* attach read-only else read-write */
#define SHM_RND 020000 /* round attach address to SHMLBA */
#define SHM_REMAP 040000 /* take-over region on attach */
If SHM_RDONLY is specified, the shared memory only has read permission.
The shmaddr parameter is an additional point of shared memory. different values have different meanings:
? If it is null, the kernel selects an idle memory zone. if it is not empty, the return address depends on whether the caller specifies the SHM_RND value for the shmflg parameter. if it is not specified, the shared memory area is appended to the address specified by shmaddr; otherwise, the additional address is the address (SHMLBA, a common address) after the shmaddr round-down address ).
Ø generally, the shmaddr parameter is set to NULL.
After shmat () is successfully called, a pointer pointing to the shared memory area is returned. with this pointer, you can access the shared memory area. if the pointer fails,-1 is returned.
Shows the ing relationship: