[C # advanced series] 01 CLR execution model-a Hello World story,

Source: Internet
Author: User
Tags hosting

[C # advanced series] 01 CLR execution model-a Hello World story,

Well, let's just talk about it. Let's take a look at the Hello World picture in the previous chapter:

We have a Hello world Program, which is so simple that I do not use Chinese Character programming o (> Objective <) o today, so everything is simple and clear.

Start:

Compile:

After a program is written, it must be compiled. In the past, all C and everything was the CPU command of the compilation cost machine, but our C # is not.

C #, VB. NET will compile themHosting module,The hosting module is in a standard portable PE file. (Those ignorant teenagers must be panic. What is this? It's a hosting module and PE file. Don't panic. All the terms on the tall lines that you don't understand are actually very simple. You don't need to understand them now. Listen to me)

The so-called PE file is a portable execution body. In short, it is a ghost such as. EXE and. DLL (the difference between this exe and C language generation ). You can use the hosting module in this PE file as an object. The object has four attributes, one PE Header (description file ), a clr header (describing the overall information of this object, such as the main entry), metadata (this is critical, A metadata table contains the descriptions of those types and Members defined in the source code, and a metadata table contains the referenced types and descriptions of members ), IL Code (the code after your source code is compiled, also known as the intermediate language ).

If you are in trouble, you can forget the first two attributes. Remember the following two attributes: Metadata and IL code (in short, the description of the Code and the compiled code ).

However, a PE file can be composed of multiple hosted modules. The compiler combines multiple managed modules and resource files into an Assembly that contains a list. This is the final PE file.

Run:

However, this managed module, that is, the PE file, cannot run directly. It needs CLR.

Common Language Runtime (CLR) is a Runtime environment similar to Java virtual machine. It is responsible for resource management (memory allocation and garbage collection ), ensure necessary separation between applications and underlying operating systems. CLR has two different translation names: the public Language Runtime Library and the public language runtime.

The description and subsequent descriptions are from Baidu encyclopedia.

Core CLR functions include memory management, assembly loading, security, exception handling, and thread synchronization.

This is also called the runtime of Public languages.

When I mentioned earlier that the file cannot be run directly, I am sure that the generated exe file can be run directly.

This is because. NET Framework is installed on your computer. When you open the exe program, the main thread of your process will call MSCorEE. dll method. This method initializes CLR, loads the exe assembly, and then calls the main function.

So in fact, your code needs to run on the CLR.

So how does CLR play with our Hello world?

It will compile our IL code at runtime. (Don't let me know why we need to compile it twice. I will talk about it later)

As early as the execution of the Main function, CLR will detect all types referenced by the Main code, and then generate an internal data structure to manage access to the reference type.

In this internal structure, each type, for example, each method on the Console, has an entry, and each entry has an address (address a here ), you can find the implementation code of the method at address.

During the initialization of the internal data structure, the addresses of all these entries will be set as a function called JITCompiler.

For example, the first sentence of the hello world code in the figure above, the CLR will run into the JITCompiler function when executing the Code. This is an internal operation:

What if we execute the Writeline function for the second time? In this case, the address of the Writeline function entry in the internal structure is already the address of the compiled CPU instruction, so the JITCompiler function will not be executed.

Do not think this will be very slow. Except for the time required for JITCompiler compilation during the first run, the local CPU command will be executed later. In addition, the JITCompiler compilation may generate some special commands dedicated to this CPU to optimize the IL code based on the CPU usage of your machine, some such hosting programs may be faster than unmanaged programs.

Well, the essence of this chapter is the above.

Now let's figure out why we need to compile it twice?

In this case, no matter whether C #, VB, and F # are used, you can generate the same managed Assembly containing IL code, and then the managed Assembly runs on the CLR, that is to say, you can write code in hybrid mode. a c # code can call the DLL of VB code and use the most appropriate language to do the most appropriate thing.

In addition, the IL code executed under CLR monitoring improves the robustness and reliability of the program because security verification is performed before execution.

Related Article

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.