Multi-Tasking, Multi-Threading, Kernel, System Calls

๐Ÿง  Program vs Process vs Thread

Term Meaning
Program A file with code (instructions), stored on disk, ready to be run.
Process A running instance of a program (stored in RAM). Has its own memory, resources.
Thread A smaller unit of a process. A path of execution within a process. Shares memory with other threads in the same process.

โš™๏ธ Multitasking vs Multithreading

Feature Multitasking Multithreading
Definition Running multiple processes at once Running multiple threads (tasks) inside a single process
Example Opening a browser, music player, and Word at the same time While typing in Word: auto-save, spell-check, and UI update run simultaneously
Switching Between processes (heavyweight) Between threads (lightweight)
Memory Each process has separate memory Threads share memory of the same process
Speed Slower (context switch needs more resources) Faster (context switch is lightweight)
CPU Requirement Can work with 1 CPU Better performance with multi-core CPU

๐Ÿ” Context Switching: Process vs Thread

Feature Thread Switching Process Switching
Switches Between threads of same process Between different processes
Memory Switch No memory address space switch Memory address space must switch
Speed Fast Slower
CPU Cache Preserved Flushed

๐Ÿงฉ Components of Operating System

OS Components Diagram

1. Kernel (Core of the OS)

  • Directly talks to hardware.
  • Loaded first when OS starts.
  • Responsible for process mgmt, memory mgmt, file mgmt, I/O mgmt.

2. User Space

  • Where apps and software run.
  • Can't directly access hardware.
  • Talks to kernel via system calls.
  • Examples: GUI, Command Line (Shell).

๐Ÿงต Kernel Functions in Detail

Process Management

Create/suspend/terminate processes, manage threads

Memory Management

Allocate/deallocate memory, track usage

File Management

Create/delete files and directories, backup

I/O Management

Handle input/output devices, use buffering, caching, spooling

๐Ÿง  Buffering vs Caching vs Spooling

Buffering vs Caching vs Spooling
Concept What it is Example
Buffering Temporary storage during a task YouTube buffering during playback
Caching Storing frequently used data Webpage loading faster on second visit
Spooling Queueing slower jobs Printing documents one by one

๐Ÿงฌ Types of Kernels

Type Features Pros Cons Examples
Monolithic Kernel All OS services (memory, process, file, I/O) are in kernel space.
  • High performance
  • Fast communication (no user-kernel mode switch)
  • Bulky in size
  • Less reliable (one crash may bring whole system down)
  • High memory usage
Linux, Unix, MS-DOS
Microkernel Only core functionalities (like memory & process management) in kernel. Other services run in user space.
  • Smaller in size
  • More stable and secure
  • Modular and reliable
  • Slower performance
  • Overhead due to user-kernel switching
MINIX, Symbian OS, L4 Linux
Hybrid Kernel Combines features of Monolithic and Microkernels. Some services in kernel, others in user space.
  • Balanced speed and stability
  • Modular and extensible
  • Still has some overhead
  • More complex than pure designs
Windows NT/7/10, macOS

๐Ÿ”— Communication: User Mode โ†” Kernel Mode

User Mode and Kernel Mode Communication
  • Apps (in user space) cannot directly access hardware.
  • They request kernel services via System Calls (with help of Shell or CLI).
  • System Call = Gateway between User Mode & Kernel Mode

๐Ÿ“Œ Example:

mkdir folder
mkdir is not the real function that creates a folder.
It calls a system call (like mkdir() syscall in C) to tell kernel to create directory.

๐Ÿ“ž What is a System Call?

  • A programmatic way for user applications to ask OS for resources or services.
  • Needed because user programs don't have permissions for direct hardware access.
  • Implemented in C and triggered by software interrupts.

๐ŸŽฏ Common System Calls:

Process Control

fork(), exec(), exit()

File Operations

open(), read(), write()

Device Management

ioctl(), read(), write()

Info Management

getpid(), alarm()

Communication

pipe(), shmget(), msgsnd()

๐Ÿ“ Summary (Quick Revision)

Concept Key Point
Program Stored code on disk
Process Running instance of a program
Thread Light-weight unit of process, shares memory
Multitasking Multiple processes running
Multithreading Multiple threads in same process
Kernel Heart of OS, handles core tasks
System Call Bridge between user programs and kernel
Buffering Within job, temporary storage (e.g., video streaming)
Spooling Job queueing for devices (e.g., printer)
Caching Speed up frequent access (e.g., web cache)

๐Ÿงช Interview-Oriented Questions

  1. What is the difference between a process and a thread?
  2. How does multithreading improve performance?
  3. Explain context switching in multithreading vs multitasking.
  4. What are the responsibilities of a kernel?
  5. Explain different types of kernels.
  6. What is the purpose of system calls?
  7. How does user space communicate with kernel space?
  8. What is the difference between buffering, spooling, and caching?
  9. Why is process switching slower than thread switching?
  10. What happens behind the scenes when we run mkdir in the terminal?