mmap

MMAP(2)                    Linux Programmer's Manual                   MMAP(2)



åå
       mmap, munmap - ãã¡ã¤ã«ãããã¤ã¹ãã¡ã¢ãªã«ããã/ã¢ã³ããããã

æ¸å¼
       #include <sys/mman.h>

       void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);
       int munmap(void *addr, size_t length);

       See NOTES for information on feature test macro requirements.

説æ
       mmap()  ã¯ãæ°ãããããã³ã°ãå¼ã³åºãåããã»ã¹ã®ä»®æ³ã¢ãã¬ã¹ç©ºéã«ä½æããã
       æ°ãããããã³ã°ã®éå§ã¢ãã¬ã¹ã¯ addr ã§æå®ãããããããã³ã°ã®é·ã㯠length
       å¼ãæ°ã§æå®ãããã

       addr ã NULL ã®å ´åãã«ã¼ãã«ããããã³ã°ãä½æããã¢ãã¬ã¹ãé¸æããã
       ãã®æ¹æ³ã¯æã移æ¤æ§ã®ããæ°ãããããã³ã°ã®ä½ææ¹æ³ã§ããã addr ã NULL
       ã§ãªãå ´åãã«ã¼ãã«ã¯ãããã³ã°ãã©ãã«éç½®ãããã®ãã³ãã¨ã㦠addr ã使ç¨ãããLinux
       ã§ã¯ããããã³ã°ã¯ããè¿ãã®ãã¼ã¸å¢çã«ä½æãããã
       æ°ãããããã³ã°ã®ã¢ãã¬ã¹ã¯ãå¼ã³åºãã®è¿ãå¤ã¨ãã¦è¿ãããã

       ãã¡ã¤ã«ãããã³ã°ã®å容ã¯ã ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ fd ã§åç§ããããã¡ã¤ã«
       (ãããã¯ä»ã®ãªãã¸ã§ã¯ã) ã®ãªãã»ãã offset ããéå§ããã length
       ãã¤ãã®ãã¼ã¿ã§åæåããã (ãã¡ã¤ã«ãããã³ã°ã¯ç¡åãããã³ã°ã®å対èªã§ããã
       MAP_ANONYMOUS ãåç§)ã offset 㯠sysconf(_SC_PAGE_SIZE)
       ãè¿ããã¼ã¸ãµã¤ãºã®åæ°ã§ãªããã°ãªããªãã

       å¼ãæ° prot ã«ã¯ããããã³ã°ã®ã¡ã¢ãªä¿è·ãã©ã®ããã«è¡ãªãããæå®ãã
       (ãã¡ã¤ã«ã®ãªã¼ãã³ã¢ã¼ãã¨çç¾ãã¦ã¯ãããªã)ã prot ã«ã¯ã PROT_NONE
       ãã以ä¸ã®ãã©ã°ãã²ã¨ã¤ä»¥ä¸ãããæ¯ã®è«çå (OR) ãã¨ã£ããã®ã æå®ã§ããã

       PROT_EXEC  ãã¼ã¸ã¯å®è¡å¯è½ã§ããã

       PROT_READ  ãã¼ã¸ã¯èªã¿è¾¼ã¿å¯è½ã§ããã

       PROT_WRITE ãã¼ã¸ã«æ¸ãè¾¼ã¿å¯è½ã§ããã

       PROT_NONE  ãã¼ã¸ã«ã¯ã¢ã¯ã»ã¹ã§ããªãã

       flags å¼ãæ°ã«ããããããã³ã°ã«å¯¾ããæ´æ°ãåãé åããããã³ã°ãã¦ãã ä»ã®ãã‐
       ã»ã¹ã«è¦ããããæ´æ°ããããã³ã°åã®ãã¡ã¤ã«ãéãã¦
       ä¼ããããããã決å®ãããããã®åä½ã¯ã以ä¸ã®å¤ã®ããããä¸ã¤ã ã (è¤æ°ã¯æå®ã§ããªã) ã
       flags ã«å«ãããã¨ã§æå®ããã

       MAP_SHARED ãã®ãããã³ã°ãå±æããã
                  ãããã³ã°ã«å¯¾ããæ´æ°ã¯ãã®ãã¡ã¤ã«ããããã³ã°ãã¦ããä»ã®ããã»ã¹
                  ããè¦ãããæ´æ°ã¯ãããã³ã°åã®ãã¡ã¤ã«ãéãã¦ä¼ããããã
                  ãã ãããã¡ã¤ã«ã®å®éã®æ´æ°ã¯ msync(2)  ã¾ã㯠munmap()
                  ãå¼ã°ããã¾ã§è¡ãããªããã¨ãããã

       MAP_PRIVATE
                  ãã©ã¤ãã¼ã㪠copy-on-write (æ¸ãè¾¼ã¿æã³ãã¼) ããããçæããã
                  ãããã³ã°ã«å¯¾ããæ´æ°ã¯åããã¡ã¤ã«ããããã³ã°ãã¦ããä»ã®ããã»ã¹
                  ã«ã¯è¦ãããæ´æ°ããããã³ã°åã®ãã¡ã¤ã«ãéãã¦ä¼ãããããã¨ããªãã mmap()
                  ã®å¼ã³åºãå¾ã«ãããã³ã°åã®ãã¡ã¤ã«ã«å¯¾ãã¦è¡ãããå¤æ´ãã
                  ãããé åã«åæ ããããã©ããã¯è¦å®ããã¦ããªãã

       ä¸è¨ã®äºã¤ã®ãã©ã°ã¯ POSIX.1-2001 ã§è¦å®ããã¦ããã

       ããã«ã以ä¸ã®å¤ã®ãã¡ 0 å以ä¸ããããæ¯ã®è«çå (OR) 㧠flags ã«æå®ãããã¨ãã§ããã

       MAP_32BIT (Linux 2.4.20, 2.6 以é)
              ãããã³ã°ãããã»ã¹ã®ã¢ãã¬ã¹ç©ºéã®åé  2 ã®ã¬ãã¤ã以åã«éç½®ããã
              ãã®ãã©ã°ããµãã¼ãããã¦ããã®ã¯ x86-64 ã¢ã¼ããã¯ãã£ä¸ã® 64 ããããã‐
              ã°ã©ã ã®ã¿ã§ããã
              ãã®ãã©ã°ã追å ãããã®ã¯ãã¹ã¬ããã®ã¹ã¿ãã¯ãã¡ã¢ãªã®åé  2GB 以åã®
              ã©ããã«å²ãå½ã¦ããã¨ã§ãåæã®ããã¤ãã® 64 ãããããã»ããµã«ããã ã³ã³ãã‐
              ã¹ãã¹ã¤ããã®æ§è½åé¡ãæ¹åããããã§ããã æè¿ã® x86-64 ãã‐
              ã»ããµã§ã¯ãã®æ§è½åé¡ã¯ãã¯ãåå¨ããã
              ãã®ãããªã·ã¹ãã ã§ã¯ãã®ãã©ã°ã使ç¨ããå¿è¦ã¯ãªãã MAP_FIXED
              ãã»ããããã¦ããå ´åã¯ã MAP_32BIT ãã©ã°ã¯ç¡è¦ãããã

       MAP_ANON
              MAP_ANONYMOUS ã®å義èªãéæ¨å¥¨ã

       MAP_ANONYMOUS
              ãããã³ã°ã¯ã©ã®ãã¡ã¤ã«ã¨ãé¢é£ä»ããããªãã ãããã³ã°ã®å容㯠0 ã§åæåãããã
              å¼ãæ° fd 㨠offset ã¯ç¡è¦ãããã ãã ããå®è£ã«ãã£ã¦ã¯ MAP_ANONYMOUS
              (ããã㯠MAP_ANON)  ãæå®ãããå ´åã fd ã -1 ã«ããå¿è¦ãããã
              移æ¤æ§ãå¿è¦ãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯å¿ã fd ã -1 ã«ãã¹ãã§ããã
              MAP_ANONYMOUS 㨠MAP_SHARED ãçµã¿åããã¦ã®å©ç¨ã¯ ã«ã¼ãã« 2.4
              以éã® Linux ã§ã®ã¿ãµãã¼ãããã¦ããã

       MAP_DENYWRITE
              ãã®ãã©ã°ã¯ç¡è¦ããã
              (ãã£ã¨åã¯ããããåã®ãã¡ã¤ã«ã¸ã®æ¸ãè¾¼ã¿ãè¡ããã¨ããã¨ãã¨ã©ã¼ ETXTBUSY
              ã§å¤±æããããã«ã·ã°ãã«ãè¨å®ããã¦ãããããã㯠denial-of-service
              (ãµã¼ãã¹æå¦) æ»æã®åå ã¨ãªã£ã)ã

       MAP_EXECUTABLE
              ãã®ãã©ã°ã¯ç¡è¦ãããã

       MAP_FILE
              äºææ§ã®ããã®ãã©ã°ãç¡è¦ãããã

       MAP_FIXED
              addr ãã¢ãã¬ã¹ã®ãã³ãã¨ãã¦ä½¿ç¨ããã®ã§ã¯ãªãã addr
              ã§æå®ãããã¢ãã¬ã¹ããã®ã¾ã¾ä½¿ç¨ãã¦ãããã³ã°ãéç½®ããã addr
              ã¯ãã¼ã¸ãµã¤ãºã®åæ°ã§ãªããã°ãªããªãã addr 㨠len
              ã§æå®ãããã¡ã¢ãªé åãæ¢åã®ãããã³ã°ã®ãã¼ã¸ã¨éãªãå ´åã æ¢å‐
              ã®ãããã³ã°ã®éãªã£ãé¨åã¯æ¨ã¦ãããã ããæå®ãããã¢ãã¬ã¹ã使ç¨ã§ããªãå ´åã
              mmap()  ã¯å¤±æããã
              ãããã³ã°ã«å¯¾ãã¦åºå®ã¢ãã¬ã¹ãè¦æ±ããã®ã¯ç§»æ¤æ§ã®é¢ã§å£ãã®ã§ã
              ãã®ãªãã·ã§ã³ã¯ä½¿ç¨ããªããã¨ãæ¨å¥¨ããã

       MAP_GROWSDOWN
              ã¹ã¿ãã¯ç¨ã«ä½¿ç¨ãããããããã³ã°ãã¡ã¢ãªåã§éåãã«è¡ããã¨ã
              ã«ã¼ãã«ä»®æ³ã¡ã¢ãªã·ã¹ãã ã«æ示ããã
              (訳注ï¼ãããã³ã°ã¯é常ã¯ã¡ã¢ãªã»ã¢ãã¬ã¹ãå¢å ããåãã«è¡ããã
              ãã®ãªãã·ã§ã³ãæå®ããã¨éåãã«ãããã³ã°ãè¡ã)

       MAP_HUGETLB (Linux 2.6.32 以é)
              "huge page" ã使ã£ã¦ãããã³ã°ãå²ãå½ã¦ãã詳ããæå ±ã¯ãLinux
              ã«ã¼ãã«ã½ã¼ã¹ã® Documentation/vm/hugetlbpage.txt ãåç§ã

       MAP_LOCKED (Linux 2.5.37 以é)
              ããããããé åã®ãã¼ã¸ã mlock(2)  ã®æ¹æ³ã§ã¡ã¢ãªåã«ããã¯ããã
              ãã以åã®ã«ã¼ãã«ã§ã¯ããã®ãã©ã°ã¯ç¡è¦ãããã

       MAP_NONBLOCK (Linux 2.5.46 以é)
              MAP_POPULATE ã¨çµã¿åãããå ´åã®ã¿æå³ãæã¤ã read-ahead (åãã£ã¦èª‐
              ã¿è¾¼ããã¨) ãå®è¡ããªãã åã«ããã§ã« RAM ä¸ã«å‐
              å¨ãããã¼ã¸ã«å¯¾ãã¦ã®ã¿ãã¼ã¸ãã¼ãã«ã¨ã³ããªãä½æããã Linux 2.6.23
              以éã§ã¯ããã®ãã©ã°ã¯ MAP_POPULATE ã«ä½ã®å½±é¿ãä¸ããªãã ãã¤ã
              MAP_POPULATE 㨠MAP_NONBLOCK
              ãçµã¿åãããå ´åã®åä½ã¯å®è£ãç´ããããããããªãã

       MAP_NORESERVE
              ãã®ãããã³ã°ã«å¯¾ããã¹ã¯ãã空éã®äºç´ãè¡ããªãã
              ã¹ã¯ãã空éãäºç´ããå ´åã¯ããã®ãããã³ã°ã®å¤æ´ãå¿ãå¯è½ãªãã¨ã
              ä¿è¨¼ããããäºç´ãè¡ããªãã£ãå ´åãç©çã¡ã¢ãªã«ç©ºãããªã㨠æ¸ãè¾¼ã¿æã«
              SIGSEGV ã¨ã©ã¼ãåãåããã¨ãããã proc(5) ã®
              /proc/sys/vm/overcommit_memory ãã¡ã¤ã«ã«ã¤ãã¦ã®è°è«ãåç§ã
              ãã¼ã¸ã§ã³ 2.6 ããåã®ã«ã¼ãã«ã§ã¯ããã®ãã©ã°ã¯æ¸ãè¾¼ã¿å¯è½ãª
              ãã©ã¤ãã¼ãã»ãããã³ã°ã«ã¤ãã¦ã®ã¿å¹æããã£ãã

       MAP_POPULATE (Linux 2.5.46 以é)
              ãããã³ã°ç¨ã®ãã¼ã¸ãã¼ãã«ãéç½® (populate) ãã
              ãã¡ã¤ã«ãããã³ã°ã®å ´åã«ã¯ãããã«ãããã¡ã¤ã«ãåèªã¿ (read-ahead)
              ãè¡ãããããã®ä»¥å¾ã¯ããããã³ã°ã«å¯¾ããã¢ã¯ã»ã¹ããã¼ã¸ãã©ã¼ã«ã㧠ãã‐
              ãã¯ããããã¨ããªããªãã Linux 2.6.23
              以éã§ã®ã¿ãã©ã¤ãã¼ãã»ãããã³ã°ã«ã¤ã㦠MAP_POPULATE ããµãã¼ãããã¦ããã

       MAP_STACK (Linux 2.6.27 以é)
              ããã»ã¹ãã¹ã¬ããã®ã¹ã¿ãã¯ã«é©ããã¢ãã¬ã¹ã«ãããã³ã°ãå²ãå½ã¦ãã
              ç¾å¨ã®ã¨ããããã®ãã©ã°ã¯ä½ãããªããã glibc
              ã®ã¹ã¬ããå®è£ã§ã¯ä½¿ç¨ããã¦ããã ããã¯ãããã¤ãã®ã¢ã¼ã‐
              ãã¯ãã£ã§ã¯ã¹ã¿ãã¯ã®å²ãå½ã¦ã«é¢ãã¦ç¹å¥ãªæ±ã ãå¿è¦ãªå ´åã«ãglibc
              ã«ãã®ãµãã¼ããå¾ã§ééçã«å®è£ã§ããããã«ãã ããã§ããã

       MAP_UNINITIALIZED (Linux 2.6.33 以é)
              ç¡åãã¼ã¸ (anonymous page)
              ã®ã¯ãªã¢ãè¡ããªãããã®ãã©ã°ã¯çµã¿è¾¼ã¿ããã¤ã¹
              ã§ã®æ§è½åä¸ãç®çã«ä½ããã¦ãã®ã§ãããã«ã¼ãã«ã®è¨å®ã§
              CONFIG_MMAP_ALLOW_UNINITIALIZED ãªãã·ã§ã³ãæå¹ã«ãªã£ã¦ããå ´åã®ã¿ã
              ãã®ãã©ã°ã¯å¹æãæã¤ã ã»ã‐
              ã¥ãªãã£é¢ã®èæ®ããããã®ãªãã·ã§ã³ã¯é常çµã¿è¾¼ã¿ããã¤ã¹ (ããªãã¡ã
              ã¦ã¼ã¶ã¡ã¢ãªã®å容ãå®å¨ã«å¶å¾¡åã«ãããããã¤ã¹) ã«ããã¦ã®ã¿æå¹ã«ãããã

       ä¸è¨ã®ãã©ã°ã®ä¸ã§ã¯ã MAP_FIXED ã ãã POSIX.1-2001 ã§è¦å®ããã¦ããã
       ããããªãããã»ã¨ãã©ã®ã·ã¹ãã 㧠MAP_ANONYMOUS (ã¾ãã¯ãã®å義èªã§ãã MAP_ANON)
       ããµãã¼ãããã¦ããã

       ããã¤ãã®ã·ã¹ãã ã§ã¯ãä¸è¨ä»¥å¤ã«ãã©ã°ã¨ã㦠MAP_AUTOGROW, MAP_AUTORESRV,
       MAP_COPY, MAP_LOCAL ãè¦å®ããã¦ããã

       mmap()  ã«ãã£ã¦ããããããã¡ã¢ãªã®å±æ§ã¯ fork(2)  ã®éã«ç¶æ¿ãããã

       ãã¡ã¤ã«ã¯ãã¼ã¸ãµã¤ãºã®æ´æ°åã®é åã«ããããããããµã¤ãºããã¼ã¸ãµã¤ãºã®
       æ´æ°åã§ãªããã¡ã¤ã«ã®å ´åããããæã«æ®ãã®é å㯠0 ã§åãããããã®é åã¸
       æ¸ããã¿ãè¡ã£ã¦ããã¡ã¤ã«ã«æ¸ãåºããããã¨ã¯ãªãããããã³ã°ãè¡ã£ãå
       ãã¡ã¤ã«ã®ãµã¤ãºãå¤æ´ããå ´åãåãã¡ã¤ã«ã®è¿½å ããããåé¤ãããé åã«å¯¾å¿
       ãããããããããã¼ã¸ã«å¯¾ãã¦ã©ã®ãããªå½±é¿ããããã¯è¦å®ããã¦ããªãã

   munmap()
       ã·ã¹ãã ã³ã¼ã« munmap()  ã¯æå®ãããã¢ãã¬ã¹ç¯å²ã®ããããæ¶å»ãã
       ãã以éã®ãã®ç¯å²åã¸ã®ã¡ã¢ãªåç§ã¯ä¸æ£ã¨ãªãã ãã®é åã¯ããã‐
       ã»ã¹ãçµäºããã¨ãã«ãèªåçã«ã¢ã³ããããããã ä¸æ¹ããã¡ã¤ã«è¨è¿°åãã¯ã‐
       ã¼ãºãã¦ãããã®é åã¯ã¢ã³ããããããªãã

       addr ã¢ãã¬ã¹ã¯ãã¼ã¸ãµã¤ãºã®æ´æ°åã§ãªããã°ãªããªããæå®ãããç¯å²ã®ä¸é¨åã
       å«ãå¨ã¦ã®ãã¼ã¸ã¯ã¢ã³ãããããããã以éã«ãããã®ãã¼ã¸ã¸ã®åç§ããã㨠SIGSEGV
       ãçºçããã æå®ããç¯å²åã«ãããããããã¼ã¸ãä¸ã¤ãå«ã¾ãã¦ããªãå ´åã§ã ã¨ã©ã¼ã«ãªããªãã

   ãã¡ã¤ã«ã¨é¢é£ä»ãããããããã³ã°ã«å¯¾ããã¿ã¤ã ã¹ã¿ã³ãã®æ´æ°
       ãã¡ã¤ã«ã¨é¢é£ä»ãããããããã³ã°ã®å ´åããããã³ã°ããããã¡ã¤ã«ã® st_atime
       ãã£ã¼ã«ãã¯ã mmap()  ããã¦ããã¢ã³ããã (unmap) ãããã¾ã§ã®éã«æ´æ°ããããã¨ãããã
       ããã¾ã§ã«æ´æ°ãè¡ããã¦ããªããã°ããããããããã¼ã¸ã¸ã®æåã®åç§ããã£ã éã«æ´æ°ãããã

       PROT_WRITE 㨠MAP_SHARED
       ã®ä¸¡æ¹ãæå®ãã¦ãããããããã¡ã¤ã«ã®å ´åãæ¸ãè¾¼ã¿ãããã¨ã st_ctime 㨠st_mtime
       ã®ä¸¡ãã£ã¼ã«ãã¯ãããããããé åã¸ã®æ¸ãè¾¼ã¿ããå¾ã§ã MS_SYNC ã¾ã㯠MS_ASYNC
       ãã©ã°ãæå®ã㦠msync(2)  ãå¼ã°ããåã¾ã§ã«æ´æ°ãããã

