Process Vs Threads

Process

In simple terms, any program in execution is called as process. Every process has a process state associated with it like memory, resources, code, data etc. In operating system, process has unique process identifier and associated process controller block (PCB). We will see in detail what PCB contains in later posts.

Process execution

Thread

Thread is an abstract entity which executes a sequence of instructions. Threads are light weight as compared to process, their creation is efficient and inter thread communication is fast as they use memory to communicate with each other instead of IPC. Process context switch is rather very expensive operation as compared to thread switches. Every program has at least one thread of execution.  A thread cannot live on its own, it has to be within some process. A process can have multiple threads.

Thread execution

From the definition we came across some differences. Let’s tabulate them.

Process

Threads

Can live on its own

Always runs in a process

All process have separate address space and stacks

Threads of a process share address space with each other. Threads have separate stack and registers as process has.

Inter process communication is done using IPC

Inter thread communication is done through shared memory.

Heavy weight in terms of IPC, context switch and creation

Light weight in terms of IPC, context switch and creation.

Process has data segment and heap of its own

Thread does not have them

There are two types of threads :

1. User space threads.

2. Kernel space threads.

Both have their own use and use case for using in a particular context.

User space threads are very fast in terms of create and switch. But they are not suitable when we need to do blocking I/O calls. Language level threads are best example of this kind of threads.

Kernel space threads are inefficient in terms of creation and switch but they do not block on system calls. These threads are scheduled using system scheduler as any normal process. Programmer does not have any direct controller over these threads.

There are models where one user thread maps to one kernel thread and one user thread maps to multiple kernel space threads or multiple user threads maps to one kernel space threads. These are termed as 

1: 1 Model

M: N Model

M : 1 Model

These models are realized using virtual processor (VP).  For first model, all user threads run in one VP.

In second model, user threads run in pool of VPs. This is default model.  While in third model, multiple user threads run in single VP.

In next post we would discuss POSIX library APIs for thread creation, execution and termination along with detailed look at process life cycle and structures used.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s