Summary of self-accomplishment of "Reading notes" programmer (vi)

Source: Internet
Author: User

Summary of self-accomplishment of "Reading notes" programmer (vi)

Disclaimer: Reference Please specify source http://blog.csdn.net/lg1259156776/

Description: This is the self-cultivation of the programmer a book summary, with the progress of reading, gradually increase the content.
This article mainly introduces the loading and process of executable files.

the difference between a program and a process

The program is static and refers to a file of pre-compiled instructions and data sets, and the process is actually a running program, which is dynamic.

Virtual address space

After the program runs, it will have a separate virtual address space, which is determined by the computer's hardware platform, specifically by the number of CPU bits. The hardware determines the maximum theoretical limit of the address space, that is, the size of the hardware addressing space, such as the 32bits CPU, its addressing capacity only 0?( 2 ?1) , corresponding to 4GB of virtual space, such as the Win7 system 32-bit CPU, the expansion of the memory bar can only support up to 4GB, more can not access. And now the general hardware platform is 64bits, its extensible addressing space is almost unlimited.

From the point of view of the program, you can calculate the size of the virtual address space through the space occupied by the C language Program pointer, in general, the C language pointer size is the same as the number of virtual space, the 32bits platform pointer is 32 bits, 4 bytes; The pointer under the 64bits platform is 64 bits, 8 bytes. But now in my development most still choose Win32 platform, this is no difference.

Win32 under Pointer test:

#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    void * pTest = NULL;    printf("on win32 platform the pointer has %d bytes!\n", sizeof(pTest));    return 0;}output:on win32 platform the pointer has 4 bytes!

x64 platform under pointer test:

#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    void * pTest = NULL;    printf("on x64 platform the pointer has %d bytes!\n", sizeof(pTest));    return 0;}output:on x64 platform the pointer has 8 bytes!

The following are mainly used in 32bits hardware platform to explain:
The Linux operating system reserves the high 1GB space of the virtual address space as the operating system space by default, leaving the remaining 3GB space to be used by the process.

Way of loading

When the program executes, the required instructions and data must be in memory to function properly, but in many cases the amount of memory required by the program is greater than the amount of physical memory, and the underlying solution of course is to increase the memory at this point. Memory is expensive and rare relative to disk, so it is desirable to allow more programs to run and use memory as efficiently as possible without adding memory. According to the local principle of the program runtime, usually the most common part of the program resides in memory, and some infrequently used data is stored in the disk, which is the basic principle of dynamic loading.

Overlay loading and page mapping are two typical dynamic loading methods, in principle, using the program's local principle, the idea of dynamic loading is the program to use which piece of the block will be loaded into memory, if not to temporarily mount, stored on the disk.

Overwrite loading

Programmers must manually divide the program into chunks when writing programs, and then write small helper code to manage when the modules should reside in memory and when they should be replaced. The programmer organizes the modules into a tree-like structure according to the invocation dependency between them. From any module in the tree structure to the root (main) module of the tree is called the call path, when the module is called, the entire call path of the module must be in memory. Prohibit calls across trees.

Page Mappings

Page mapping is part of the virtual storage mechanism, which divides the memory and all the data and instructions on the disk into pages, and the objects that are loaded and manipulated are pages, and the hardware specifies page sizes 4096, 8192, 2MB, 4MB, and so on.

loading executable files from the operating system the establishment of the process

The most critical feature of a process is having a separate virtual address space, creating a process, and then loading the appropriate executable file and executing it with the following three things in case of virtual storage:
1. Create a separate virtual address space
A virtual space is a set of page mapping functions that map the individual pages of a virtual space to the appropriate physical space, and creating virtual space is not actually creating space, but rather creating the corresponding data structures needed to map functions.
2. Read the executable header to establish a mapping between the virtual space and the executable file
The previous step is to map the virtual space to the physical space, which is the mapping between the virtual space and the executable file. When the program executes a page fault, the operating system allocates a physical page from the physical memory, reads it from the disk into memory, and then sets the mapping of the page and the physical page of the pages, so that the program runs correctly.
3. Set the CPU instruction register to the entry address of the executable file, start the Run
The operating system transfers control to the process by setting the CPU's instruction register, and the process begins execution.

Page Error

After the above steps have been performed, the actual instructions and data of the executable are not loaded into memory, and the operating system simply establishes the mapping between the executable file and the process virtual storage through the executable header information. Suppose the program entry address is 0x08048000, which is exactly the starting address of the. Text segment. When the CPU intends to execute the instruction of this address, the Discovery page 0x08048000~0x08049000 is an empty page, which is considered a page fault, page fault. The CPU gives control to the operating system, the operating system's dedicated page error handling routines handle this situation, allocates a physical page in physical memory, establishes a mapping between the virtual address in the process and the allocated physical page, and then returns control back to the process, which restarts execution from the location just before the page was wrong.
As the process executes and errors continue to occur, the operating system assigns the appropriate physical pages to the process to meet the needs of the process execution. Of course, it is also possible that the process requires more memory than is available, especially when multiple processes are executing at the same time, the operating system needs to carefully organize and allocate physical memory, or even temporarily reclaim the physical memory allocated to the process, involving the operating system's virtual storage management.

2015-10-30 Reading Note Zhang Bongyi

Copyright NOTICE: This article for Bo Master original article, reprint please indicate source http://blog.csdn.net/lg1259156776/.

Summary of self-accomplishment of "Reading notes" programmer (vi)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.