è¿ãå¤
       mmap()  ã¯æåããã¨ããããããé åã¸ã®ãã¤ã³ã¿ãè¿ãã 失æããã¨å¤ MAP_FAILED (ã¤ã¾ã
       (void *) -1)  ãè¿ãã errno ãã¨ã©ã¼ã®å容ã«ãããã£ã¦ã»ãããããã munmap()
       ã¯æåãã㨠0 ãè¿ãã失æãã㨠-1 ãè¿ãã errno ãã»ããããã (å¤ãã®å ´å EINVAL
       ã«ãªãã ãã)ã

ã¨ã©ã¼
       EACCES 以ä¸ã®ããããã®å ´åã ãã¡ã¤ã«è¨è¿°åã®åç§åãé常ã®ãã¡ã¤ã«ã§ã¯ãªã
              (non-regular file) ã MAP_PRIVATE ãè¦æ±ããã fd ã¯èª‐
              ã¿è¾¼ã¿ç¨ã«ãªã¼ãã³ããã¦ããªãã MAP_SHARED ãè¦æ±ã㦠PROT_WRITE
              ãã»ããããã fd ã¯èªã¿æ¸ãã¢ã¼ã (O_RDWR)  ã§ãªã¼ãã³ããã¦ããªãã
              PROT_WRITE ãã»ãããããããã¡ã¤ã«ã¯è¿½å  (append) å°ç¨ã§ããã

       EAGAIN ãã¡ã¤ã«ãããã¯ããã¦ãããã¾ãã¯ããã¯ããã¦ããã¡ã¢ãªãå¤ããã (setrlimit(2)
              ãåç§)ã

       EBADF  fd ãæå¹ãªãã¡ã¤ã«è¨è¿°å (file descriptor) ã§ã¯ãªã (ãã¤
              MAP_ANONYMOUS ãã»ããããã¦ããªã)ã

       EINVAL addr ã length ã offset ãé©åã§ãªã
              (ä¾ãã°ã大ããããã¨ãããã¼ã¸å¢çã«ã¢ã©ã¤ã³ã¡ã³ãããã¦ããªã)ã

       EINVAL (Linux 2.6.12 以é)  length ã 0 ã§ãã£ãã

       EINVAL flags ã« MAP_PRIVATE 㨠MAP_SHARED ã®ã©ã¡ããå«ã¾ãã¦ããªãã£ãããããã¯
              ãã®ä¸¡æ¹ãå«ã¾ãã¦ããã

       ENFILE ã·ã¹ãã å¨ä½ã§ãªã¼ãã³ããã¦ãããã¡ã¤ã«ã®ç·æ°ãä¸éã«éããã

       ENODEV æå®ããããã¡ã¤ã«ãç½®ããã¦ãããã¡ã¤ã«ã·ã¹ãã ãã¡ã¢ãªãããã³ã°ããµãã¼ã
              ãã¦ããªãã

       ENOMEM ã¡ã¢ãªã«ç©ºãããªããã¾ãã¯å¦çä¸ã®ããã»ã¹ã®ãããã³ã°æ°ãæ大æ°ãè¶éããã

       EPERM  prot å¼ãæ°ã¯ PROT_EXEC ãè¡ãããã«æå®ããã¦ãããã no-exec
              ã§ãã¦ã³ãããããã¡ã¤ã«ã·ã¹ãã ä¸ã®ãã¡ã¤ã«ã« ãããé åã対å¿ãã¦ããã

       ETXTBSY
              MAP_DENYWRITE ãã»ããããã¦ããã fd
              ã§æå®ããã¦ãããªãã¸ã§ã¯ãã¯æ¸ãè¾¼ã¿ç¨ã«éããã¦ããã

       EOVERFLOW
              On 32-bit architecture together with the large file extension
              (i.e., using 64-bit off_t): the number of pages used for length
              plus number of pages used for offset would overflow unsigned
              long (32 bits).

       ãããé åãå©ç¨ããéã«ã以ä¸ã®ã·ã°ãã«ãçºçãããã¨ããã:

       SIGSEGV
              èªã¿è¾¼ã¿å°ç¨ã§ mmap ãããé åã¸æ¸ãè¾¼ã¿ãè¡ããã¨ããã

       SIGBUS ãããã¡ã®ãã¡ããã¡ã¤ã«ã«é¢é£ã¥ãããã¦ããªãé¨å
              (ä¾ãã°ãã¡ã¤ã«æ«å°¾ãè¶ããé¨åãªã©ãããã«ã¯ ä»ã®ãã‐
              ã»ã¹ããã¡ã¤ã«ãåãè©°ããå ´åãªã©ãå«ã¾ãã)  ã«ã¢ã¯ã»ã¹ãããã¨ããã

