进程虚拟地址空间划分

*任何编程语言,产生:1. 指令;2. 数据

进程的虚拟地址空间

image-20250107132558424

  1. 上面×处,不可访问, image-20250107131215417

​ nullptr 空指针(0地址)不可访问

  1. .text .rodata(read only) 指令放在这,叫代码段

    image-20250107131606959

​ 这是错误的,*p在代码段,read only不能修改

​ 现在有的编译器已经不允许指针直接指向常量字符串

  1. .data 存放初始化且初始化不为0
  1. .bss 0与未初始化
  1. .heap new,malloc在这分配内存 从上往下
  1. 加载共享库 *.dll(win) .so(linux)
  1. stack 从下往上
  1. 命令行参数和环境变量
  1. ZONE_DMA

    ZONE_NORMAL

    ZONE_HIGHMEN

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include "pch.h"
#include <iostream>
using namespace std;

int gdata1 = 10; //.data
int gdata2 = 0; //.bss
int gdata3; //.bss

static int gdata4=11 //.data
static int gdata5=0 //.bss
static int gdata6 //.bss


int main()
{
int a = 12; //这里不是数据,而是指令
int b = 0; //本质为MOV DWORD PTR[a],0CH,指令运行时,在stack上开辟4B存放12这个和数据
int c;

static int e = 13; //静态局部变量,放在数据段,但不初始化,知道程序第一次运行到他们才开始初始化,因此e在.data,fg在.bss
static int f = 0;
static int g;
cout<<c<<g<<endl; //c为无效值,g为0
}

进程内核空间共享

**每一个进程的用户空间是私有的,但内核空间是共享的

image-20250107134011285

因此,进程之间的通信:匿名管道通信