简体   繁体   中英

passing -1 as file descriptor to mmap

I did an strace on the "ls" command in FC17 Linux.

Following was the output.

execve("/usr/bin/ls", ["ls"], [/* 48 vars */]) = 0
brk(0)                                  = 0x27c1000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc765fa6000
...

I am not getting the purpose and result of passing -1 as the file descriptor to the mmap call, can some one shed some light on this ?

There are two kinds of mappings (areas of virtual memory mapped to a process): file-backed mappings, and anonymous (non file-backed) mappings. There are two ways to request an anonymous mapping:

  • (BSD) Pass MAP_ANONYMOUS (formerly MAP_ANON ) to mmap() . There is no associated file, so you should pass -1 as file parameter. Some OSes ignore the file parameter, others require it to be -1 (BSD IIRC).
  • (Sys V) Map /dev/zero . In this case, file is obviously meaningful.

See mmap(2) . This, along with the MAP_ANONYMOUS flag, allocates shared memory. It's an alternative to SysV-style shared memory ( shmctl ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM