您好,登錄后才能下訂單哦!
這篇文章主要介紹“Linux下的進(jìn)程虛擬內(nèi)存結(jié)構(gòu)是什么”,在日常操作中,相信很多人在Linux下的進(jìn)程虛擬內(nèi)存結(jié)構(gòu)是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Linux下的進(jìn)程虛擬內(nèi)存結(jié)構(gòu)是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
PostgreSQL使用進(jìn)程架構(gòu),每個(gè)連接對應(yīng)一個(gè)后臺進(jìn)程,為了更好的理解這種架構(gòu),有必要深入理解進(jìn)程的相關(guān)知識
進(jìn)程虛擬內(nèi)存的詳細(xì)結(jié)構(gòu)如下圖所示:
C樣例程序如下:
#include <stdlib.h> #include <stdio.h> #include <string.h> /** * f - print locations of various elements * * Returns: nothing */ void f(void)//子函數(shù) { int a; int b; int c; a = 98; b = 1024; c = a * b; printf("[f] a = %d, b = %d, c = a * b = %d\n", a, b, c); printf("[f] Adresses of a: %p, b = %p, c = %p\n", (void *)&a, (void *)&b, (void *)&c); } /** * main - print locations of various elements * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */ int main(int ac, char **av, char **env) { int a;//本地變量,位于棧中 void *p;//指針,在堆中分配內(nèi)存 int i; int size; printf("Address of a: %p\n", (void *)&a); p = malloc(98);//在堆中分配內(nèi)存 if (p == NULL) { fprintf(stderr, "Can't malloc\n"); return (EXIT_FAILURE); } printf("Allocated space in the heap: %p\n", p); printf("Address of function main: %p\n", (void *)main);//main函數(shù)所在的內(nèi)存地址 printf("First bytes of the main function:\n\t"); for (i = 0; i < 15; i++) { printf("%02x ", ((unsigned char *)main)[i]);//首16個(gè)字節(jié) } printf("\n"); printf("Address of the array of arguments: %p\n", (void *)av);//輸入?yún)?shù)數(shù)組地址 printf("Addresses of the arguments:\n\t");// for (i = 0; i < ac; i++) { printf("[%s]:%p ", av[i], av[i]);//打印輸入?yún)?shù) } printf("\n"); printf("Address of the array of environment variables: %p\n", (void *)env);//環(huán)境變量地址 printf("Address of the first environment variables:\n");//環(huán)境變量信息 for (i = 0; i < 3; i++) { printf("\t[%p]:\"%s\"\n", env[i], env[i]); } /* size of the env array */ i = 0; while (env[i] != NULL)//以NULL作為終結(jié) { i++; } i++; /* the NULL pointer */ size = i * sizeof(char *); printf("Size of the array env: %d elements -> %d bytes (0x%x)\n", i, size, size);//計(jì)算環(huán)境變量數(shù)組大小 f();//調(diào)用函數(shù) getchar();//接受輸入,進(jìn)程不退出 return (EXIT_SUCCESS); }
編譯并執(zhí)行
[root@localhost hacker]# gcc -Wall -Wextra -Werror main-7.c -o 7 [root@localhost hacker]# ./7 Hello Hacker Jack! Address of a: 0x7fff5b327bb8 --> 位于棧中 Allocated space in the heap: 0x9b6010 --> 位于堆中 Address of function main: 0x400729 --> 可執(zhí)行文件 First bytes of the main function: 55 48 89 e5 48 83 ec 40 89 7d dc 48 89 75 d0 Address of the array of arguments: 0x7fff5b327cb8 -->參數(shù)數(shù)組地址 Addresses of the arguments: [./7]:0x7fff5b329808 [Hello]:0x7fff5b32980c [Hacker]:0x7fff5b329812 [Jack!]:0x7fff5b329819 --> 輸入?yún)?shù) Address of the array of environment variables: 0x7fff5b327ce0 --> 環(huán)境變量數(shù)組 Address of the first environment variables: [0x7fff5b32981f]:"XDG_SESSION_ID=1" [0x7fff5b329830]:"HOSTNAME=localhost.localdomain" [0x7fff5b32984f]:"SELINUX_ROLE_REQUESTED=" Size of the array env: 27 elements -> 216 bytes (0xd8) [f] a = 98, b = 1024, c = a * b = 100352 [f] Adresses of a: 0x7fff5b327b7c, b = 0x7fff5b327b78, c = 0x7fff5b327b74 --> 函數(shù)f中的變量地址
通過udis86可反編譯,得到匯編代碼
[root@localhost install]# echo "55 48 89 e5 48 83 ec 10 48 8d 45 f0 48 89 c6" | udcli -64 -x -o 400729 0000000000400729 55 push rbp 000000000040072a 4889e5 mov rbp, rsp 000000000040072d 4883ec10 sub rsp, 0x10 0000000000400731 488d45f0 lea rax, [rbp-0x10] 0000000000400735 4889c6 mov rsi, rax [root@localhost install]#
查看該進(jìn)程的內(nèi)存映射信息
[root@localhost install]# ps aux | grep "./7" | grep -v grep root 6471 0.0 0.0 4348 348 pts/0 S+ 15:24 0:00 ./7 Hello Hacker Jack! [root@localhost install]# cat /proc/6471/maps 00400000-00401000 r-xp 00000000 fd:00 134287606 /data/source/hacker/7 00600000-00601000 r--p 00000000 fd:00 134287606 /data/source/hacker/7 00601000-00602000 rw-p 00001000 fd:00 134287606 /data/source/hacker/7 009b6000-009d7000 rw-p 00000000 00:00 0 [heap] 7f95b5d16000-7f95b5ed8000 r-xp 00000000 fd:00 151914 /usr/lib64/libc-2.17.so 7f95b5ed8000-7f95b60d8000 ---p 001c2000 fd:00 151914 /usr/lib64/libc-2.17.so 7f95b60d8000-7f95b60dc000 r--p 001c2000 fd:00 151914 /usr/lib64/libc-2.17.so 7f95b60dc000-7f95b60de000 rw-p 001c6000 fd:00 151914 /usr/lib64/libc-2.17.so 7f95b60de000-7f95b60e3000 rw-p 00000000 00:00 0 7f95b60e3000-7f95b6105000 r-xp 00000000 fd:00 151907 /usr/lib64/ld-2.17.so 7f95b62f8000-7f95b62fb000 rw-p 00000000 00:00 0 7f95b6301000-7f95b6304000 rw-p 00000000 00:00 0 7f95b6304000-7f95b6305000 r--p 00021000 fd:00 151907 /usr/lib64/ld-2.17.so 7f95b6305000-7f95b6306000 rw-p 00022000 fd:00 151907 /usr/lib64/ld-2.17.so 7f95b6306000-7f95b6307000 rw-p 00000000 00:00 0 7fff5b309000-7fff5b32a000 rw-p 00000000 00:00 0 [stack] 7fff5b3f1000-7fff5b3f3000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] [root@localhost install]#
到此,關(guān)于“Linux下的進(jìn)程虛擬內(nèi)存結(jié)構(gòu)是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。