æºæ
       SVr4, 4.4BSD, POSIX.1-2001.

å¯ç¨æ§
       mmap(), msync(2)  munmap()  ãå©ç¨å¯è½ãª POSIX ã·ã¹ãã ã§ã¯ã
       _POSIX_MAPPED_FILES 㯠<unistd.h> 㧠0 ãã大ããªå¤ã«å®ç¾©ããã (sysconf(3)
       ãåç§ã®ãã¨)ã

注æ
       ãã®ãã¼ã¸ã§ã¯ glibc ã® mmap() ã®ã©ããã¼é¢æ°ãæä¾ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«
       ã¤ãã¦èª¬æãã¦ãããåãã¯ããã®é¢æ°ã¯åãååã®ã·ã¹ãã ã³ã¼ã«ãèµ·åãã¦ããã ã«ã¼ãã« 2.4
       以éããã®ã·ã¹ãã ã³ã¼ã«ã¯ mmap2(2) ã«åã£ã¦ä»£ããããç¾å¨ ã§ã¯ã glibc ã®
       mmap() ã®ã©ããã¼é¢æ°ã¯ offset ãé©åã«èª¿æ´ãã¦ãã mmap2(2) ãèµ·åããã

       (i386 ãªã©ã®) ããã¤ãã®ã¢ã¼ããã¯ãã£ã§ã¯ã PROT_WRITE ãã»ããããã¨ãæé»ã®ãã¡ã«
       PROT_READ ãã»ãããããã PROT_READ ãã»ããããéã«æé»ã®ãã¡ã« PROT_EXEC
       ãã»ããããããã©ããã¯ãã¢ã¼ããã¯ãã£ä¾åã§ããã 移æ¤æ§ãèæ®ããããã°ã©ã ã§ã¯ã
       æ°è¦ã«ãããããé åã§ã³ã¼ããå®è¡ãããå ´åã¯ã常㫠PROT_EXEC ãã»ãããã¹ãã§ããã

       ãããã³ã°ãä½æãã移æ¤æ§ã®ããæ¹æ³ã¯ã addr ã« 0 (NULL) ãæå®ãã flags ãã
       MAP_FIXED ãå¤ããã¨ã§ããã ãã®å ´åãã·ã¹ãã ããããã³ã°ç¨ã®ã¢ãã¬ã¹ã®é¸æãè¡ãã
       ã¢ãã¬ã¹ã¯æ¢åã®ãããã³ã°ã¨è¡çªããªãããã«ã ã㤠0 ã«ãªããªãããã«é¸æãããã
       MAP_FIXED ãã©ã°ãæå®ãããã㤠addr ã 0 (NULL) ã®å ´åã«ã¯ãããããããã¢ãã¬ã¹ã 0
       (NULL) ã«ãªãã

       Certain flags constants are defined only if either _BSD_SOURCE or
       _SVID_SOURCE is defined.  (Requiring _GNU_SOURCE also suffices, and
       requiring that macro specifically would have been more logical, since
       these flags are all Linux specific.)  The relevant flags are:
       MAP_32BIT, MAP_ANONYMOUS (and the synonym MAP_ANON), MAP_DENYWRITE,
       MAP_EXECUTABLE, MAP_FILE, MAP_GROWSDOWN, MAP_HUGETLB, MAP_LOCKED,
       MAP_NONBLOCK, MAP_NORESERVE, MAP_POPULATE, and MAP_STACK.

