C# Application Execution Process

C# Application execution process?

Execution contain Four phrases:

Phrase 1: Need to choose compiler.

Phrase 2: Compiler compiles the Source code into MSIL

Phrase 3: Compile MSIL to Native Code

Phrase 4: Running the native code.

Phrase 1 (Need to choose compiler)  :

Choosing compiler is nothing but when we select a language like Visual Basic, C#, Visual C++, F#  etc that

compiler will compile the application automatically.

Ex: Here we choose Language as C#

Phrase 2(Compiler compiles the Source code into MSIL) :

From the our C# source code taken by the CSC (c# compiler) which will generate MSIL.

MSIL(Microsoft intermediate language) :

Its contain CPU independent set of instructions.

Its contain instructions from initializing to exit.

Public static void Main()

        {

            int a =1, b=3;

            Console.WriteLine(a + b);

            Console.ReadLine();

        }

For above we can see the below MSIL code which is contain instructions from initializing to exit.

.method public hidebysig static void  Main() cil managed

{

  .entrypoint

  // Code size       21 (0x15)

  .maxstack  2

  .locals init ([0] int32 a,

           [1] int32 b)

  IL_0000:  nop

  IL_0001:  ldc.i4.1

  IL_0002:  stloc.0

  IL_0003:  ldc.i4.3

  IL_0004:  stloc.1

  IL_0005:  ldloc.0

  IL_0006:  ldloc.1

  IL_0007:  add

  IL_0008:  call       void [mscorlib]System.Console::WriteLine(int32)

  IL_000d:  nop

  IL_000e:  call       string [mscorlib]System.Console::ReadLine()

  IL_0013:  pop

  IL_0014:  ret

} // end of method MyClassForDelegates::Main

Here Along with the MSIL compiler generate the metadata. Metadata contain info about types and

references. This metadata will be useful at runtime.

The PE contain the info necessary for the Windows OS loader to manage the wrapped executable code

Both MSIL and Metadata contained in PE File.

Phrase 3(Compile MSIL to Native Code):

Before run the code we need to compile MSIL code into Native code which is done by CLR.  Native code

is generated for the target machine architecture.

This can be performed in two ways

1) JIT (just-in-time) compiler

2) Ngen.exe (Native Image Generator)


1. JIT (just-in-time) compiler:


JIT compilation converts the MSIL into Native code on demand at application runtime because some

code might not called at runtime.

The loader creates and attaches a stub to each method.

When method is called first time , The stub passes control to JIT which converts MSIL to native code and

modified stub point directly to the generated Native code. Subsequent method calls Native code stub

directly.

2. Ngen.exe (Native Image Generator):

Because JIT compilation performed at run time depend method call its hurts the performance.

Another Important thing is code generated by the JIT compiler is bound to the process that triggered the

compilation. It cannot be shared across multiple processes.

Due to above CLR supports ahead-of-time compilation mode.

This ahead-of-time compilation mode uses the Ngen.exe.

Ngen.exe perform conversion before running the application. Its convert entire assembly generated

code in the Native Image Cache as a file on disk its available to all process. So its definitely increases the

performance.

Phrase 4(Running the native code):

CLR provide the infrastructure to run the code and services are used while running the code.

Services which are used are garbage collection, security, interoperability with unmanaged code, cross-

language debugging support, and enhanced deployment and versioning support.

No comments:

Post a Comment