Cainiao nginx Source Code Analysis Data Structure Article (11) shared memory ngx_shm_t, nginxngx_shm_t
Cainiao nginx Source Code Analysis Data Structure Article (11) shared memory ngx_shm_t
Author: Echo Chen (Chen Bin)
Email: chenb19870707@gmail.com
Blog: Blog.csdn.net/chen19870707
Date: Nov 14th, 2014
1. Shared Memory
Shared memory is the most basic process communication method provided in Linux. It creates a continuous linear address space in the memory through mmap or shmget system calls, the memory is released by calling the munmap or shmdt system. The advantage of using the shared memory is that when multiple processes use the same memory, after any process modifies the content in the shared memory, other processes can change the memory by accessing the memory.
2. Source Code Location
Header file: http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_shmtx.h
Source File: http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_shmtx.c
3. data structure definition
Ngx_shm_t: used to describe a shared memory:
1: typedef struct {
2: u_char * addr; // point to the starting address of the shared memory.
3: size_t size; // length of the shared memory
4: ngx_str_t name; // name of the shared memory
5: ngx_log_t * log; // The ngx_log_t object for logging
6: ngx_uint_t exists; // indicates whether the shared memory has been allocated. If it is 1, it indicates that it already exists.
7: } ngx_shm_t;
4. Linux shared memory interfaceShared Memory application mmap:
1: #include<sys/mman.h>
2: void*mmap(void* start,size_t length,int prot,int flags,
3: int fd,off_t offset);
Mmap can map disk files to the memory. When operating the memory directly, the Linux kernel will be responsible for synchronizing data in the memory and disk files, and the fd parameter will execute the disk files to be synchronized, offset indicates that the file is shared from the offset. Nginx does not use this feature. When MAP_ANON or MAP_ANONYMOUS is added to the flags parameter, the file ing mode is not applicable. In this case, the fd and offset parameters are meaningless and do not need to be passed.Nginx does not need to be synchronized to the disk.
The length parameter is the size of the linear address space to be opened in the memory.
Port parameter indicates the way to operate this shared memory (read-only or readable and writable)
The start parameter specifies the starting address of the expected shared memory, which is usually set to NULL.
Shared Memory releases munmap:
1: #include<sys/mman.h>
2: int munmap(void *start,size_t length);
Start refers to the start address of the ing memory. The length parameter indicates the size of the memory to be canceled.
5. Main Operations of shared memory
The main operations of shared memory are as follows:
Shared Memory Allocation |
Ngx_shm_alloc |
Release of shared memory |
Ngx_shm_free |
5.1 shared memory allocation ngx_shm_alloc
1: ngx_int_t ngx_shm_alloc(ngx_shm_t *shm)
2: {
3: // open up a shm-> size and read-write shared memory, the first memory address is placed in shm-> addr
4: shm->addr = (u_char *) mmap(NULL, shm->size,
5: PROT_READ|PROT_WRITE,
6: MAP_ANON|MAP_SHARED, -1, 0);
7:
8: if (shm->addr == MAP_FAILED) {
9: ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
10: "mmap(MAP_ANON|MAP_SHARED, %uz) failed", shm->size);
11: return NGX_ERROR;
12: }
13:
14: return NGX_OK;
15: }
- 5.2 release ngx_shm_free from shared memory
1: void
2: ngx_shm_free(ngx_shm_t *shm)
3: {
4: // use addr and size in ngx_shm_t to call munmap to release the shared memory
5: if (munmap((void *) shm->addr, shm->size) == -1) {
6: ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
7: "munmap(%p, %uz) failed", shm->addr, shm->size);
8: }
9: }
6. Reference
An in-depth understanding of Ngxin
-
Echo Chen: Blog.csdn.net/chen19870707
-