Linux檔案映射的檢視方法有哪些

PHPz
發布: 2024-02-19 17:24:03
轉載
1045 人瀏覽過

一.檔案映射的定義

#檔案映射是一種將磁碟上的檔案映射到進程的虛擬記憶體空間的機制。

透過這種方式,進程可以直接透過記憶體位址來讀寫文件,而不必透過常規的 read 和 write 等系統呼叫。

在檔案映射中,我們透過Linux的機制將檔案和進程的虛擬記憶體連結起來,使得進程可以直接在記憶體中讀寫檔案數據,而無需直接存取磁碟。這種機制提供了更有效率的文件存取方式,同時也簡化了文件操作的流程。

檔案映射通常被稱為記憶體映射,這兩者通常是一樣的,記憶體映射涵蓋了將檔案映射到記憶體和將匿名記憶體映射到進程位址空間的操作。

檔案映射是記憶體映射的一種特例。

二.檔案映射的檢視方法【兩種方法】

方法一:使用pmap工具

#查看對應進程的檔案映射資訊

$ pmap -X 12345#查看指定PID的文件映射信息
12345: ./example
0000555555554000100K r-x-- example
00005555556730004K r---- example
00005555556740004K rw--- example
00007ffff7de0000 1360K r-x-- libc-2.31.so
...
mapped: 1448Kwriteable/private: 8Kshared: 0K
登入後複製
  • 每行代表一個記憶體映射區域。
  • 位址範圍、權限、對應類型、檔案路徑等資訊。
  • “mapped” 表示映射的總大小,”writeable/private” 表示可寫入和私有的大小,”shared” 表示共享的大小。

方法二:cat檢視檔案對應檔案

#使用 cat /proc/PID/maps 指令可以查看進程的記憶體對映情況。

每一行都表示一個記憶體映射區域,格式如下:

address perms offsetdev inodepathname
00400000-0040b000 r-xp 00000000 08:01 1167685/usr/bin/cat
0060a000-0060b000 r--p 0000a000 08:01 1167685/usr/bin/cat
0060b000-0060c000 rw-p 0000b000 08:01 1167685/usr/bin/cat
登入後複製
  • address: 映射的虛擬記憶體位址範圍。
  • perms: 權限,包括讀取(r)、寫入(w)、執行(x)等。
  • offset: 檔案中映射區域的偏移量。
  • dev: 設備號。
  • inode: 檔案在檔案系統中的節點號。
  • pathname: 對應的檔案路徑或匿名對應。

三.如何在Linux下使用檔案映射

#現在我們透過一個範例示範如何使用檔案映射將檔案映射到記憶體中,然後透過修改記憶體中的內容,最後透過解除記憶體映射來進行示範。

Linux檔案映射的檢視方法有哪些

#example.c檔

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main() {
const char *file_path = "example.txt";
const size_t file_size = 4096;

int fd = open(file_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

if (ftruncate(fd, file_size) == -1) {
perror("ftruncate");
close(fd);
exit(EXIT_FAILURE);
}

// Create a memory-mapped region
void *addr = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
perror("mmap");
close(fd);
exit(EXIT_FAILURE);
}

// Now 'addr' points to the beginning of the file in memory
// 现在addr表示文件在进程的内存空间代表区域的起始位置
// Write a message to the memory-mapped file
// 向映射文件写入一句消息。
const char *message = "Hello, Memory Mapping!n";
strncpy(addr, message, strlen(message));

printf("Press Enter to exit...n");
getchar();// Wait for user to press Enter

// Unmap the memory region解除文件和内存区域的映射关系
if (munmap(addr, file_size) == -1) {
perror("munmap");
close(fd);
exit(EXIT_FAILURE);
}

// Close the file descriptor
close(fd);

return 0;
}
登入後複製

編譯執行:

$ ls
example.c
$ gcc example.c -o example
$ ./example
Press Enter to exit...
登入後複製

查看進程的檔案映射資訊:

