Process, Thread, Concurrency, Parallel processing

Process

As the first step towards understanding multithreading, let us understand the difference between process and threads.    A program loaded in the computer memory in the form of binary ready to be executed.    Every process requires registers, counter and stack.  Registers are storage in the CPU ( central processing unit).  The program counter usually stores an address or the data required by the program. It is called “Instruction Pointer” which knows where the sequence of the program is in.  The stack is a space where the subroutines are pushed on to.  It is different from the heap which is dynamically allocated.   

There are various instances of a program running in the system and each of them is a process. Every process runs in isolation and does not share and resources. The switching from one process to another is usually time-consuming.   

process

Thread

How does thread work? 

Thread is a unit of execution of a process. A process can have multiple threads. The threads share the resources.   As per the name, the single-threaded application involves one thread. The thread and process mean the same.  In a multi-threaded application each thread has its own:

  • registers
  • stack,
  • Inside a process, they share the same heap.
  • Access the same address space – Lightweight process.

The cost of communication between them is low. But they do interfere with each other. We can study more in the later sections. 

process-threads-c++

  Process use inter-process communication whereas threads are fast to start and low overhead. They use shared memory to communicate which is faster. Distributed systems do not support threads. Do processes and threads run at the same time?   

Concurrency vs Parallelism

 The CPU is shared among running processes or threads using a process scheduling algorithm. This divides the CPU’s time and yields the illusion of parallel execution. The time given to each task is called a “time slice.” The switching back and forth between tasks happens so fast it is usually not perceptible. The terms parallelism (genuine simultaneous execution) and concurrency (interleaving of processes in time to give the appearance of simultaneous execution) distinguish between the two types of real or approximate simultaneous operation.