In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the method of Linux process sharing memory". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of Linux process sharing memory".
Shared memory IPC principle
The inter-process communication mechanism of shared memory is mainly used to achieve a large amount of data transfer between processes. The following figure shows a schematic diagram of using shared memory to achieve a large amount of data transfer between processes:
Shared memory is a section of memory space opened up separately in memory, which has its own unique data structure, including access rights, size, recent access time, and so on. The data structure is defined as follows:
From / usr/include/linux/shm.hstruct shmid_ds {struct ipc_perm shm_perm; / * operation perms operation rights * / int shm_segsz; / * size of segment (bytes) segment length size * / _ _ kernel_time_t shm_atime; / * last attach time recent attach time * / _ _ kernel_time_t shm_dtime; / * last detach time recent detach time * / _ _ kernel_time_t shm_ctime / * last change time recent change time * / _ _ kernel_ipc_pid_t shm_cpid; / * pid of creator creator pid * / _ _ kernel_ipc_pid_t shm_lpid; / * pid of last operator recently operated pid * / unsigned short shm_nattch; / * no. Of current attaches * / unsigned short shm_unused; / * compatibility * / void * shm_unused2; / * ditto-used by DIPC * / void * shm_unused3; / * unused * /}
Before using this shared memory space, two processes need to establish a relationship between the process address space and the shared memory space, that is, to mount the shared memory space into the process.
The system imposes the following restrictions on shared memory:
# define SHMMAX 0x2000000 / * max shared seg size (bytes) maximum shared segment size * / # define SHMMIN 1 / * min shared seg size (bytes) minimum shared segment size * / # define SHMMNI 4096 / * max num of segs system wide * / # define SHMALL (SHMMAX/getpagesize () * (SHMMNI/16)) # define SHMSEG SHMMNI/ * max shared segs per process * / Linux shared memory management
1. Create shared memory
# include # include
/ * * the first parameter is the key value, which is usually generated by the ftok () function * the second parameter is the size of the shared memory segment to be created (in bytes) * the third parameter is used to identify the creation identity of the shared memory segment * /
Int shmget (key_t key, size_t size, int shmflg)
two。 Shared memory control
# include # include
/ * * the first parameter is the shared memory identifier to be operated * the second parameter is the operation to be performed * the third parameter is the temporary shared memory variable information of the shmid_ds structure * /
Int shmctl (int shmid, int cmd, struct shmid_ds * buf)
3. Map shared memory objects
The system calls the shmat () function to map a shared memory segment to the data segment of the calling process and returns the first address of the memory space. The function declaration is as follows:
# include # include
/ * * the first parameter is the shared memory identifier to be operated * the second parameter is used to specify the mapping address of the shared memory, and if it is not 0, it is assigned by the system * the third parameter is used to specify the access rights and mapping conditions of the shared memory segment * /
Void * shmat (int shmid, const void * shmaddr, int shmflg)
4. Detach shared memory objects
After using the shared memory space, you need to separate it from the current process using the shmdt () function call. The function is declared as follows:
# include # include
/ * * Parameter is the assigned first address of shared memory * /
Int shmdt (const void * shmaddr); conventions followed between parent and child processes in shared memory
1. After you create a child process using the fork () function, the process inherits the shared memory mounted by the parent process.
two。 If you call exec () to execute a new program, all mounted shared memory will be automatically unloaded.
3. If the exit () function is called in a process, all mounted shared memory will be disconnected from the current process.
Program example
Apply for a section of shared memory, the parent process stores an integer at the first address, and the child process reads it.
# include#include# include#include# include#include#define SHM_SIZE 1024int main () {int shm_id, pid;int * ptr = NULL
/ * apply for shared memory * /
Shm_id = shmget ((key_t) 1004, SHM_SIZE, IPC_CREAT | 0600)
/ * Mapping shared memory to process address space * /
Ptr = (int*) shmat (shm_id, 0,0); printf ("Attach addr is% p\ n", ptr); * ptr = 1004 political printf ("The Value of Parent is:% d\ n", * ptr); if ((pid=fork ()) = =-1) {perror ("fork Err"); exit (0);} else if (! pid) {printf ("The Value of Child is:% d\ n", * ptr); exit (0);} else {sleep (1)
/ * Unmap * /
Shmdt (ptr)
/ * Delete shared memory * /
Shmctl (shm_id, IPC_RMID, 0);} return 0;}
Output result:
Thank you for your reading, the above is the content of "what is the method of Linux process sharing memory". After the study of this article, I believe you have a deeper understanding of what the method of Linux process sharing memory is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.