Shared Memory
shared memory mapped into the process's address space with mmap,
and unmapped with munmap.
header #include <sys/mman.h>
macro should defined _POSIX_SHARED_MEMORY
Shared memory objects are created with a size of zero.
They are sized using ftruncate and mapped in using mmap.
The descriptor returned by this call is only useful for those functions
int shm_unlink(char *shm_name);
int shm_open(char *shm_name, int oflags, mode_t mode);
int close(int fd);
Memory Mapping
The mmap function creates a virtual memory mapping for a region
of the file fd. This mapping allows you to refer to the contents
of the file as if it were memory.
mmap—Map a shared memory object (or possibly another file) into process's address space.
void *mmaps(void *desired_addr, size_t length, int memory_protections, int mapping_flags, int fd, off_t offset_within_file);
/*
mapping_flags values
MAP_SHARED Create a mapping shared by all shared mappers
MAP_PRIVATE Create a private (copy-on-write) mapping
MAP_FIXED Map at desired_addr, or else fail
*/
/*
memory_protections
PROT_READ Read permission for the memory area
PROT_WRITE Write permission for the memory area
PROT_EXEC Execute permission for the memory area
PROT_NONE No permissions for the memory area
*/
//mmap returns the address at which the mapping was performed, or MAP_FAILED if the call failed.
msync — Make a mapping consistent with the underlying object..
int msync(const void *addr, size_t length, int flags);
/*
flags
MS_SYNC Synch the data out now and don't return until it's done.
MS_ASYNC Start the synch operation and immediately return.
MS_INVALIDATE Invalidate cached copies of the data that are inconsistent with the synched data. useful for multiprocessed or multiple mapping.
*/
munmap — Undo a mapping established by mmap.
int munmap(void *addr, size_t length);
Memory locking
.
Memory locks are not inherited by children created by fork, and are also abandoned when exit or one of the exec
functions are called.
locks a process address space into physical memory, rendering the memory immune from being paged or swapped to disk.
#ifdef _POSIX_MEMLOCK_RANGE
mlock—Lock a range of memory. don't use more then 3 times for stacking.
int mlock(const void *addr, sizet len);
mlockall—Lock your entire memory space down. don't use more then 10 times for stacking.
int mlockall(int how);
/*
flag how : MCL_CURRENT and MCL_FUTURE.
*/
munlock—Unlock a range of memory.
int munlock(const void *address, size_t length);
munlockall—Unlock your entire address space.
int munlockall ();
After a call to munlockall, a process's memory may be paged or swapped.
Page Size and Allingment
always take care page size when you pass size to memroy calls.
Example code.
long psize = sysconf(_SC_PAGE_SIZE);
long offset = (long)addr%psize;
long align_addr = (long)addr - offset;
long sync_size = 0;
sync_size = (long)addr + size - align_addr;
long page_num = sync_size / psize;
if( (sync_size % psize) > 0 ){ page_num++; }
sync_size = psize * page_num;
msync( (void*)align_addr, sync_size, MS_SYNC | MS_INVALIDATE );
syconf
#include
sysconf — Query system options at run-time.
long sysconf(int option);
long psize = sysconf(_SC_PAGE_SIZE); // get system page size
Persistents in POSIX
Shared memory is persistent so long as the system remains up.
you can create and map a shared memory region, store some data
into it, close and unmap it, go away for a few days, then come
back and still see the data you stored, assuming no one unlinks
the shared memory object in the meantime. Application should always
include a startup phase where the status and general sanity of all
required objects, including shared memory, is assured. Such a phase
might consist of just shm_unlinking all needed shared memory objects
before anyone opens them up—just to be sure that the memory, when
created and mapped, is empty. This concept should used with all
kind of POIX objects.
No comments:
Post a Comment
would you like it. :)