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