# Are Kernel Codes (which are written in C) compiled during boot time?

I have a confusion regarding how the Kernel is loaded during the boot time. The intuition I have is like following:

During installation of an OS, the compiled Code of the Kernel (depending on the architecture of the system) is written to the part of the Primary Memory that is restricted to the OS. When the PC is turned on, during boot time, initial some of the Assembly Instructions are executed to start-up (like creating the process init and starting other services).

But how the other processes, services are created ? Are all codes are compiled to Assembly codes during installation or Kernel codes (written in C) are compiled during boot time? If the latter happens, then is GCC pre-installed to the OS address space?

Usually, everything is compiled in advance: the kernel and userland applications.

Roughly, the kernel is written in a special place on the disk (or other permanent memory), which the hardware will access at boot time, copy in RAM, and execute.

The kernel includes the disk driver and the filesystem driver, and will look for specific files on the disk, using them to load the first OS process (e.g. /bin/init, in unix-like OSs). This will in turn ask the OS to spawn new OS services/daemons, load new drivers, and finally start the user interface.

There is no need to have a compiler around during this. All the code which is loaded is machine code (already compiled).

The compiler is also not needed at installation time, since whoever produced the installation media (or network repository) wrote binary packages in it, containing compiled code.

MacOS does a very, very small portion of this: During boot time, it determines which exact processor model is used by your computer, and then for a very small number of important functions, processor dependent code is written to a reserved memory location.

This is code for the memcpy and memset functions (where you really want the fastest possible implementation, and that is processor dependent), for atomic operations, and very few others. Hopefully the standard library that you are using uses this code when needed.

Installing code depending on the processor used is risky - I would want to be able to remove the hard drive from one computer, put it into a different computer, and have it working. But if you install processor dependent code, then code for processor A might not work on processor B.