Monday, March 23

COMPARISON OF THREADS AND PROCESS

THREADS ?



  • A thread is a primitive that can execute code. 
  • It is a single sequential flow of control within a process.
  • A process can have anywhere from just one thread to many threads.
  • Different threads which are part of a process share the same address space meaning they share the memory area. They maintain their own thread status, program counter and stack.
Each process has it’s own address space, but the threads within the same process share that address space. Threads also share any other resources within that process. This means sharing data among threads is easier.

Context switching between threads is generally less expensive than in processes


Threads, of course, allow for multi-threading. 

Multithreading :

A process can be single threaded or multi threaded. An example of multi threading that all of us have encountered is the fact that we can have a word processor that prints a document using a background thread, but at the same time another thread is running that accepts user input, so that we can type up a new document. If it was single threaded printing and responding to user input at the same time would not be possible.
Advantages of multithreading

  1. Better Memory Utilization.
  2. Efficient CPU utilization.
  3. Even if one of the threads of a process enters a wait state the process can continue its execution with other threads that are ready for execution, this speeds up the execution of process.

Different Thread standards includes : 

POSIX
Win32 Threads.
Java Threads.

Here is a summary of the differences between threads and process

Process
Threads
A process is an executing instance of an application
Thread is a single unit of execution and is part of a process
Process has its own code memory data memory and stack memory
Threads doesn’t have its own memory, it shares the memory allotted to a process with other threads of the same process. Each thread holds separate memory area for the stack.
A process contains at least one thread.
Threads live within a process
Creation of a process involves many OS overhead
Threads are inexpensive to create
Context switching is slower and involves a lot of overhead
Faster context switching
If a process expires all the associated resources are reclaimed by the OS and all the threads associated with it dies
If a thread is expired its associated stack is reclaimed by the process




No comments:

Post a Comment