词条 | Mmap |
释义 |
In computing, In Linux, Mac OS X and the BSDs, Historymmap and associated systems calls were designed as part of the Berkeley Software Distribution (BSD) version of Unix. Their API was already described in the 4.2BSD System Manual, even though it was neither implemented in that release, nor in 4.3BSD.[1] Sun Microsystems had implemented this very API, though, in their SunOS operating system. The BSD developers at U.C. Berkeley requested Sun to donate its implementation, but these talks never led to any transfer of code; 4.3BSD-Reno was shipped instead with an implementation based on the virtual memory system of Mach.[2]File-backed and anonymousFile-backed mapping maps an area of the process's virtual memory to files; i.e. reading those areas of memory causes the file to be read. It is the default mapping type. Anonymous mapping maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero.[3] In this respect an anonymous mapping is similar to Memory visibilityIf the mapping is shared (the If the mapping is private (the A process reading from or writing to the underlying file will not always see the same data as a process that has mapped the file, since the segment of the file is copied into RAM and periodically flushed to disk. Synchronization can be forced with the mmap(2)ing files can significantly reduce memory overhead for applications accessing the same file; they can share the memory area the file encompasses, instead of loading the file for each application that wants access to it. This means that mmap(2) is sometimes used for Interprocess Communication (IPC). On modern operating systems mmap(2) is typically preferred to the System V IPC Shared Memory facility. The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that SystemV shared memory is persistent: unless explicitly removed by a process, it is kept in memory and remains available until the system is shut down. mmap'd memory is not persistent between application executions (unless it is backed by a file). Example of usage under the C programming language
/* Does not work on OS X, as you can't mmap over /dev/zero */ int main(void) {const char str1[] = "string 1"; const char str2[] = "string 2"; pid_t parpid = getpid(), childpid; int fd = -1; char *anon, *zero; if ((fd = open("/dev/zero", O_RDWR, 0)) == -1) err(1, "open"); anon = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0); zero = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (anon == MAP_FAILED || zero == MAP_FAILED) errx(1, "either mmap"); strcpy(anon, str1); strcpy(zero, str1); printf("PID %d:\\tanonymous %s, zero-backed %s\", parpid, anon, zero); switch ((childpid = fork())) { case -1: err(1, "fork"); /* NOTREACHED */ case 0: childpid = getpid(); printf("PID %d:\\tanonymous %s, zero-backed %s\", childpid, anon, zero); sleep(3); printf("PID %d:\\tanonymous %s, zero-backed %s\", childpid, anon, zero); munmap(anon, 4096); munmap(zero, 4096); close(fd); return EXIT_SUCCESS; } sleep(2); strcpy(anon, str2); strcpy(zero, str2); printf("PID %d:\\tanonymous %s, zero-backed %s\", parpid, anon, zero); munmap(anon, 4096); munmap(zero, 4096); close(fd); return EXIT_SUCCESS; } sample output: PID 22475: anonymous string 1, zero-backed string 1PID 22476: anonymous string 1, zero-backed string 1PID 22475: anonymous string 2, zero-backed string 2PID 22476: anonymous string 2, zero-backed string 2 See also
References and further reading1. ^{{cite report |author1=William Joy |author2=Eric Cooper |author3=Robert Fabry |author4=Samuel Leffler |author5=Kirk McKusick |author6=David Mosher |title=4.2BSD System Manual |year=1983 |publisher=Computer Systems Research Group, University of California, Berkeley}} 2. ^{{cite encyclopedia |title=Twenty Years of Berkeley Unix: From AT&T-Owned to Freely Redistributable |first=Marshall Kirk |last=McKusick |authorlink=Marshall Kirk McKusick |encyclopedia=Open Sources: Voices from the Open Source Revolution |year=1999 |publisher=O'Reilly}} 3. ^{{cite web |url=https://www.kernel.org/doc/man-pages/online/pages/man2/mmap.2.html |title=mmap(2) - Linux manual page}}
2 : Inter-process communication|C POSIX library |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。