Proces vs Thread

Process : 
Is an executing instance of an application . They run in separate memory space unlike threads.
To put it in simple terms, we write our computer programs in a text file and when we execute this program, it becomes a process which performs all the tasks mentioned in the program.
Example:when you double-click the Microsoft Word icon, you start a process that runs word.

Threads : 
are path of execution within a process . Threads within the same process runs in a shared memory space .

-----------------------------------------------------------------
-----------------------------------------------------------------

U may have heard process is heavy weight . actually when process is created Operating system creates a PCB (process control block) which includes may things like 

  • process id - Unique identification for each of the process in the operating system.
  • pointers  -A pointer to parent process.
  • state -The current state of the process i.e., whether it is ready, running, waiting, or whatever.
  • counter -Program Counter is a pointer to the address of the next instruction to be executed for this process.
  • Cpu registers- Various CPU registers where process need to be stored for execution for running state
  • Accounting information-This includes the amount of CPU used for process execution, time limits, execution ID etc.
  • Memory management information-This includes the information of page table, memory limits, Segment table depending on memory used by the operating system.


This information is necessary for scheduling of processes by Operating system (for each process to get proper time on CPU) . Also sharing of resources between process requires a lot of overhead(due to security provided by Os) .
while threads are part of process that run independent without much overhead . Each process have one thread while each process can create multiple thread for execution .Interthread communication is each (it is responsibility of programmer to see security). On multicore processor each thread can actually run simultaneously on each core of processor.


Following things threads shares with other threads :
  1. Code section
  2. Data Section (or data segment , eg. global & static variables are stored in data segment are shared by all threads)
  3. OS resources (eg. open files & signals)
  4. Address Space (Shared memory Space)
But like Process , threads has its own :
  1. Program Counter (PC)
  2. Register set
  3. Stack space.

Finally have a look at this picture for more clarity.


Image clarifies the difference between Process and Thread. However lets discuss it point by point.

Process
A process has a self-contained execution environment. An instance of program called Process.
  • Each Process has its own address space.
  • Process have to use Inter Process Communication (IPC) resource like pipes and sockets to communicate with other process.
  • New processes require duplication of the parent process.
  • Processes have considerable overhead.
  • Process have their own copy of data segment of the parent process.
  • Process have control over its child processes.
  • Any change in the parent process does not affect child processes.
  • Process is controlled by the operating system.
  • Processes are independent.

Thread
Threads are sometimes called lightweight processes. It runs within the Process.
  • Thread(s) shares the address space of Process that created it.
  • Threads can directly communicate with other Threads of its process.
  • New threads can be created easily.
  • Threads have almost no overhead.
  • Threads have direct access to data segment of its Process.
  • Threads have considerable control over threads of the same process.
  • Any change in main thread (cancellation, priority change, etc...) may affect the behavior of the other threads of the process.
  • Threads are controlled by programmer in a program.
  • Threads are dependent.


Comments

Popular posts from this blog

Different Type of keys in Database Managemenet System.

FC Barcelona vs Real Madrid -El Clasico Rivalry.

Normalization in Database.