$ ps aux|grep example
codersong 15245420.00.0 27761152 pts/0S+ 19:23 0:00 ./example
codersong 15245470.00.0121882432 pts/2S+ 19:23 0:00 grep --color=auto example
$ pmap -X 1524542
1524542: ./example
地址 Perm 偏移量 设备 Inode SizeRss Pss Pss_Dirty Referenced Anonymous LazyFree ShmemPmdMapped FilePmdMapped Shared_Hugetlb Private_Hugetlb Swap SwapPss Locked THPeligible Mapping
557b482c3000 r--p0000000008:03 712405144 4 04 000 00 00 00 0 example
557b482c4000 r-xp0000100008:03 712405144 4 04 000 00 00 00 0 example
557b482c5000 r--p0000200008:03 712405144 4 04 000 00 00 00 0 example
557b482c6000 r--p0000200008:03 712405144 4 44 400 00 00 00 0 example
557b482c7000 rw-p0000300008:03 712405144 4 44 400 00 00 00 0 example
557b48e9e000 rw-p0000000000:00 01324 4 44 400 00 00 00 0 [heap]
7f8fe5600000 r--p0000000008:03264612160160 7 0160 000 00 00 00 0 libc.so.6
7f8fe5628000 r-xp0002800008:03264612 162078824 0788 000 00 00 00 0 libc.so.6
7f8fe57bd000 r--p001bd00008:03264612352 64 1 0 64 000 00 00 00 0 libc.so.6
7f8fe5815000 r--p0021400008:03264612 16 161616 161600 00 00 00 0 libc.so.6
7f8fe5819000 rw-p0021800008:0326461288 8 88 800 00 00 00 0 libc.so.6
7f8fe581b000 rw-p0000000000:00 0 52 202020 202000 00 00 00 0
7f8fe58f6000 rw-p0000000000:00 0 128 8 88 800 00 00 00 0
7f8fe5908000 rw-p0000000000:00 084 4 44 400 00 00 00 0
7f8fe590a000 r--p0000000008:0326460088 0 08 000 00 00 00 0 ld-linux-x86-64.so.2
7f8fe590c000 r-xp0000200008:03264600168168 7 0168 000 00 00 00 0 ld-linux-x86-64.so.2
7f8fe5936000 r--p0002c00008:03264600 44 40 1 0 40 000 00 00 00 0 ld-linux-x86-64.so.2
7f8fe5941000 rw-s0000000008:03 712405244 4 04 000 00 00 00 0 example.txt
7f8fe5942000 r--p0003700008:0326460088 8 88 800 00 00 00 0 ld-linux-x86-64.so.2
7f8fe5944000 rw-p0003900008:0326460088 8 88 800 00 00 00 0 ld-linux-x86-64.so.2
7ffef93f2000 rw-p0000000000:00 0132 121212 121200 00 00 00 0 [stack]
7ffef9485000 r--p0000000000:00 0 160 0 00 000 00 00 00 0 [vvar]
7ffef9489000 r-xp0000000000:00 084 0 04 000 00 00 00 0 [vdso]
ffffffffff600000 --xp0000000000:00 040 0 00 000 00 00 00 0 [vsyscall]
 ==== ==== === ========= ========== ========= ======== ============== ============= ============== =============== ==== ======= ====== ===========
 2780 1344 15296 13449600 00 00 00 0 KB
$
$
$ cat /proc/1524542/maps
557b482c3000-557b482c4000 r--p 00000000 08:03 7124051/home/codersong/zhengshihong/example
557b482c4000-557b482c5000 r-xp 00001000 08:03 7124051/home/codersong/zhengshihong/example
557b482c5000-557b482c6000 r--p 00002000 08:03 7124051/home/codersong/zhengshihong/example
557b482c6000-557b482c7000 r--p 00002000 08:03 7124051/home/codersong/zhengshihong/example
557b482c7000-557b482c8000 rw-p 00003000 08:03 7124051/home/codersong/zhengshihong/example
557b48e9e000-557b48ebf000 rw-p 00000000 00:00 0[heap]
7f8fe5600000-7f8fe5628000 r--p 00000000 08:03 264612 /usr/lib/x86_64-linux-gnu/libc.so.6
7f8fe5628000-7f8fe57bd000 r-xp 00028000 08:03 264612 /usr/lib/x86_64-linux-gnu/libc.so.6
7f8fe57bd000-7f8fe5815000 r--p 001bd000 08:03 264612 /usr/lib/x86_64-linux-gnu/libc.so.6
7f8fe5815000-7f8fe5819000 r--p 00214000 08:03 264612 /usr/lib/x86_64-linux-gnu/libc.so.6
7f8fe5819000-7f8fe581b000 rw-p 00218000 08:03 264612 /usr/lib/x86_64-linux-gnu/libc.so.6
7f8fe581b000-7f8fe5828000 rw-p 00000000 00:00 0
7f8fe58f6000-7f8fe58f9000 rw-p 00000000 00:00 0
7f8fe5908000-7f8fe590a000 rw-p 00000000 00:00 0
7f8fe590a000-7f8fe590c000 r--p 00000000 08:03 264600 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f8fe590c000-7f8fe5936000 r-xp 00002000 08:03 264600 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f8fe5936000-7f8fe5941000 r--p 0002c000 08:03 264600 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f8fe5941000-7f8fe5942000 rw-s 00000000 08:03 7124052/home/byzoro/zhengshihong/example.txt
7f8fe5942000-7f8fe5944000 r--p 00037000 08:03 264600 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f8fe5944000-7f8fe5946000 rw-p 00039000 08:03 264600 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7ffef93f2000-7ffef9413000 rw-p 00000000 00:00 0[stack]
7ffef9485000-7ffef9489000 r--p 00000000 00:00 0[vvar]
7ffef9489000-7ffef948b000 r-xp 00000000 00:00 0[vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0[vsyscall]
登入後複製

現在按ctrl C退出example程序,查看example.txt檔案的內容:

$ cat example.txt
Hello, Memory Mapping!
登入後複製

以上是Linux檔案映射的檢視方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:mryunwei.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!