11

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 ?

Amit Vikram
  • 394
  • 2
  • 15
user28264
  • 249
  • 2
  • 9

2 Answers2

8

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.
ninjalj
  • 42,493
  • 9
  • 106
  • 148
4

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

pilona
  • 320
  • 2
  • 8