Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing -1 as file descriptor to mmap

Tags:

linux

posix

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 ?

like image 387
user28264 Avatar asked Jun 10 '14 08:06

user28264


1 Answers

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.
like image 74
ninjalj Avatar answered Oct 16 '22 06:10

ninjalj