ãã°
       Linux ã«ããã¦ã¯ãä¸è¨ã® MAP_NORESERVE ã§è¿°ã¹ããã¦ãããããªä¿è¨¼ã¯ãªãã
       ããã©ã«ãã§ã¯ãã·ã¹ãã ãã¡ã¢ãªã使ãåã£ãå ´åã«ã¯ã ã©ã®ãã‐
       ã»ã¹ããã¤å¼·å¶çµäºããããåãããªãããã§ããã

       2.6.7 ããåã®ã«ã¼ãã«ã§ã¯ã prot ã« PROT_NONE ãæå®ãããå ´åã«ã®ã¿ã
       MAP_POPULATE ãã©ã°ãå¹åãæã¤ã

       SUSv3 ã§ã¯ã length ã 0 ã®å ´åã mmap()
       ã¯å¤±æããã¨è¦å®ããã¦ãããããããªããã2.6.12 ããåã®ã«ã¼ãã«ã§ã¯ã ãã®å ´åã« mmap()
       ã¯æåãã¦ãã (ãããã³ã°ã¯ä½æãããã addr ãè¿ããã¦ãã)ã ã«ã¼ãã« 2.6.12 以éã§ã¯ã
       mmap()  ã¯ã¨ã©ã¼ EINVAL ã§å¤±æããã

       POSIX specifies that the system shall always zero fill any partial page
       at the end of the object and that system will never write any
       modification of the object beyond its end.  On Linux, when you write
       data to such partial page after the end of the object, the data stays
       in the page cache even after the file is closed and unmapped and even
       though the data is never written to the file itself, subsequent
       mappings may see the modified content.  In some cases, this could be
       fixed by calling msync(2)  before the unmap takes place; however, this
       doesn't work on tmpfs (for example, when using POSIX shared memory
       interface documented in shm_overview(7)).

