Tag: article of public account


Memory for the user is an array of bytes. We can ask a byte, or some bytes, according to the address.

Memory from a long time ago

Once upon a time, there was only one program on a machine, and the operating system was stored as a function. As for memory, except for some space occupied by the operating system code and data, the rest of the space is allocated to the running program, so the graph looks like this:

Tip: We call a running program a process.

Memory for running more than one program at a time

Later, people decided that running only one program on a computer at the same time was too bad, so they devised a mechanism to run multiple programs at the same time. However, there is only one memory, so these user programs can only share the same memory, and can only divide different parts of memory among different user programs, so a graph looks like this:

There are some problems with this:

  • Different user programs can only use the portion of memory specified for them, that is, programmers should be careful to calculate whether they are using the memory of other people’s home when typing code, which is very unfriendly to the coder.

  • If someone tries to read the memory that someone else’s program is using, that’s a big reveal, or worse, if the bad guy updates some bytes of memory that someone else’s program is using

  • The memory space that can be used is provided well, too little have ~

Virtual memory

The operating system is a nice guy whose mission is to solve all the problems that annoy users. Design operating system mutombo think, if let the user directly to memory operation, users don’t know whether others use which bytes of memory, which can use his bytes, if to do this, people in the development program will get together for a meeting, the everyone rules which block of memory to use good, Even when specified, users have to be careful not to use parts of memory that do not belong to them. Let’s just, let’s not let the user manipulate memory directly, let’s just let the user program and think of memory as a very, very large array of bytes that they can play around with, and they call this very large array of bytes virtual memory, The operating system performs the mapping from the virtual address of virtual memory to the real address of real memory. Draw a picture that looks something like this:

Still have a problem so, the user is more and more, although each user uses very little memory space, the memory space that adds up to take up may exceed the size of real memory, what’s more, the program that some frenzical programmer writes itself used the space that exceeds real memory size, how can this do. It’s not hard for the guys who designed the operating system, who were smart enough to pull hard drives into the mix.

Operating system finished by the virtual memory addresses to real memory address or disk address mapping between the work, that provide users with the virtual memory address space can be very, very big, users do not have all the long memory space in the program can be above the operating system to get the disk storage, when needed, and from the disk loaded into real memory, Important is the process of all is that the operating system automatically, for code for these farmers, write a program like Fried chicken in a large place will be the byte array of data is moved to another place, or put something somewhere after the CPU data processing after put them somewhere in a byte array, the whole process so easy, You don’t have to worry about reading/overwriting memory that someone else is using.

Tip: For Windows operating systems, the disk space required for virtual memory is allocated from the system disk, usually drive C. When you have little free space on drive C, it can cause congestion

Virtual memory structure

Virtual memory is a very large array of bytes, but the array is also divided into parts that are not accessible to users everywhere. The structure of virtual memory looks like this:

When a user needs to call a function, a structure called a stack frame is generated, which causes the stack space in virtual memory to grow.

When the user needs to allocate memory space separately, it is allocated from the heap space, which causes the heap space in virtual memory to grow.

It is important to note that the operating system kernel is not a process, but the operating system code.

Unconsciously I feel more than a minute, see you next time ~

Tip: Make a prediction about the implementation of virtual memory, that is, how the operating system maps virtual memory addresses to real memory addresses and disk addresses.

digression

Writing articles is tiring, and sometimes you feel that the reading is smooth, but it is actually the result of countless revisions behind. If you think it is good, please help to forward it, thanks a million ~ here is my public account “we are all small frog”, there are more technical dry goods, occasionally pull a calf, welcome to pay attention to: