Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

shm_map(9) [freebsd man page]

SHM_MAP(9)						   BSD Kernel Developer's Manual						SHM_MAP(9)

NAME
shm_map, shm_unmap -- map shared memory objects into the kernel's address space SYNOPSIS
#include <sys/types.h> #include <sys/mman.h> int shm_map(struct file *fp, size_t size, off_t offset, void **memp); int shm_unmap(struct file *fp, void *mem, size_t size); DESCRIPTION
The shm_map() and shm_unmap() functions provide an API for mapping shared memory objects into the kernel. Shared memory objects are created by shm_open(2). These objects can then be passed into the kernel via file descriptors. A shared memory object cannot be shrunk while it is mapped into the kernel. This is to avoid invalidating any pages that may be wired into the kernel's address space. Shared memory objects can still be grown while mapped into the kernel. To simplify the accounting needed to enforce the above requirement, callers of this API are required to unmap the entire region mapped by shm_map() when calling shm_unmap(). Unmapping only a portion of the region is not permitted. The shm_map() function locates the shared memory object associated with the open file fp. It maps the region of that object described by offset and size into the kernel's address space. If it succeeds, *memp will be set to the start of the mapping. All pages for the range will be wired into memory upon successful return. The shm_unmap() function unmaps a region previously mapped by shm_map(). The mem argument should match the value previously returned in *memp, and the size argument should match the value passed to shm_map(). Note that shm_map() will not hold an extra reference on the open file fp for the lifetime of the mapping. Instead, the calling code is required to do this if it wishes to use shm_unmap() on the region in the future. RETURN VALUES
The shm_map() and shm_unmap() functions return zero on success or an error on failure. EXAMPLES
The following function accepts a file descriptor for a shared memory object. It maps the first sixteen kilobytes of the object into the ker- nel, performs some work on that address, and then unmaps the address before returning. int shm_example(int fd) { struct file *fp; void *mem; int error; error = fget(curthread, fd, CAP_MMAP, &fp); if (error) return (error); error = shm_map(fp, 16384, 0, &mem); if (error) { fdrop(fp, curthread); return (error); } /* Do something with 'mem'. */ error = shm_unmap(fp, mem, 16384); fdrop(fp, curthread); return (error); } ERRORS
The shm_map() function returns the following errors on failure: [EINVAL] The open file fp is not a shared memory object. [EINVAL] The requested region described by offset and size extends beyond the end of the shared memory object. [ENOMEM] Insufficient address space was available. [EACCES] The shared memory object could not be mapped due to a protection error. [EINVAL] The shared memory object could not be mapped due to some other VM error. The shm_unmap() function returns the following errors on failure: [EINVAL] The open file fp is not a shared memory object. [EINVAL] The address range described by mem and size is not a valid address range. [EINVAL] The address range described by mem and size is not backed by the shared memory object associated with the open file fp, or the address range does not cover the entire mapping of the object. SEE ALSO
shm_open(2) HISTORY
This API was first introduced in FreeBSD 10.0. BSD
December 14, 2011 BSD

Check Out this Related Man Page

SHM_OVERVIEW(7) 					     Linux Programmer's Manual						   SHM_OVERVIEW(7)

NAME
shm_overview - Overview of POSIX shared memory DESCRIPTION
The POSIX shared memory API allows processes to communicate information by sharing a region of memory. The interfaces employed in the API are: shm_open(3) Create and open a new object, or open an existing object. This is analogous to open(2). The call returns a file descriptor for use by the other interfaces listed below. ftruncate(2) Set the size of the shared memory object. (A newly created shared memory object has a length of zero.) mmap(2) Map the shared memory object into the virtual address space of the calling process. munmap(2) Unmap the shared memory object from the virtual address space of the calling process. shm_unlink(3) Remove a shared memory object name. close(2) Close the file descriptor allocated by shm_open(3) when it is no longer needed. fstat(2) Obtain a stat structure that describes the shared memory object. Among the information returned by this call are the object's size (st_size), permissions (st_mode), owner (st_uid), and group (st_gid). fchown(2) To change the ownership of a shared memory object. fchmod(2) To change the permissions of a shared memory object. Versions POSIX shared memory is supported since Linux 2.4 and glibc 2.2. Persistence POSIX shared memory objects have kernel persistence: a shared memory object will exist until the system is shut down, or until all pro- cesses have unmapped the object and it has been deleted with shm_unlink(3) Linking Programs using the POSIX shared memory API must be compiled with cc -lrt to link against the real-time library, librt. Accessing shared memory objects via the file system On Linux, shared memory objects are created in a (tmpfs) virtual file system, normally mounted under /dev/shm. Since kernel 2.6.19, Linux supports the use of access control lists (ACLs) to control the permissions of objects in the virtual file system. CONFORMING TO
POSIX.1-2001. NOTES
Typically, processes must synchronize their access to a shared memory object, using, for example, POSIX semaphores. System V shared memory (shmget(2), shmop(2), etc.) is an older semaphore API. POSIX shared memory provides a simpler, and better designed interface; on the other hand POSIX shared memory is somewhat less widely available (especially on older systems) than System V shared mem- ory. SEE ALSO
fchmod(2), fchown(2), fstat(2), ftruncate(2), mmap(2), mprotect(2), munmap(2), shmget(2), shmop(2), shm_open(3), shm_unlink(3), sem_over- view(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-06-25 SHM_OVERVIEW(7)
Man Page