ä¾
       以ä¸ã®ããã°ã©ã ã¯ãä¸çªç®ã®ã³ãã³ãã©ã¤ã³å¼ãæ°ã§æå®ããã
       ãã¡ã¤ã«ã®ä¸é¨ãæ¨æºåºåã«è¡¨ç¤ºããã
       表示ããç¯å²ã¯ãäºçªç®ãä¸çªç®ã®ã³ãã³ãã©ã¤ã³å¼ãæ°ã§æ¸¡ããã
       ãªãã»ããã¨é·ãã§æå®ãããã ãã®ãã‐
       ã°ã©ã ã¯ãæå®ããããã¡ã¤ã«ã®å¿è¦ãªãã¼ã¸ã®ã¡ã¢ãªã» ãããã³ã°ãä½æãã write(2)
       ã使ã£ã¦ææã®ãã¤ããåºåããã

       #include <sys/mman.h>
       #include <sys/stat.h>
       #include <fcntl.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>

       #define handle_error(msg) \
           do { perror(msg); exit(EXIT_FAILURE); } while (0)

       int
       main(int argc, char *argv[])
       {
           char *addr;
           int fd;
           struct stat sb;
           off_t offset, pa_offset;
           size_t length;
           ssize_t s;

           if (argc < 3 || argc > 4) {
               fprintf(stderr, "%s file offset [length]\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           fd = open(argv[1], O_RDONLY);
           if (fd == -1)
               handle_error("open");

           if (fstat(fd, &sb) == -1)           /* To obtain file size */
               handle_error("fstat");

           offset = atoi(argv[2]);
           pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
               /* offset for mmap() must be page aligned */

           if (offset >= sb.st_size) {
               fprintf(stderr, "offset is past end of file\n");
               exit(EXIT_FAILURE);
           }

           if (argc == 4) {
               length = atoi(argv[3]);
               if (offset + length > sb.st_size)
                   length = sb.st_size - offset;
                       /* Can't display bytes past end of file */

           } else {    /* No length arg ==> display to end of file */
               length = sb.st_size - offset;
           }

           addr = mmap(NULL, length + offset - pa_offset, PROT_READ,
                       MAP_PRIVATE, fd, pa_offset);
           if (addr == MAP_FAILED)
               handle_error("mmap");

           s = write(STDOUT_FILENO, addr + offset - pa_offset, length);
           if (s != length) {
               if (s == -1)
                   handle_error("write");

               fprintf(stderr, "partial write");
               exit(EXIT_FAILURE);
           }

           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       getpagesize(2), mincore(2), mlock(2), mmap2(2), mprotect(2), mremap(2),
       msync(2), remap_file_pages(2), setrlimit(2), shmat(2), shm_open(3),
       shm_overview(7)

       The descriptions of the following files in proc(5): /proc/[pid]/maps,
       /proc/[pid]/map_files, and /proc/[pid]/smaps.

       B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128-129 and 389-391.

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2013-04-17                           MMAP(2)