Rookie Nginx Source analysis data Structure (11) shared memory ngx_shm_t[go]

Source: Internet
Author: User

Rookie Nginx Source analysis data Structure (11) shared memory ngx_shm_t

  • Author:echo Chen (Chenbin)

  • Email:[email protected]

  • blog:blog.csdn.net/chen19870707

  • Date:nov 14th, 2014

    1. Shared memory

    Shared memory is the most basic process communication method provided under Linux, which creates a contiguous linear address space in memory through MMAP or shmget system calls, and frees the memory through Munmap or SHMDT system calls. The benefit of using shared memory is that when multiple processes use the same piece of memory, when any one process modifies the contents of the shared memory, other processes can gain memory changes 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 piece of shared memory:

       1:typedef struct {
       2:u_char *addr; Start address pointing to shared memory
       3:size_t size; Length of shared memory
       4:ngx_str_t name; The name of this shared memory
       5:ngx_log_t *log; Ngx_log_t Object for logging
       6:NGX_UINT_T exists; Indicates whether the shared memory is assigned a flag bit, or 1 indicates that it already exists
       7:} ngx_shm_t;
  • 4.Linux Shared Memory Interface

    Shared Memory Request 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 memory, while the Linux kernel is responsible for synchronizing the data in memory and disk files while the FD parameter executes the disk files that need to be synchronized, and offset represents the start of sharing from this offset of the file, Nginx does not use this feature. When a Map_anon or map_anonymous parameter is added to the flags parameter to indicate that the file mapping method is not applicable, then the FD and offset parameters are meaningless and do not need to be passed. There is no need to sync to disk in Nginx.

    The length parameter is the amount of linear address space that will be opened in memory

    The port parameter indicates how this shared memory is manipulated (read-only or readable writable)

    The start parameter describes the desired shared memory start address, which is usually set to null

    Shared Memory Release Munmap:

       1: #include <sys/mman.h>
       2:int munmap (void *start,size_t length);

    Start refers to the mapped memory start address, the parameter length is the memory size to be canceled

  • 5. Main operation of shared memory
  • The main operations for shared memory are the following:

  • Allocation of shared memory Ngx_shm_alloc
    Release of shared memory Ngx_shm_free
    5.1 Allocation of shared memory Ngx_shm_alloc
       1:ngx_int_t Ngx_shm_alloc (ngx_shm_t *shm)
       2: {
       3://Open up a shm->size-sized and readable writable shared memory with the first memory address in SHM->ADDR
       4:SHM->ADDR = (U_char *) mmap (NULL, Shm->size,
       5:prot_read| Prot_write,
       6:map_anon| Map_shared,-1, 0);
       
       8:if (shm->addr = = map_failed) {
       9:ngx_log_error (Ngx_log_alert, Shm->log, Ngx_errno,
      Ten: "Mmap (map_anon| Map_shared,%uz) failed ", shm->size);
      11:return Ngx_error;
      12:}
      
      14:return NGX_OK;
      15:}
      • 5.2 Shared Memory Release Ngx_shm_free
       1:void
       2:ngx_shm_free (ngx_shm_t *shm)
       3: {
       4://Use addr and size in ngx_shm_t to call Munmap to free 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
  • "In-depth understanding of ngxin"

Rookie Nginx Source analysis data Structure (11) shared memory ngx_shm_t[go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.