0

I'm studying Linux programming, and I have a question about mmap.

I'd like to look more detail at a process, by using strace. i've done with it, and I saw a system call called mmap. But I couldn't see any flag or other clue whether it was mapped memory for read, for write.

Is there any way to distinguish read or write in mmap?

I'm using Ubuntu 14.04.

thanks sincerely

youngsun
  • 63
  • 7
  • Strace doesn't show you something like `mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0)` ? For read-only you'll see `mmap` with `PROT_READ` – Severin Pappadeux Sep 10 '15 at 14:46

1 Answers1

2

In Ubuntu 15.04, if I ran

strace -f echo "QQQ"

I'll get output

mmap(0x7f28bc458000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f28bc458000
mmap(0x7f28bc45e000, 16128, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f28bc45e000
...
mmap(NULL, 2919792, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f28bbdcf000

PROT_READ - pages may be read

PROT_WRITE - pages may be written

http://man7.org/linux/man-pages/man2/mmap.2.html

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • but it is about protection. doesn't it ? – youngsun Sep 10 '15 at 17:25
  • @user3293627 it is exactly what you want, I believe. Mapping is done on per-page basis, and each page is marked as read, read-write, or other flags. You're writing into this memory using say byte addressable array, but it page is marked as PROT_WRITE, it will be ok – Severin Pappadeux Sep 10 '15 at 18:53
  • and i'd like to know. mmaped memory will be written to disk. is this case also distinguished with that prot ? it's because. nowadays a lots of programs use mmap instead of write syscall. all i want to know is which mmap in strace is about file wrtie. sorry i was kind of vague. is it possible ? thanks again – youngsun Sep 11 '15 at 01:34