Thursday, March 28, 2013

Linux Kernel

 What is a kernel ?


                      A kernel is the part of the operating system that mediates access to system resources. It's responsible for enabling multiple applications to effectively  share the hardware by controlling access to CPU, memory, disk I/O, and networking.







                      In simple words, a kernel is an interface between hardware and OS  It takes commands from OS and make hardware act upon and takes data from hardware and gives it to OS. It also provides inter process communication.

kernel has two execution levels: 




 
 Kernel space

    Code running in the kernel mode is said to be inside the kernel space. 


User space

 
    Every other programs, running in user mode, is said to be in user space.


 



 Let see what is inside the Linux kernel

 

                      Here you can see a large collection of entry points,technically speaking system calls.when an interrupt happen this system call switch the operating mode of the processor from user space to kernel space.This will open, read, write and close the abstraction layer of the underlying hardware such as files,storage devices, and memory.

                     The filesystem is one of the more obvious abstractions provided by the kernel. Some features are not so directly visible. For example, the kernel is responsible for process scheduling. At any one time, there are likely to be several processes (programs) waiting to run.

                     The kernel's scheduler allocates CPU time to each one, so that if you look over a longer timescale (a few seconds) you have the illusion that the computer is running several programs at the same time. Here's another little C program:




                        This program creates a new process; the original process (the parent) and the new process (the child) each write a message to standard output, then terminate you can see system calls fork(), exit() and wait() perform process creation, termination and synchronisation respectively.These are elegantly simple calls that hide the underlying compexities of process management and scheduling.

Operating system concepts


 


                An operating system is the core software that manages the hardware and software resources such things as the processor, memory, disk space.

 





The operating system's tasks, in the most general sense, fall into seven categories:

  •     Kernel management   
  •     Processor management
  •     Memory management
  •     Device management
  •     Storage management
  •     Application interface
  •     User interface
  
In simple terms when user working on a word processing application issues a print,Operating system dispatches the commands to the hardware [printer],which prints the datasheet.so we  can say that operating system  is an intermediate layer between user application and system hardware.Operating system major components consist of program execution,interrupts,multi tasking,user interface and kernel.


Thus An operating system is the kernel plus applications that enable users to get something done (i.e compiler, text editor, window manager, etc)


Wednesday, March 27, 2013

Linux Boot Process

 

 

1.BIOS (Basic Input Out System)

The first step of the BIOS is the power-on self test (POST).
The second step is to locate boot device which is both active and bootable .
If all goes well, the BIOS reads the MBR of the boot device(HDD) and loads the program into memory.


2. MBR (Master Boot Record)



The MBR is 512 bytes of the boot device.
446 bytes contain Boot loader (GRUB for Linux).
64 bytes contains partition table,
2 bytes for error check.



3. GRUB (GRand Unified Bootloader)


The purpose of a bootloader is to load the operating system.


GRUB works in stages called Stage1, and Stage 2.


Stage1
  1. The stage1 code of GRUB is written within the 512 bytes of the MBR.Its sole job is to locate the second stage boot loader .

Stage 2
  1. This stage presents users with a Graphical screen showing different OS.
  2. From this screen user can select which OS to boot,if no selection made the boot loader loads the default OS


The configuration file for the above screen is written in the /boot/grub/grub.conf





    Once user has determined which kernel to boot.It locates the corresponding kernel binary in the /boot/ directory ie: /boot/vmlinuz-<kernel-version>

Then the bootloader load initramfs images into memory.The initramfs is used by the kernel to load drivers and modules necessary to boot the system.

Once the kernel and the initramfs image(s) are loaded into memory, the boot loader hands control of the boot process to the kernel.


4. Kernel

Kernel image is not an executable file, but a compressed image. Typically this is a bzImage or zImage.

During the boot of the kernel, the initial-RAM disk (initrd) also loads into memory.This initrd serves as a temporary root file system in RAM and allows the kernel to access disks and the file systems on those disks.

Then the kernel initializes and configures the computer's memory and configures the various hardware attached to the system, including all processors, I/O subsystems, and storage devices .

It then looks for the compressed initramfs image(s) in a predetermined location in memory, decompresses it directly to /sysroot/, and loads all necessary drivers.

The kernel then creates a root device, mounts the root partition read-only, and frees any unused memory. At this point, the kernel is loaded into memory and operational.
kernel starts the first user-space application which is in /sbin/init

5. INIT - initialization


On system bootup, the init process runs the /etc/rc.d/rc.sysinit script to setup the system.rc.sysinit reads the /etc/sysconfig/clock ,/etc/rc.serial file. 
 
Next, the init command sets the source function library, /etc/rc.d/init.d/functions, for the system, which configures how to start, kill, and determine the PID of a program.


The init command then runs the /etc/inittab script .



Runlevels are a state, or mode, defined by the services listed in the SysV /etc/rc.d/rc<x>.d/ directory, where <x> is the number of the runlevel.
 
SysV runlevels define how the system is started and what services are available in the running system.


Run Level 0 Halt
Run Level 1 Single user mode
Run Level 2 Multiuser but without NFS
Run Level 3 Full multiuser mode (text mode login)
Run Level 4 unused / User-definable
Run Level 5 X11 (graphical login )
Run Level 6 reboot


After booting, the system starts as defined in /etc/inittab in the line initdefault
 
As per default runlevel settings, system will reads and execute the programs from
following directories.

  1. Run Level 0 – /etc/rc.d/rc0.d/
  2. Run Level 1 – /etc/rc.d/rc1.d/
  3. Run Level 2 – /etc/rc.d/rc2.d/
  4. Run Level 3 – /etc/rc.d/rc3.d/
  5. Run Level 4 – /etc/rc.d/rc4.d/
  6. Run Level 5 – /etc/rc.d/rc5.d/
  7. Run Level 6 – /etc/rc.d/rc6.d/
/etc/rc0.d/ is linked to /etc/rc.d/rc0.d/ directory. Under /etc/rc.d/rc0.d/ directory we could see programs starting with S and K which means startup and kill respectively.



Then /etc/inittab script forks an /sbin/mingetty process for each virtual console (login prompt) allocated to the runlevel. 
 
Runlevel 2 through 5 have all six virtual consoles
Runlevel 1 (single user mode) has one,
Runlevels 0 and 6 have none. 
 
The /sbin/mingetty process opens communication pathways to tty devices sets their modes,

This prints the login prompt, accepts the user's username and password, and initiates the login process.






Tuesday, March 26, 2013

LINUX Filesystem Explained



 
]
/bin -    command executable for all users(binaries)
            (cat,ls,cp)

/boot -  contains static files required to boot the system
            (bootloaders[GRUB,LILO],Linux kernel,initrd)

/dev -  device files, either character(keyboard)or block(HDD,USB)

/etc -  Host specific config file.Application also store their config filre here.
             (similar to control panel in windows)

/home - User's home directory

/lib   -   System Libraries files required by the /bin and /sbin.
                (similar to DLL in windows)

/media  - Mount point for removable storage.
                      (Cdrom,USB)

/mnt  -   Temporarily mounted file systems.
               (FAT,NTFS,UFS)

/opt - Add-on application packages.(optional)

/proc - virtual filesystem containing information kernel and process status.
             (uptime,network)

/root - Linux system administrator's home directory

/sbin - Binaries which can only be exceuted by root privilege accounts.
            (route,ifconfig,init)

/tmp - Temporary files

/usr - Contains binaries, libraries, documentation, and source-code.(UNIX system resources)
              (/usr/doc,share,src,include,X11R6)

/var - variable files.system log files and  and temporary spool.
                (/var/log,spool,mail)

Monday, March 25, 2013