user13351501
user13351501

Reputation:

What are the differences bettween linux system call mmap(2) and posix mmap(3) function?

What are the differences bettween linux system call mmap(2) and posix mmap(3) function? How to distinguish which one is used when broswing the source code,since they have the same header file.For details, see below.

I am running on Ubuntu.I do not think it matters what operating system you are using.The mannual page really does not supply much useful information indeed.

As per the reply of Jörg W Mittag, I think the mmap must be posix function when i am broswing the source code.But i wonder that why i need not to explicitly link to posix library when using the mmamp(3) function .I mean no extra link flag is needed when complie the source code.

As per the reply of Faschingbauer,some question arise if we make the conclusion that no posix mmap is implenmented.You see, there are some posix function implemented(eg, shm_opn、sem_open, mq_open).In the mean time,there are corresponding ones with the same functions(eg, shmget,semget, msgget).How to explain it?At least, I think some posix functions are implemented by linux.

#log for "man 2 mmap"
MMAP(2)  Linux Programmer's Manual                                                         
NAME
       mmap, munmap - map or unmap files or devices into memory
SYNOPSIS
       #include <sys/mman.h>



#log for "man 3 mmap"
MMAP(3POSIX)  POSIX Programmer's Manual                                                    
PROLOG
       This  manual page is part of the POSIX Programmer's Manual.  The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.    
NAME
       mmap — map pages of memory
SYNOPSIS
       #include <sys/mman.h>

Upvotes: 1

Views: 489

Answers (3)

Armali
Armali

Reputation: 19395

But i wonder that why i need not to explicitly link to posix library when using the mmamp(3) function .

You need not link another library because mmap is contained in GLIBC; you can see this e. g. with

 nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep mmap
00000000000e4610 W mmap
00000000000e4610 W mmap64

Upvotes: 0

POSIX vs. Linux

First, some facts:

  1. POSIX is a standard, made by a standards body. POSIX does not implement anything, but rather define feature set and behavior of interfaces. Part of this definition is a number of man pages - the "POSIX Programmer's Manual"
  2. Linux implements the POSIX standard, just like other UNIX operating systems do. (I do not know if Linux is "POSIX certified", nor do I care.) In implementing the POSIX standard, Linux takes the freedom to extend the standard with Linux specific features; hence it brings its own set of manual pages, the "Linux Programmer's Manual".

Looking at the Linux ("man 2 mmap") man page, you can see that it defines, for example, the MAP_LOCKED bit in the flags argument (btw. MAP_LOCKED makes a separate call to mlock() unnecessary). This flag does not appear in the POSIX man page ("man 3 mmap"), because it is not required by the POSIX standard for a conforming implementation.

That said, there is no way to use an alternative implementation of mmap() in Linux. If some source code that you are reading uses mmap(), and you are on Linux, then the Linux implementation of mmap() is used, simply because there is no POSIX implementation of it.

Respectively, the POSIX version is contained in the Linux implementation. Linux is "compatible" with POSIX, so to say - it does not redefine any feature required by POSIX (this would mean to violate the standard), but only adds extensions like the MAP_LOCKED above.

Manual Pages

Hm. My personal opinion is that the POSIX version of, say, the mmap man page is only there to confuse users. If you inadvertently hit the section "3" mmap() man page, and you don't know what the relationship is between POSIX and Linux, then you are indeed seriously confused at best, or on the wrong track at worst.

I suggest you omit the section number and just say "man mmap" - this searches all the manual sections for occurences of "mmap" and stops at the first (see "man man" for the exact definition).

(This does not work as envisioned with "man write" when you are searching for the definition of the write() system call, because there is a command "write" with the same same in section "1" :-) )

System Calls

As stated by "man man", manual section "2" contains system call documentation. Knowing that mmap() is implemented by the Linux kernel (because it is the kernel who implements core OS functionality like memory management) can only help to clear up the confusion as to whether the documentation you are reading is what you want.

Upvotes: 6

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369594

What are the differences bettween linux system mmap(2) and posix mmap(3) function?

Section 2 documents syscalls. Section 3 documents functions. Therefore, mmap(2) is not a function at all, it is a syscall.

How to distinguish which one is used when broswing the source code?

If it is a function call, it is mmap(3). If it is a syscall, it is mmap(2). Since it is impossible to portably call syscalls from C, there will always be some sort of macro or wrapper function for the syscall.

Also, unless you are reading the source code of the runtime library for a C compiler (e.g. GCC's) or the source code of a POSIX library (such as Glibc, dietlibc, µClibc, or musl), it is highly unlikely that you will find any syscalls in the code.

Upvotes: 0

Related Questions