In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
C++ shared memory deletion trap is what, for this problem, this article details the corresponding analysis and solution, hoping to help more want to solve this problem of small partners to find a simpler and easier way.
When the process ends using the shared memory area, it disconnects the shared memory area via the function shmdt. This function is declared in sys/shm.h and its prototype is as follows:
int shmdt(const void *shmaddr);
Parameter shmaddr is the return value of the shmat function.
When a process leaves a shared memory area, shm_nattch in the data structure shmid_ds is decremented by 1. However, shared memory is still present, and only when shm_attch is 0, i.e. no process uses the shared memory area, the shared memory area is deleted in the kernel. In general, when a process terminates, the shared memory area to which it is attached is automatically detached.
We adopt:
int shmctl( int shmid , int cmd , struct shmid_ds *buf );
To delete shared memory that already exists:
The first parameter, shmid, is the identifier returned by shmget.
The second parameter, cmd, is the action to be performed. It can have three values:
Command Description:
IPC_STAT sets the value associated with shared memory for data reflection in the shmid_ds structure.
IPC_SET Sets the value associated with shared memory to the value provided in the shmid_ds data structure if the process has the appropriate permissions.
IPC_RMID Delete shared memory segment.
The third parameter, buf, is a pointer to a structure containing shared memory modes and permissions, and can be deleted by default to 0.
If the shared memory has been disconnected from all processes accessing it, the IPC_RMID subcommand will immediately delete the identifier of the shared memory and delete the shared memory area and all related data structures;
If there are other processes still connected to the shared memory, the shared memory is not immediately deleted from the system after invoking the IPC_RMID subcommand, but is set to the IPC_PRIVATE state and marked as "deleted"(the dest field can be seen with the ipcs command); the shared memory does not eventually disappear from the system until all existing connections are broken.
Note that once a shared memory is deleted via shmctl, that shared memory cannot accept any new connections, even if it is still present in the system! Therefore, it can be ascertained that no new connections can be made after the deletion of shared memory, so it is safe to perform the deletion operation; otherwise, if new connections still occur after the deletion operation, these connections will probably fail!
Difference between Shmdt and Shmctl:
hmdt is to detach shared memory from the process space, so that shmid in the process is invalid and cannot be used. But keep the space.
shmctl(sid,IPC_RMID,0) deletes shared memory, which is completely unavailable and frees up space.
About C++ shared memory deletion trap is what kind of problem answer to share here, hope the above content can have some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.