Thursday, November 2, 2017

Under the hood - what happens when a program is being run on a computer

A computer system is a bare minimum combination of - Software, Hardware and a bridge
Software: Applications and other programs
Hardware: CPU, Memory(RAM and ROM) and other hardware
Bridge: KERNAL

In present days, computer basically is a binary computer, well we are not yet into quantum-computing era. All the instructions fed to the processor consist of combinations of 1s and 0s. 1 and 0 are decoded to certain voltage levels in real world which will be passed to hardware via circuit.

Hence, at this point we say a processor is a microprocessor(an organized circuit of number of logical gates constructed with Transistors, Diodes etc) which is manufactured to be able to understand a set of instructions called "Instruction Set" specified by manufacturer.

Various processors(from different vendors Intel, AMD, Motorola, Apple etc) have their own instruction sets - x86, AMD64 etc)

Few sample instructions common across main stream sets below:
ADD, COMPARE, IN, JUMP, JUMP IF, LOAD, OUT and STORE etc. They are mnemonics which need to be fed in the form of binary digits.

Sample set of binary instructions or also called Machine Code:
ADD - 101001001001
SUBTRACT - 100000101001
COMPARE - 111100001101

Software: User application programs or System Programs
Any software program to be executed by a CPU need to have its source compiled to those Machine Code instructions which can be understood by the processor where it is supposed to be run. The system crashes if it encounters a foreign instruction. This is called Platform Dependence. 

*Platform - a Processor with its own Instruction Set

KERNAL:
A computing machine with its numerous built-in hardware resources need to be managed efficiently. Example - Memory management, Optimize performance, drive media and other hardware apart from CPU etc. This needs a software program called Kernal-normally written in C and/or Assembly - is the first program that is loaded by BootLoader when you switch-on a computer or a mobile. It has instructions to drive literally each and every hardware packaged in a computing system. As mentioned above, like any software program, Kernal needs to have its Machine code specific to the underlying Processor. Hence, Kernal developers should compile it to corresponding Instruction set of the CPU(platform dependency). Apart from managing system resources, Kenral has yet another important task of being a bridge between other software applications(programs) which need to be run on the machine.

Being a Bridge:
Lets say you have written a piece of Java Program which you want to run it. Assume that the program contains instructions not just to CPU(operations involved with Stack and Heap) but to interact with other hardware(I/O) like display, speakers etc. 

eg: System.out.println("Hello World"); --> This is the operation that should display the message on monitor. This is a H/W operation.

The .java program first compiles to ByteCode represented as .class file. A program called Java Virtual Machine(JVM - java.exe) interprets this ByteCode to Machine Instruction(remember specific to Instruction Set of the underlying Processor) which can be understood by CPU. As part of this process, all H/W operations get compiled to a code that make calls to Kernal's H/W invoking code(Machine Code) corresponding to the H/W you want to interact with. These calls are called 'System Calls' or 'OS Calls'. These system-call invoking operations are called Privileged means User applications don't have authority to directly invoke hardware but can only be done with the help of Kernal.

*java.exe: On a side note, this is too a compiled C/C++ program which can be directly executed by CPU.

When you give a command to run the .java program, the Kernal loads your compiled Machine Code in the memory(RAM) and instructs CPU to start acting on it. All the Stack & Heap related operations are directly executed by CPU but when it encounters any System Call, CPU executes corresponding Kernal's  Machine Code for that hardware. Hence, Kernal acts as a bridge between Hardware and software through System-Call architecture.

Lets understand with following example:
Example: Java program: Ignore the syntax for now.
int i=20;
int j=10;
sytem.out.println("Sum of i,j"+i+j);
System.playMusic("Some song");

Machine code of compiled Java program:
10010101 - CPU1 - Stack &Heap code
11010010 - CPU2 - Stack &Heap code
10100100 - MONITOR1 - System Call for Monitor
11010010 - CPU3 - Stack &Heap code
10010010 - MONITOR2 - System Call for Monitor
11010010 - CPU3 - Stack &Heap code
11010010 - CPU4 - Stack &Heap code
11100110 - SPEAKERS1 - System Call for Speaker
11010010 - CPU5 - Stack &Heap code
10101001 - SPEAKERS2 - System Call for Speaker

Kernal Machine Code for h/w resources:
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3

Finally, the combined code is what is being executed by CPU by switching between User's Java program and Kernal.
10010101 - CPU1
11010010 - CPU2
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
11010010 - CPU3
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
11010010 - CPU3
11010010 - CPU4
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3
11010010 - CPU5
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3


P.S.
MOST IMPORTANT: Any software program/application be it OS Program or User specific application - Java, c#, c++, c, JavaScript or python etc - has to be ultimately compiled to Machine Code corresponding to the specific CPU(Microprocessor) on which it needs to be run.

In fact, CPU doesn't different differentiate between OS program or User Application. It executes Binary Instructions of those programs.

CPU's view of Software applications or programs


Programmer's view of Software applications




No comments:

How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...