2.3.2. Shared Memory

To be done.

2.3.2.1. Example: System V Shared Memory

To be done.

int shmget (key_t key, size_t size, int shmflg);
void *shmat (int shmid, const void *shmaddr, int shmflg);
int shmdt (const void *shmaddr);
> ipcs -m
key        shmid      owner      perms      bytes      nattch     status
0x00000000 12345      root       600        123456     2          dest
0x00000000 123456     root       600        234567     2          dest
0x00000000 1234567    nobody     777        345678     2          dest

2.3.2.2. Example: POSIX Shared Memory

To be done.

2.3.2.3. Example: Windows Shared Memory

To be done.

HANDLE CreateFileMapping (
  HANDLE hFile,
  LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
  DWORD flProtect,
  DWORD dwMaximumSizeHigh,
  DWORD dwMaximumSizeLow,
  LPCTSTR lpName);

Flag PAGE_READONLY gives read only access to the committed region. Flag PAGE_READWRITE gives read write access to the committed region of pages. Flag PAGE_WRITECOPY gives copy on write access to the committed region.

Flag SEC_COMMIT allocates physical storage in memory or in the paging file on disk for all pages of a section. Flag SEC_IMAGE says file is executable, mapping and protection are taken from the image. Flag SEC_NOCACHE disables caching, used for shared structures in some architectures. Flag SEC_RESERVE reserves all pages of a section without allocating physical storage, reserved range of pages cannot be used by any other allocation operations until it is released.

If hFile is 0xFFFFFFFF, the calling process must also specify a mapping object size in dwMaximumSize. The function creates a file mapping object backed by the operating system paging file rather than by a named file.

LPVOID MapViewOfFile (
  HANDLE hFileMappingObject, DWORD dwDesiredAccess,
  DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow,
  DWORD dwNumberOfBytesToMap);

LPVOID MapViewOfFileEx (
  HANDLE hFileMappingObject, DWORD dwDesiredAccess,
  DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow,
  DWORD dwNumberOfBytesToMap, LPVOID lpBaseAddress);

Flags FILE_MAP_WRITE, FILE_MAP_READ, FILE_MAP_ALL_ACCESS, FILE_MAP_COPY. Address is suggested, if the address is not free the call fails.

BOOL UnmapViewOfFile (LPCVOID lpBaseAddress);

The address must be from a previous MapViewOfFile(Ex) call.

2.3.2.4. Example: Windows Clipboard

To be done.