mmap(2) vs mmap(3)

linux, memory, mmap

Solution

`mmap(3)` is supposed to be the POSIX function. It should in all respect provide the very semantics that POSIX mandates, even if that deviates from what Linux does "natively". `mmap(2)` is the system call, and provides all bells and whistles that Linus considers important. The C library function is, of course, implemented using the system call.

To invoke the system call literally (bypassing the C library), you can use `<sys/syscall.h>`.

The actual implementation of mmap(3) can be found in the C library. As you can see, it usually transfers control directly to mmap(2), unless, at compile time (of glibc), mmap2(2) was selected as the "back-end".

Problem

Does anyone know what the difference between `mmap(2)` and `mmap(3)` is? Man section 3 is described as "This chapter describes all library functions excluding the library functions described in chapter 2, which implement system calls." Doesn't `mmap(3)` perform a system call? Reading the two man pages, I see that `mmap(2)` seems to accept a much wider variety of flags than `mmap(3)` does, and claims to be able to map device I/O spaces. `mmap(3)` claims to be able to map "shared memory objects" and "typed memory objects" in addtion to files, but doesn't mention device I/O. Since the two functions have the same name, I'm not even sure how I can choose one rather than the other.

Original source