Saturday 11 August 2012

Threads and Processes

Processes and Threads:

This is my first blog and I am pretty excited to write it. I would like to give a deep overview about the processes. I being a Computer Science student, like to explore new things and hence i was very keen to know about what actually happens inside my PC.

The OS which I am going to make use of to explain this is LINUX

To know what a process is we have to know how that is created. It will be clearly understood if we know what actually happens from the start of our PC.

The Booting...

Whenever you switch on the power button of your PC, there is a program called BIOS(Basic Input Output System) on the ROM(Read only memory), which is the first program ran by the PC. The first instruction executed by the processor is JMP. For more details you can see here

The job of this program is to initialize and identify all the devices like RAM, CPU, Keyboard and other devices. Its next job is to identify the boot-strap program from the booting device. The device may be a CD/DVD, hard disk or a floppy disk. On hard disk there is a separate sector called Boot Sector which consists of the Boot-strap program. This is the first sector on the hard-disk.

So what is this 'Boot Loader'?

Boot Loader in Linux systems is specifically called as MBR(Master Boot Record). It generally stores the information about the starting addresses and the length of the partitions situated on the Hard-disk. It also has the code which executes another program called GRUB(Grand Unified Boot-Loader).

GRUB is the program that allows you to choose a kernel from a list of available kernels. If you observe carefully this screen is the one which appears with a pink or ash background and shows a list of kernels to load. This screen will be seen if you have upgraded your linux OS to a newer version. It waits for few seconds and executes the default kernel if none is selected by us. Now, the control transfers to the selected kernel.

The Kernel

Kernel is the core part of the operating system. The kernel is the one that runs as long as the system is working. The first job of it when it is loaded is to mount the root file system. It then executes the scheduler program with process ID 0 and then a program called init which has the process ID 1. This is the process that spawns all other processes.

This program then decides what to do based on the Run Level and it generally executes all the programs specified for the different run levels. The list of the programs can be viewed at /etc/rc*.d/ where * lies between 0 and 6 inclusive. At last the system will be ready with all the essential programs being executed and waits for the response of the user.

Difference between Kernel and Operating System

The kernel as I said before is the core of the operating System. I say 'core' because it actually acts as the bridge between the processes and the System resources. Here, the System Resources include the main memory, CPU, Input/Output(I/O).

It actually decides which program to run and which to not. It requests for Input/Output from the devices on the behalf of the processes. The processes communicate with the Kernel using the so-called System Calls.

Examples include fork, read, write, close, exit and so on... There are about 300 different System calls for LINUX!!!.

Operating System handles all the Graphical User Interface programs and others. Even the Operating System programs should also request the kernel for resources. For example if you open Notepad on your PC, then it has to request the kernel for space in the main memory and then gets executed.


Types of Processes

There are different types of processes running on the system.

Interactive Processes are those which belong mostly to the User Interface part. All the actions involving clicking a button, closing a window are carried by these processes. They are given higher priority and all these are Round Robin scheduling. These are usually started from the terminal.

Daemon Processes on the other hand run in background where the user cannot see what the process is doing. These processes include server software. We can also create a daemon process from the terminal just by appending & at the end of the name of the process which you want to run.

Suppose, you want to run gedit from the terminal in order to write a program. You don't want to open another compiler to compile that program. Then you can initially start the process by issuing the following command:

gedit abc.cpp&

Batch Processes on the other hand are not started through the terminal, but they are executed automatically without any manual intervention, and hence the other name Automatic Processes.These are executed on a First in First out(FIFO) basis.

Structure of a Process:

Considering a multiprocessing environment, where the processor switches between different processes, it is important to store a pointer to next instruction a processor must execute in case it is resumed an Instruction Pointer is necessary. Along with these we need to store the Register values and also a Stack pointer since the process may execute different functions. All the entities that are described above are essential for every process. These are collectively known as Context of a process.

Threads:

Threads on the other hand are just really like processes, but are light weight in nature. These are useful over processes because the time for context switch between two processes is more compared to that of threads. The main reason is that threads have unique register values, stacks and Instruction pointer, the remaining are shared among all the threads. These include File descriptors, the instruction part of the process etc.


In Linux, threads are created by clone() system call, while processes are created by fork() system call.


This is a brief overview of what a process is and what a thread is. I hope you have understood it. But if there are any doubts that are crept in your mind, please do ask through the comments. :) :)

Next>>