>_
EngineeringNotes
← Back to OS Internals
Chapter 02

Processes & Threads

Understanding programs in execution, logical control flows, and CPU context switching.

01

Process & Thread

Process

Program under execution, which has been loaded into RAM from disk and given resources by the OS.

Program:It is an executable file which contains a certain set of instructions written to complete a specific job.

=> Compiled Code. Ready to execute=> Stored in Disk

Thread

A light-weight process. Thread is used to achieve parallelism by dividing a process's tasks which are independent paths of execution.

Example:Multiple tabs in browser, text editor (checking spelling, syntax, saving code).

=> Multiple threads perform these tasks simultaneously.

Visualizing Threads in an Application

Application (Process)
Take I/P
Save to cloud (Thread)
Display data

The "Save to cloud" thread can do this work as it is a totally independent task and can be executed independently without any interruption (asynchronous work).

Process
├── Main Thread
└── Sub-task Thread (Independent process, e.g., save to cloud)
02

Why Threads? (Image Conversion Analogy)

Scenario: Convert 200x100 JPG image to 100x100 PNGs

Platform converts 100x100 JPG image to 100x100 PNG. We have a 200x100 image, so we split it into two 100x100 images (A and B).

Sequential (20 sec)
A
Logic (10s)
A1
B
Logic (10s)
B1

Total Time: 10 + 10 = 20 seconds

Multi-threading (10 sec)
A
Th 1 (10s)
Logic
A1
B
Th 2 (10s)
Logic
B1

Total Time: 10 seconds (Running concurrently)

Wait... Single CPU Limitation!But this is not possible in case of Single CPU as both tasks require different CPU to execute. So, Single CPU cannot perform Multi-tasking Multi-threading in true parallel logic. It will work on a multi-processing OS (Windows, Linux, ...).

In a single CPU system we can perform multi-tasking, but the same concept of time quantum sharing works and it will not cause any physical gain in execution speed for CPU-bound tasks.
03

Multi-Tasking vs Multi-Threading

Multi-TaskingMulti-Threading
The execution of more than one task simultaneously is called multi-tasking.A process is divided into several different sub-tasks called as threads, which have their own path of execution.
Concept of more than 1 processes being context switched.Concept of more than 1 thread. Threads are context switched.
No. of CPU: 1No. of CPU ≥ 1 (better to have more than 1)
Isolation & Memory Protection:
OS allocates separate memory & resource to each program.
No Isolation & Memory Protection:
OS allocates memory to process, multiple threads of that process share the same memory & resources.
04

Thread Scheduling & Context Switching

Thread Scheduling: Threads are scheduled for execution based on their priority. Even though threads are executing within the runtime, all threads are assigned processor time slices by the Operating System.

Thread Context SwitchingProcess Context Switching
OS saves current state of thread & switches to another thread of same process.OS saves current state of process & switches to another process by restoring its state.
Doesn't include switching of memory space.Includes switching of memory address space.
Fast switchingSlow switching
CPU cached state is preserved.CPU's cache state is flushed.