Address is NULL - the kernel to pick any address. If an address is specified, it must be page aligned and not already in use. If the requested mapping would conflict with another mapping or would not be page aligned, mmap() may fail. Length - tells the kernel how much of the file to map into memory. Protection - The process controls which types of access are allowed to the new memory region. PROT_NONE if no access to the mapped region should be allowed. A file can be mapped only for access types that were also requested when the file was originally opened. For example, a file that was opened O_RDONLY cannot be mapped for writing with PROT_WRITE . Flag Description PROT_READ The mapped region may be read.
PROT_WRITE The mapped region may be
written. PROT_EXEC The mapped region may be executed. Flag POSIX? Description MAP_ANONYMOUS Yes Ignore fd ,create an anonymous mapping. MAP_FIXED Yes Fail if address is invalid. MAP_PRIVATE Yes Writes are private to process. MAP_SHARED Yes Writes are copied to the file. MAP_DENYWRITE No Do not allow normal writes to the file. MAP_GROWSDOWN No Grow the memory region downward. MAP_LOCKED No Lock the pages into memory. All calls to mmap() must specify one of MAP_PRIVATE or MAP_SHARED ; the remainder of the flags are optional. fd - for the file that is to be mapped into memory. IfMAP_ANONYMOUS was used, this value is ignored. Offset - where in the file the mapping should begin, and it must be an integral multiple of the page size. Most applications begin the mapping from the start of the file by specifying an offset of zero. mmap() returns an address that should be stored in a pointer. If an error occurred, it returns the address that is equivalent to -1. After a process is finished with a memory mapping, it can unmap the memory region through munmap() . All memory regions are unmapped when a process terminates or begins a new program through an exec () system call. #include<sys/mman.h> int munmap(caddr_t addr, int length); If a memory map is being used to write to a file, the modified memory pages and the file will be different for a period of time. If a process wishes to immediately write the pages to disk, it may use msync() . #include <sys/mman.h> int msync(caddr_t addr, size_t length, int flags);
addr and length , specify the region to sync
to disk. Flags specifies how the memory and disk should be synchronized Memory regions may be paged to disk when memory becomes scarce . Applications that are sensitive to external timing constraints affected by the delay that results from the kernel paging memory back into RAM. To make these applications more robust, Linux allows a process to lock memory in RAM to make these timings more predictable. For security reasons, only processes running with root permission may lock memory. If any process could lock regions of memory, a rogue process could lock all the system's RAM, making the system unusable. The total amount of memory locked by a process cannot exceed its RLIMIT_MEMLOCK usage limit. The following calls are used to lock and unlock memory regions: #include <sys/mman.h> int mlock(caddr_t addr, size_t length); int mlockall(int flags); int munlock(caddr_t addr, size_t length); int munlockall(void); mlock() - locks length bytes starting at address addr . An entire page of memory must be locked at a time, so mlock() actually locks all the pages between the page containing the first address and the page containing the final address to lock, inclusively. If a process wants to lock its entire address space, mlockall() should be used. Flags MCL_CURRENT - All the pages currently in the process's address space are locked into RAM. MCL_FUTURE - All pages added to the process's address space will be locked into RAM. munlockall() - unlocks all the process's pages. munlock() - takes the same arguments as mlock() and unlocks the pages containing the indicated region. Locking a page multiple times is equivalent to locking it once. In either case, a single call to munlock() unlocks the affected pages. To read and write various types of data to consecutive areas of a file. done fairly easily through multiple read() and write() calls, this solution is not particularly efficient. Applications could instead move all the data into a consecutive memory region, allowing a single system call, but doing so results in many unnecessary memory operations. readv() and writev () - implements scatter/gather reads and writes. (v – vector) reads scatter data across memory, and the writes gather data from different memory regions Instead of being passed a single pointer and buffer size , these system calls are passed as an array of records Each record describes a buffer. Each buffer is described by a struct iovec . #include <sys/uio.h> struct iovec { void * iov_base; /* buffer address */ size_t iov_len; /* buffer length */ }; iov_base - points to the buffer space. iov_len item - the number of characters in the buffer. (same as the second and third parameters passed to read() and write() . the prototypes for readv() and writev() : #include <sys/uio.h> int readv(int fd, const struct iovec * vector, size_t count); int writev(int fd, const struct iovec * vector, size_t count); Fd - the file descriptor to be read from or written to. 2nd argument -vector points to an array of count struct iovec items. Both functions return the total number of bytes read or written. Mandatory locking doesn’t require cooperation from the participating processes. Mandatory locking causes the kernel to check every open, read, and write to verify that the calling process isn’t violating a lock on the given file. 1. lock files 2. record locking. 1. Lock files 1. lock file exists - the data file is considered locked and other processes do not access it. 2. lock file does not exist - a process creates the lock file and then accesses the file. 3. As long as the procedure for creating the lock file is atomic, this method guarantees that only one process accesses the file at a time. 1. Only one process may have the lock at a time, preventing multiple processes from reading a file simultaneously . 2. The O_EXCL flag is reliable only on local file systems. None of the network file systems supported by Linux preserve O_EXCL semantics between multiple machines that are locking a common file. 3. The locking is only advisory; processes can update the file despite the existence of a lock. 4. If the process that holds the lock terminates abnormally, the lock file remains. If the pid is stored in the lock file, other processes can check for the existence of the locking process and remove the lock if it has terminated - a complex procedure. L-type contains the following flags, F_RDLCK - A read(shared) lock is being set. F_WRLCK - A write (exclusive) lock is being set. F_UNLCK - An existing lock is being removed. l_whence and l_start - specify where the region begins in. l_len - tells how long, in bytes, the lock is. If l_len is 0, the lock is considered to extend to the end of the file. The final entry, l_pid , is used only when locks are being queried - set to the pid of the process that owns the queried lock. 2nd argument command, 1. F_SETLK - Sets the lock . If the lock cannot be granted , EAGAIN is returned. If the l_type is set to F_UNLCK , an existing lock is removed. 2. F_SETLKW - Similar to F_SETLK , but blocks until the lock is granted. F_GETLK - Checks to see if the described lock would be granted. If the lock would be granted - struct flock is unchanged except for l_type , which is set to F_UNLCK . If the lock would not be granted - l_pid is set to the pid of the process that holds the conflicting lock. Success (0) is returned whether or not the lock would be granted.