tags:

views:

395

answers:

1
+5  Q: 

mmap(2) vs mmap(3)

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.

+12  A: 

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".

Martin v. Löwis
Thank you, that was very clear. Thanks also for the pointer into the C library source tree. Although I do a fair amount of work at kernel and driver level, I rarely foray out into user space for anything other than test utilities, and I haven't looked into glibc source at all. I can see I've been missing a very useful approach to a number of issues.
EQvan