Showing posts with label OS. Show all posts
Showing posts with label OS. Show all posts

Sunday, 18 December 2016

// // Leave a Comment

What happens when you start your computer???

In this post we will discuss about what happens when you start your computer system and how that Linux operating system launched.
When you start computer system then control goes to BIOS that Basic Input Output System.
After that control goes to boot loader.
So question arises in mind that what is boot loader.
Here we will try to understand about boot loader.


What is Boot Loader?

Boot Loader is small code which is resides in MBR (Master Boot Record) which will load kernel code of operating system and then control goes to operating system.
When we switched on the computer system then first control  goes to BIOS. BIOS will check all peripherals and check booting order of devices.then select highest priority booting device and execute code stored in MBR of that device.

For Example :-



From Windows Vista onward they use BOOTMGR as a boot loader.


Microsoft Windows up to Windows XP comes with  NTLDR boot loader

These boot loaders are specifically designed to load Windows OS.



For the Ubuntu operating system the boot loader is GRUB2 (Grand Unified Boot loader 2).



What is Master Boot Record (MBR)?



Its located in first sector of HDD, CD, other device.Master Boot Record (MBR) is one block with 512 bytes. The Size of MBR is 512 bytes because smallest sector size on hard disk is 512 bytes Its holds the partition entries and boot code.

Following MBR block, the actual partition are begin.





If you disconnect your hard disk from one machine and attached to another machine then that machine will understand about the partition table and file system from MBR. Because MBR held the data regarding file system and partition table.




As we know that size of MBR is 512 bytes. Out of which 446 bytes are used for boot code (Boot Loader) , 4 partition entry (each entry required 16 bytes so total 16*4=64) and 2 bytes for MBR signature. Total =446+64+2=512 bytes.



As MBR can store only four entries for partition due to that resoan in one Hard disk you can create only four partitons. All four will be primary or 3 primary and 1 logical. But you cannot create more than that partitions.


Here each volume (or) partition has separate boot sector its called volume boot record apart from MBR. its used for chain-loader of boot loader. The reason is grub boot loader can boot all Linux .




 GRUB Boot loader --> Load Linux Kernel




but GRUB Boot loader cannot load windows opearting system because GRUB dont knows windows boot process.




How GRUB Works?




In 446 bytes in MBR is not enough to put entire grub code. so MBR contain small code called boot.img and its capable to read first block of core.img file from any partition (logical volume too) before partition is mounted. Because that time they cant understand the file system. so they read directly one sector (block) where core.img file is located.

boot.img location   

core.img location
diskboot.img location




The first block of core.img is called diskboot.img for HDD or cdboot.img for cd-rom or pxeboot.img for network boot. This block contain many address of blocks , to iterate these address and load blocks then the complete core.img (around 32 KB) comes into memory. This is job for boot code in GRUB.

Actually core.img file is generated when GRUB is installed . This file contain one or more necessary module to mount the File system, where GRUB is installed. So core.img file is responsible for mount the File system and access the GRUB configuration file. Once file system is mounted then we can access the files normal like Linux. All other modules are located /boot/grub. Its loaded when its needed.

All grub related configuration (menu entries, graphics resolution, timeout, etc...) are stored grub.cfg file in /boot/grub/ location. No need to put this line in MBR. because once File system is mounted then we can access through path /boot/grub/grub.cfg 

Read More

Monday, 24 October 2016

// // Leave a Comment

How c program runs in os?

Today we are going do discuss when you are going to run any user application or any program in Linux operating system. Here we are considering c program.
in the last post we seen how we are executing c program in Linux.
Now we are going to discuss how c program handled by operating system.
before starting with it. we need to go through some basic concepts of OS and kernel.
Basically there are two spaces in system
1) User space:- Where we are going to run user application. Most of the user program runs in this space.
2) Kernel space:- In the kernel space is reserved for kernel programs and device drivers.
In kernel space all file management , memory management and scheduling programs are executed.

In the following figure you can able to see the clear difference between user space and kernel space.
when you are going to run any user program its runs in user space.
Image result for user space and kernel space in linux
In the figure there is linkage present between user space and kernel space and that is system calls.
Now we will see what is use of system call in simple way.
user processes can directly communicated to kernel or access hardware due to security purpose.
so they communicated through the system calls.
you can say system call is systematic way for user process to request kernel service such as hard disk access or any other device access, creation of any new process or process scheduling.....
  In the Linux operating system the system calls are handled by POSIX libraries.

lets discuss briefly about POSIX.
POSIX is Portable Operating System Interface.
POSIX defines Application Programming Interface(API) and some command shell and utilities.
lets understands in easy way lets consider I want to develop application for Linux operating system so I have to follow posix standard so my application will able to communicate with kernel.

You can get detailed function set of POSIX here.
you will understand this concept after this example.

Let us consider you written down simple C program. Now in the c program you used c system function  printf("hello world") which is function of c.

So when you are going to execute c program you have to tell to the OS that you have to display hello world on terminal but c program is running in user space so c program dont have privilege to access the output window. only kernel can access that so C program requests to POSIX and the POSIX carry forward that request to kernel.
and then we are able to see hello world message on terminal.

printf()----> write()---->kernel code
C program Function----->POSIX fuction------->Kernel code for write()

to see how the system call works:
run the c program to create temporary file for the same refer previous post.
that is $gcc -save-temps -o hi.c
now open the file hi.s
that is assembly code.
the below code is for hi.s

.global _start

     

        .text
_start
_start:        # write(1, message, 13)        mov     $1, %rax                # system call 1 is write        mov     $1, %rdi                # file handle 1 is stdout        mov     $message, %rsi          # address of string to output        mov     $13, %rdx               # number of bytes        syscall                         # invoke operating system to do the write
        # exit(0)        mov     $60, %rax               # system call 60 is exit        xor     %rdi, %rdi              # we want return code 0        syscall                         # invoke operating system to exitmessage:        .ascii  "hi h r u"

In this you can understand from the comments.
This way you can able to see how c program runs by OS.








Read More