Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mmap
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example of usage under the C programming language == <syntaxhighlight lang="c" line> #include <sys/types.h> #include <sys/mman.h> #include <err.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* This example shows how an mmap of /dev/zero is equivalent to using anonymous memory (MAP_ANON) not connected to any file. N.B. MAP_ANONYMOUS or MAP_ANON are supported by most UNIX versions, removing the original purpose of /dev/zero. */ /* Does not work on OS X or macOS, where 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\n", parpid, anon, zero); switch ((childpid = fork())) { case -1: err(1, "fork"); /* NOTREACHED */ case 0: childpid = getpid(); printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero); sleep(3); printf("PID %d:\tanonymous %s, zero-backed %s\n", 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\n", parpid, anon, zero); munmap(anon, 4096); munmap(zero, 4096); close(fd); return EXIT_SUCCESS; } </syntaxhighlight> sample output: <pre><nowiki> PID 22475: anonymous string 1, zero-backed string 1 PID 22476: anonymous string 1, zero-backed string 1 PID 22475: anonymous string 2, zero-backed string 2 PID 22476: anonymous string 2, zero-backed string 2 </nowiki></pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)