Program vs. Process vs. Thread vs. Task

These are four very similar terms yet very different. Lets start understanding the difference between them.

As everybody says its better to visualize than just blabber. So I have put all my thoughts about these 4 terms in the form of a diagram as shown below.

blog4

Now here comes the textual part:

A program in Simple terms can be described as any executable file. Basically it contains certain set of instructions written with the intent of carrying out a specific operation. It resides in Memory & is a passive entity which doesn’t go away when system reboots.

Any running instance of a program is called as process or it can also be described as a program under execution. 1 program can have N processes. Process resides in main memory & hence disappears whenever machine reboots. Multiple processes can be run in parallel on a multiprocessor system.

A Thread is commonly described as a lightweight process. 1 process can have N threads. All threads which are associated with a common process share same memory as of process.The essential difference between a thread and a process is the work that each one is used to accomplish. Threads are being used for small & compact tasks, whereas processes are being used for more heavy tasks.

One major difference between a thread and a process is that threads within the same process consume the same address space, whereas different processes do not. This allows threads to read from and write to the common shared and data structures and variables, and also increases ease of communication between threads. Communication between two or more processes – also known as Inter-Process Communication i.e. IPC – is quite difficult and uses intensive resources.

Tasks are very much similar to threads, the difference is that they generally do not interact directly with OS. Like a Thread Pool, a task does not create its own OS thread. A task may or may not have more than one thread internally.

If you want to know when to use Task and when to use Thread: Task is simpler to use and more efficient that creating your own Threads. But sometimes, you need more control than what is offered by Task. In those cases, it makes more sense to use Thread directly.

The bottom line is that Task is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.The only reasons to explicitly create your own Threads in modern code are setting per-thread options, or maintaining a persistent thread that needs to maintain its own identity.

Advertisements

2 thoughts on “Program vs. Process vs. Thread vs. Task

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