Initialize shared memory on the server

Source: Internet
Author: User
The shared memory of the server and the shared memory initialization will be performed when the Gs is opened when the client opens the server to receive notifications. The process is complex and has been read many times, today, I finally learned about bool datalayer: Init () {int pid = getcurrentprocessid (); Auto Path = plug: getcurrentpath (); path + = l "pid.txt"; Auto file = _ wfopen (path. c_str (), L "WB"); char Buf [10] = {0}; ITOA (PID, Buf, 10); fwrite (BUF, 6, 1, file ); // write the current gsprogress number to pid.txt. The net process can get the name of the shared memory fclose (File); m_spsharememinter = newsp (shareminter); m_spsharememememint Er-> Init ("network", pid); // create a network sub-process if (! Createnetprocess () {plug: plugmessagebox (L "failed to start the net process! "); Return false;} return true;} // void shareminter: Init (const char * Name, int PID) {STD: String name2 = Name; m_a2b.init (name2, pid); name2 + = "-sending back"; m_b2a.init (name2, PID, 2);} // initialize void shareminteroneway for single-side shared memory :: init (STD: string name, int PID, int more) {// initialize three types of shared memory m_processmempool.init (name, PID, more); m_pid = PID; // shared memory for mutex, mainly for mutex access m_sharememque m_shm = new shared_memory_object (open_or_create, getmemname (name + "MUX", m_pid ). c_str (), read_write); m_shm-> truncate (sizeof (MEM image); mapped_region * m_region = new mapped_region (* m_shm, read_write ); void * ADDR = m_region-> get_address (); m_mem image = new (ADDR) MEm image; // a queue shared memory is created here, this is used to fetch a piece of memory from the memory pool and put it into this queue. It has been stuck for a long time in this place m_segment = new managed_shared_memory (open_or_create, getmemname (name + "mysharedmemory9 ", m_pid ). c_str (), 1024*1024*10 * more); m_sharememque = m_segment-> find_or_construct <mem_queue> (getmemname (name + "mysharedmemory92", m_pid ). c_str () (mem_allocator (m_segment-> get_segment_manager ();} // this process has been around for a long time and is also the void processmempool: Init (INT memsize, int buffer_num, STD: string name, int PID) {m_memsize = memsize; m_buffer_num = buffer_num; m_name = Name; m_pid = PID; // whether the shared memory storage bool initializes auto SHM = new shared_memory_object (open_or_create, getmemname (m_name + "mux2", pid ). c_str (), read_write); SHM-> truncate (sizeof (bool); mapped_region * m_region = new mapped_region (* SHM, read_write ); void * ADDR = m_region-> get_address (); M _ image = (processmempoolmem image *) (ADDR); // 1024 x buffer_num shared memory, it stores queue/* in boost. Generally, containers in STL cannot be placed in the shared memory, because the memory is not allocated on the shared memory, it is mainly related to its distributor. At this time, we need to use the corresponding container defined by boost. This m_queue is the role of the shared memory index, obtain the address of the shared memory through the Index */m_segment = new managed_shared_memory (open_or_create, getmemname (m_name + "processmempool9", pid ). c_str (), 1024 * buffer_num/* 1024*1024*10*2 */); m_queue = m_segment-> find_or_construct <mem_queue> (getmemname (m_name + "m_queue ", PID ). c_str () (mem_allocator (m_segment-> get_segment_manager (); // swap // shared memory auto shm2 = new shared_memory_object (open_or_create, getmemname (name + "memall2 ", PID ). c_str (), read_write); shm2-> truncate (m_memsize * (m_buffer_num + 10); mapped_region * m_region2 = new mapped_region (* shm2, read_write ); m_start_addr = m_region2-> get_address (); If (M _ image-> is_inited) // checks whether the shared memory has been initialized {return;} M _ image-> is_inited = true; // initialize the shared queue, that is, insert into dataex for (INT I = 0; I <m_buffer_num; I ++) {writable dataex SD; SD. index = I; SD. size = 0; SD. total_size = m_memsize; m_queue-> push_back (SD );}} because the shared memory here is open_or_create, when the net process initializes the shared memory, it will open when it sees the shared memory with the same name. If it does not, it will create

 

Initialize shared memory on the server

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.