zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux - System Image and Process Image

Linux and system Image Process
2023-09-11 14:22:09 时间

System image (系统映像)

在计算机领域,系统映像是以某种非易失性形式(如文件)存储的计算机系统整个状态的序列化副本。如果一个系统能够被关闭,并在之后恢复到完全相同的状态,那么就可以说它能够使用系统映像。在这种情况下,系统映像可用于备份。

一个例子是许多操作系统的休眠功能。休眠使用整个机器的RAM作为一个映像。这时,所有RAM内存的状态被存储到磁盘上,之后计算机进入节能模式,然后再恢复到正常操作。

In computing, a system image is a serialized copy of the entire state of a computer system stored in some non-volatile form such as a file. A system is said to be capable of using system images if it can be shut down and later restored to exactly the same state. In such cases, system images can be used for backup.

An example would be the hibernate feature of many operating systems. Hibernation uses an image of the entire machine's RAM. Here, the state of all RAM memory is stored to disk, the computer is brought into an energy saving mode, then later restored to normal operation.

Process Image (进程映像)

进程映像是一个给定的进程在某个时间点的状态的副本。它经常被用来在一个不断变化的系统中生成一个某时刻的状态副本,并持久性保存下来。

一个比较常见的例子是数据库管理系统,DBMS。大多数DBMS可以在关闭前将其数据库的状态存储到一个文件中(见数据库转存)。然后,DBMS可以在数据库中的信息完好无损的情况下重新启动,并像软件从未停止过一样继续进行。

一些仿真器提供了一个保存被仿真系统映像的功能。在视频游戏中,这通常被称为savestate。

另一个用途是代码移动性:移动代理可以通过保存其状态在机器之间迁移,然后将数据复制到另一台机器并在那里重新启动。

我们使用core dump出来的是进程映像,其结构和系统映像是类似的。

A process image is a copy of a given process's state at a given point in time. It is often used to create persistence within an otherwise volatile system.

Most DBMS can store the state of its database or databases to a file before being closed down (see database dump). The DBMS can then be restarted later with the information in the database intact and proceed as though the software had never stopped.

Some emulators provide a facility to save an image of the system being emulated. In video gaming this is often referred to as a savestate.

Another use is code mobility: a mobile agent can migrate between machines by having its state saved, then copying the data to another machine and restarting there.

Although its purpose is different, a "system image" is often similar in structure to a core dump.

Process Image 详细说明

进程映像是执行程序时需要的一个可执行文件,这个是进程实时在使用的一个文件,而上面所说的映像指的是一种将此文件转存的情况。

该映像通常包含以下部分:

- 代码段或文本段 // Code segment or text segment

- 数据段  //  Data segment

- 堆栈段 // Stack segment

- 堆段    // Heap segment

下面是进程映像的图形表示:

Code segment

代码段是编译后得到的object(目标)文件的一部分,里面包含可执行指令,会放在程序的虚拟地址空间中。这通常是只读的数据段,有一个固定的大小。

Data segment

数据段分两种类型:

- 初始化的 (Initialized)

- 未初始化的 (Un-initialized)

初始化数据段是object文件中的一部分,里面包含的是带有初始值的静态或全局变量,也会放在程序的虚拟地址空间中。

未初始化的数据段是object文件中的一部分,里面包含的是没有初始化的静态或全局变量,也会放在程序的虚拟地址空间中。未初始化的数据段也被称为BSS(由符号开始的块, Block Started by Symbol)段。

数据段是可读可写的,因为变量的值在运行期间可以被改变。这个段也有一个固定的大小。

Stack segment

堆栈段是一个分配给自动变量和函数参数的内存区域。在执行函数调用时,它还存储了一个返回地址。堆栈使用LIFO(Last-In-First-Out)机制来存储本地或自动变量、函数参数和存储下一个地址或返回地址。返回地址指的是函数执行完成后返回的地址。这个段的大小是根据本地变量、函数参数和函数调用而变化的。这个段所占用空间的增长方向,是从较高的地址向较低的地址。

Heap segment

堆段是分配给动态内存存储的内存区域,例如用于malloc()和calloc()调用。这个段的大小也是根据用户分配而变化的。这个段从一个较低的地址增长到一个较高的地址。

现在让我们检查一下段(数据段和bss段)的大小在几个示例程序中是如何变化的。段的大小可以通过执行 "size "命令来了解。

File: segment_size1.c

#include<stdio.h>

int main() {

   printf("Hello World\n");

   return 0;

}

在下面的程序中,增加了一个未初始化的静态变量。这意味着未初始化段(BSS)的大小将增加4字节。注意 - 在Linux操作系统中,int的大小是4字节。整数数据类型的大小取决于编译器和操作系统的支持。

File: segment_size2.c

#include<stdio.h>

int main() {

   static int mystaticint1;

   printf("Hello World\n");

   return 0;

}

在下面的程序中,增加了一个初始化的静态变量。这意味着初始化段(DATA)的大小将增加4字节。

File: segment_size3.c

#include<stdio.h>

int main() {

   static int mystaticint1;

   static int mystaticint2 = 100;

   printf("Hello World\n");

   return 0;

}

在下面的程序中,增加了一个初始化的全局变量。这意味着初始化段(DATA)的大小将增加4字节。

File: segment_size4.c

#include<stdio.h>

int myglobalint1 = 500;

int main() {

   static int mystaticint1;

   static int mystaticint2 = 100;

   printf("Hello World\n");

   return 0;

}

在下面的程序中,增加了一个未初始化的全局变量。这意味着未初始化段(BSS)的大小将增加4字节。

File: segment_size5.c

#include<stdio.h>

int myglobalint1 = 500;

int myglobalint2;

int main() {

   static int mystaticint1;

   static int mystaticint2 = 100;

   printf("Hello World\n");

   return 0;

}

执行测试:

先编译:

$ gcc segment_size1.c -o segment_size1

$ gcc segment_size2.c -o segment_size2

$ gcc segment_size3.c -o segment_size3

$ gcc segment_size4.c -o segment_size4

$ gcc segment_size5.c -o segment_size5

输出结果:

$size segment_size1 segment_size2 segment_size3 segment_size4 segment_size5

   text  data  bss  dec  hex  filename

   878   252    8   1138 472  segment_size1

   878   252   12   1142 476  segment_size2

   878   256   12   1146 47a  segment_size3

   878   260   12   1150 47e  segment_size4

   878   260   16   1154 482  segment_size5

参考:

Process Image

https://en.wikipedia.org/wiki/System_image