80. Shared Memory for process communication

Source: Internet
Author: User

  • Server creates shared memory
    1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 #defineSIZE 40966 //The pointer identifies the first address7LPVOID lpdata =NULL;8 9 Ten voidMain () One { A     if(lpdata!=NULL) -     { -Puts"Shared memory exists"); the     } -     //Create shared Memory -HANDLE Hmap =Createfilemappinga (Invalid_handle_value, NULL, -Page_readwrite | Sec_commit,0, SIZE,"Cgwmem"); +  -     if(hmap==NULL) +     { APuts"creation failed"); at     } -     Else -     { -         //mapping files to pointers -lpdata = MapViewOfFile (Hmap, File_map_read | File_map_write,0,0,0); -  in         inta[Ten] = {1,2,3,4,5,6,7,8,9,0 }; -memcpy (Lpdata, A, +); to     } +  -System"Pause"); the  *  $UnmapViewOfFile (lpdata);//liftedPanax Notoginseng CloseHandle (hmap); -System"Pause"); the  +  A}

  • The server uses this piece of shared memory
    1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 voidMain ()6 {7HANDLE hmapfile = Openfilemappinga (File_map_read, FALSE,"Cgwgmem");8     if(Hmapfile = =NULL)9     {Tenprintf"failed to open up"); One     } A  -     //Create pointers, point to this piece of memory -LPVOID lpbase = MapViewOfFile (Hmapfile, File_map_read,0,0,0); the     if(Lpbase = =NULL) -     { -printf"Open Failed"); -  +     } -  +     int*p =lpbase; A      for(inti =0; I <Ten; i++) at     { -printf"%d\n", P[i]); -     } -  -  -UnmapViewOfFile (lpbase);//lifted in CloseHandle (hmapfile); -  to  +System"Pause"); -}

80. Shared Memory for process communication

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.