Tuesday, April 15, 2014

Differences between thread and process


What’s the difference between a thread and a process?


Both processes and threads are independent sequences of execution.
Lets focus on difference - process vs threads.

Thread Process
Threads (of the same process) run in a shared memory space. A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. Processes run in separate memory spaces.
 In addition, each thread maintains
  • exception handlers, 
  • a scheduling priority, 
  • thread local storage, 
  • a unique thread identifier, and 
  • a set of structures the system will use to save the thread context until it is scheduled. 
A process has
  • a virtual address space, 
  • executable code, 
  • open handles to system objects, 
  • a security context, 
  • a unique process identifier, 
  • environment variables, 
  • a priority class, 
  • minimum and maximum working set sizes, 
  • and at least one thread of execution. 
The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

A process can be thought of as an instance of a program in execution. Each process is an independent entity to which system resources (CPU time, memory, etc.) are allocated and each process is executed in a separate address space. One process cannot access the variables and data structures of another process. If you wish to access another process’ resources, inter-process communications have to be used such as pipes, files, sockets etc.

A thread uses the same stack space of a process. A process can have multiple threads. A key difference between processes and threads is that multiple threads share parts of their state. Typically, one allows multiple threads to read and write the same memory (no processes can directly access the memory of another process). However, each thread still has its own registers and its own stack, but other threads can read and write the stack memory.

A thread is a particular execution path of a process; when one thread modifies a process resource, the change is immediately visible to sibling threads.



Post a Comment