Linux concept-process address space

<1> Process address space

For C language learning, we are not unfamiliar with this picture.
Insert picture description here
We write a piece of C language code to verify the accuracy of the above picture.

#include<stdio.h>
#include<stdlib.h>

int g_val = 100;
int g_unval;

int main(int argc, char* argv[], char* env[])
{
    
    
  //环境变量
  printf("env addr:%p\n", env[0]);
  printf("env addr:%p\n",env[1]);
  //命令行参数
  printf("argv addr:%p\n",argv[0]);
  printf("argv addr:%p\n",argv[argc-1]);
  char* mem = (char *)malloc(sizeof(char)*4);

  //栈
  printf("Stack addr:%p\n",&mem);
  //堆
  printf("Heap addr:%p\n",&mem);
  //未初始化数据
  printf("uninit addr:%p\n",&g_unval);
  //初始化数据
  printf("init addr:%p\n",&g_val);
  //正文代码
  printf("code addr:%p\n",&main);

  return 0;
}

result

Insert picture description here
It can be seen that all the addresses printed out for the variables defined in the program correspond one to one.

Q1: What is the address space?

Address space is a virtual representation of physical memory, which is divided into different areas in the form of structures in the Linux kernel.

<2> Virtual address space

Since we mentioned that the process space is a virtual representation, we run the following code to verify the process address space

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int g_val = 0;
int main()
{
    
    
  pid_t id = fork();
  if(id < 0)
  {
    
    
    perror("fork");
    return 0;

  }
  else if(id == 0)
  {
    
     //child 
     printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val);
  }
  else
  {
    
     //parent
    printf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val);
  }
  sleep(1);
  return 0;
}

Insert picture description here
It can be seen from the result that the global variable address of the parent process is the same as the global variable address of the child process, which corresponds to the picture of the process space address, so we can modify it slightly

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int g_val = 0;
int main()
{
    
    
  pid_t id = fork();
  if(id < 0)
  {
    
    
    perror("fork");
    return 0;

  }
  else if(id == 0)
  {
    
     //child ,子进程肯定先跑完,也就是子进程先修改,完成之后,父进程再读取
    g_val = 100;
    printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val);
  }
  else
  {
    
     //parent
    sleep(3);
    printf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val);
  }
  sleep(1);
  return 0;
}

Insert picture description here
We found that the output addresses of the parent and child processes are the same, but the contents of the variables are different !

Can draw the following conclusions:

  • The contents of the variables are different, so the variables output by the parent and child processes are definitely not the same variable
  • But the address value is the same, indicating that the address is definitely not a physical address !
  • Under Linux addresses, such addresses are called virtual addresses .
  • The addresses we see in C/C++ are all virtual addresses ! The physical address is invisible to the user and managed by the OS

Q2: Why is there an address space?

  1. If there is no address space, then the addresses accessed by the process are all physical addresses. When the pointer of one process reads and writes the address of another process, the phenomenon of wild pointers occurs, which leads to the destruction of other people's space, thus violating the independence of the process. in principle.
  2. The location where the process data is stored is not continuous, so the access is more difficult, and the probability of abnormal out-of-bounds is also increased.

Q3: How does the address space work?

  1. Since the address space is virtual, the OS must be responsible for converting the virtual address into a physical address, and a mapping relationship needs to be established in the middle of the page table for conversion.
  2. By the operating system to access the physical memory, the process of artificial interference will be prohibited, and the heap memory plays a protective role.

<3> Page table

Insert picture description here
The above picture is enough to talk about the name problem. The same variable with the same address is actually the same virtual address, but the content is different because it is mapped to different physical addresses!

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/114544984