UnicMinds

Program vs. Process vs. Thread - Basics

Program vs. Process vs. Thread

Any line or flow of execution in an application software is always done by a thread. A thread is a single independent flow or line of execution. All application softwares first starts a process and then the process starts at least one thread called as the primary thread to execute something. If required the process can also start multiple threads that will all share the resources allocated to that process by the operating system.

Process vs. Program

A process is a program in execution – no confusion here. When an operating system executes a program it is called a process. So we may have a single program executed simultaneously multiple times which creates multiple processes. Also we may have a single program which creates multiple processes in the program. Each process has a unique ID called the process ID allocated by the OS.

Program vs. Process vs. Thread - UnicMinds
  • A process has at least one thread which is called as a primary thread. 
  • When a process starts, it is assigned memory and resources. A process can have one or more threads. 
  • A process can have multiple other threads too and all these threads share resources and the execution time is time-shared.
  • A program is what programmers write and a program in execution starts with a process. So, the run-time entity of a program is a process and its thread(s).
  • Threads exist as a subset of process whereas processes are independent
  • A process with multiple threads controls which thread to execute when in terms of starting, waiting, and stopping the threads.

Types of processes 

There are basically two types of event-specific processes – I/O Bound Process & CPU Bound Process.

  • I/O Bound Process: If a program/process spends more of its time doing I/O than it spends doing computations then it is called I/O bound process.
  • CPU Bound Process: If a program/process spends more of its time doing CPU operations (computations) and generates I/O requests infrequently then it is called CPU bound process.
Process Memory & Process Control Block - UnicMinds

Each process is made up of few sections:

text: This is where the code of the process reside. This is a read-only section.

data: This is where global and static initialized variables are stored.

rodata: This is where constant variables are stored.

bss (block started by symbol): This is where global and static un-initialized variables are stored.

stack: This is where the local variables and function calls are stored.

heap: This is where user allocated memory is stored. When the user uses malloc, calloc, new etc.

Memory Layout - Text, Data, BSS, Stack, Heap, RoData

To learn more about these, refer to this excellent post on memory layout by Muhammad Hassan Shah

What is a Thread

A thread is the unit of execution within a process. A process can have anywhere from one thread to many. It comes with its own set of registers and stack. A single-thread process means that a process could only perform a task one at a time. There is also multi-threads process, meaning it could run multiple tasks at a time. Of course multi-threads are more efficient, but it also brings some side effects that single thread wouldn’t encounter, such as deadlock and concurrency. 

It is important to understand that some languages support multi-threading and some languages don’t support multi-threading. For example, C++ supports multi-threading whereas JavaScript is a single threaded language which doesn’t support multi-threading.

Summary

In summary, a process is an instance of a program with its own address space, stack and heap. A process might have multiple threads within but it will at least have one thread. All the threads of a process will have individual stacks but they will share the heap. If a process has only one thread it can only use one core. Most modern CPUs have 8 to 64 cores – so, if you do multithreading – each of those threads can run on multiple cores and hence take advantage of more resources and the program can be efficient.

Hope this is useful, thank you.

You may like to read: The Travelling Salesman Problem, Motion Blocks in Scratch Programming, & Microbit Programming for Kids

Leave a Comment

Your email address will not be published. Required fields are marked *

BOOK A FREE TRIAL