1MADVISE(2) Linux Programmer's Manual MADVISE(2)
2
3
4
6 madvise - give advice about use of memory
7
9 #include <sys/mman.h>
10
11 int madvise(void *addr, size_t length, int advice);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 madvise(): _BSD_SOURCE
16
18 The madvise() system call advises the kernel about how to handle paging
19 input/output in the address range beginning at address addr and with
20 size length bytes. It allows an application to tell the kernel how it
21 expects to use some mapped or shared memory areas, so that the kernel
22 can choose appropriate read-ahead and caching techniques. This call
23 does not influence the semantics of the application (except in the case
24 of MADV_DONTNEED), but may influence its performance. The kernel is
25 free to ignore the advice.
26
27 The advice is indicated in the advice argument which can be
28
29 MADV_NORMAL
30 No special treatment. This is the default.
31
32 MADV_RANDOM
33 Expect page references in random order. (Hence, read ahead may
34 be less useful than normally.)
35
36 MADV_SEQUENTIAL
37 Expect page references in sequential order. (Hence, pages in
38 the given range can be aggressively read ahead, and may be freed
39 soon after they are accessed.)
40
41 MADV_WILLNEED
42 Expect access in the near future. (Hence, it might be a good
43 idea to read some pages ahead.)
44
45 MADV_DONTNEED
46 Do not expect access in the near future. (For the time being,
47 the application is finished with the given range, so the kernel
48 can free resources associated with it.) Subsequent accesses of
49 pages in this range will succeed, but will result either in re-
50 loading of the memory contents from the underlying mapped file
51 (see mmap(2)) or zero-fill-on-demand pages for mappings without
52 an underlying file.
53
54 MADV_REMOVE (Since Linux 2.6.16)
55 Free up a given range of pages and its associated backing store.
56 Currently, only shmfs/tmpfs supports this; other file systems
57 return with the error ENOSYS.
58
59 MADV_DONTFORK (Since Linux 2.6.16)
60 Do not make the pages in this range available to the child after
61 a fork(2). This is useful to prevent copy-on-write semantics
62 from changing the physical location of a page(s) if the parent
63 writes to it after a fork(2). (Such page relocations cause
64 problems for hardware that DMAs into the page(s).)
65
66 MADV_DOFORK (Since Linux 2.6.16)
67 Undo the effect of MADV_DONTFORK, restoring the default behav‐
68 ior, whereby a mapping is inherited across fork(2).
69
71 On success madvise() returns zero. On error, it returns -1 and errno
72 is set appropriately.
73
75 EAGAIN A kernel resource was temporarily unavailable.
76
77 EBADF The map exists, but the area maps something that isn't a file.
78
79 EINVAL The value len is negative, addr is not page-aligned, advice is
80 not a valid value, or the application is attempting to release
81 locked or shared pages (with MADV_DONTNEED).
82
83 EIO (for MADV_WILLNEED) Paging in this area would exceed the
84 process's maximum resident set size.
85
86 ENOMEM (for MADV_WILLNEED) Not enough memory: paging in failed.
87
88 ENOMEM Addresses in the specified range are not currently mapped, or
89 are outside the address space of the process.
90
92 POSIX.1b. POSIX.1-2001 describes posix_madvise(3) with constants
93 POSIX_MADV_NORMAL, etc., with a behavior close to that described here.
94 There is a similar posix_fadvise(2) for file access.
95
96 MADV_REMOVE, MADV_DONTFORK, and MADV_DOFORK are Linux-specific.
97
99 Linux Notes
100 The current Linux implementation (2.4.0) views this system call more as
101 a command than as advice and hence may return an error when it cannot
102 do what it usually would do in response to this advice. (See the
103 ERRORS description above.) This is non-standard behavior.
104
105 The Linux implementation requires that the address addr be page-
106 aligned, and allows length to be zero. If there are some parts of the
107 specified address range that are not mapped, the Linux version of mad‐
108 vise() ignores them and applies the call to the rest (but returns
109 ENOMEM from the system call, as it should).
110
112 getrlimit(2), mincore(2), mmap(2), mprotect(2), msync(2), munmap(2)
113
115 This page is part of release 3.22 of the Linux man-pages project. A
116 description of the project, and information about reporting bugs, can
117 be found at http://www.kernel.org/doc/man-pages/.
118
119
120
121Linux 2008-04-22 MADVISE(2)