1.
Shell and Kernel
1.1 What is a Shell?
A shell is a program that acts as a user interface to an operating system, translating user commands into
instructions the operating system kernel can understand and execute.
Shells can be of two types:
• Graphical (GUI) — allows interaction through windows and icons.
• Command-line (CLI) — where users type commands.
Difference between Shell and Kernel in an Operating System
1.2 Types of Shell
Command-Line Shell (CLI)
• Text-based interface.
• Examples:
◦ Linux/Unix: Bash, Zsh, Ksh, Tcsh
◦ Windows: Command Prompt, PowerShell
Graphical Shell (GUI)
• Provides windows, icons, and menus for user interaction.
• Examples:
◦ Windows Explorer (Windows)
◦ Finder (MacOS)
◦ GNOME, KDE (Linux desktops)
Key functions of a shell include acting as an interpreter, enabling interaction with the OS, automating tasks
through scripts, managing processes, and providing tools for input/output redirection and error handling.
1.3 What is a Kernel?
A kernel is the core component of an operating system that acts as a fundamental bridge between a
computer's hardware and its software, managing all system resources like memory, the CPU, and
input/output devices.
1.4 Types of Kernels
Monolithic Kernel
• Everything runs in kernel space (all services are tightly integrated).
• Examples: Linux kernel (Ubuntu, Red Hat, Android, etc.), MS-DOS (simpler monolithic system).
Microkernel
• Only essential services (like process and memory management) run in kernel space.
• Other services run in user space, improving reliability.
• Examples: QNX, Minix.
Hybrid Kernel
• Combination of monolithic and microkernel designs.
• Examples: Windows, macOS (XNU kernel), ReactOS.
Nano Kernel
• Minimal kernel with very limited functions, mostly delegating to hardware.
• Examples: Adeos, KeyKOS.
Exokernel
• Extremely small kernel that gives maximum control to applications. It lets applications directly manage
hardware resources.
• Examples: Nemesis, XOK.
Types of Kernels — overview diagram
1.5 Functions of the Kernel
Process Management
• Creates, schedules, and terminates processes.
• Decides which process gets CPU time.
Memory Management
• Allocates and deallocates memory to processes.
• Ensures one process doesn't interfere with another's memory.
Device Management
• Provides a way for processes to communicate with hardware (like printers, disks, keyboards) using
drivers.
File System Management
• Manages reading, writing, and storing files on disks.
System Calls (Interface)
• Provides services to user programs through system calls (like read(), write(), fork()).
• Without system calls, applications cannot directly access hardware.
2. Process
A Process is basically a program in execution.
A program is just a set of instructions stored on disk (like a .exe file or script). When you run it, the operating
system loads it into memory, gives it resources (CPU time, memory space, I/O handles), and tracks its
progress. That active instance is the process.
2.1 Process States and Lifecycle
(new, ready, running, waiting, terminated)
Process state transition diagram
A process state is the current condition or stage of a process while it is being managed by an operating
system.
New
• A process is created (for example, when you open an application).
• At this stage, it has not yet been admitted to the ready queue.
• Example: When you click on a program icon, the process is created but not yet scheduled.
Ready
• The process is loaded into main memory and is waiting for CPU allocation.
• It is ready to run but the CPU is busy executing other processes.
• Think of it as "waiting in line" for the CPU.
Running
• The process is currently being executed on the CPU.
• Only one process per CPU core can be in this state at a time.
• Example: When your code is actually executing instructions.
Waiting (or Blocked)
• A process enters this state when it is waiting for some event to occur, like I/O completion (disk read,
network response, keyboard input).
• The process cannot continue until the event is done.
Terminated (or Exit)
• The process has finished execution (either successfully or killed by the OS).
• It will be removed from memory.
2.2 Process in Memory — Layout
The process is stored in RAM as per the diagram below:
• Text/Program Section: A text or code segment contains executable instructions. It is typically a read-
only section.
• Data Section: Contains global variables and constants.
• Heap Section: Dynamically allocated memory to the process during its run time.
• Stack: The stack contains temporary data, such as function parameters, return addresses, and local
variables.
Layout of a process in memory (Text, Data, Heap, Stack)
Process in memory Block — structure overview
3. Process Control Block (PCB)
A Process Control Block (PCB) is a core data structure in an operating system that contains all the
information/attributes about a specific process, allowing the OS to manage and control its execution. The
PCB is maintained separately by the operating system in kernel space, not inside the process's memory
layout.
Context Switching is the process in which the CPU stops executing one process and starts executing
another process. Before switching, the operating system saves the current process's information
(context) in its PCB and loads the next process's information from its PCB.
3.1 Fields / Contents of a PCB
1. Pointer / Process State
• The process state tells whether the process is new, ready, running, waiting, or terminated.
• The pointer is used to link this PCB with other PCBs (like in a scheduling queue).
2. Process ID (PID)
• A unique number assigned to each process so the OS can identify it.
3. Program Counter
• Holds the address of the next instruction that the CPU will execute for this process.
• If the process is paused, this value helps the OS resume it later.
4. Registers
• Stores the current working values of the CPU registers (accumulators, index registers, stack pointers,
etc.) when the process is not running.
• Needed for context switching (so when the CPU comes back to this process, it continues exactly where
it left off).
5. Memory Limits
• Defines the memory boundaries allocated to this process (start and end addresses).
• Prevents one process from interfering with another's memory space.
6. Accounting Information
• Keeps track of how much CPU time, memory, I/O devices, etc., the process has used.
• Useful for scheduling, billing (in time-sharing systems), and performance monitoring.
7. List of Open Files
• Keeps track of files currently opened by the process.
• Ensures the OS knows which files to close if the process terminates unexpectedly.
4. Threads
A thread in an operating system is the smallest unit of execution inside a process.
Threads in operating systems are lightweight units of execution that allow programs to perform multiple
tasks concurrently within the same process.
4.1 Need of Threads in an OS
• Efficiency — Creating a new thread is lighter than creating a whole new process. Less overhead.
• Parallelism — Multiple threads can run on multiple CPU cores at the same time → faster execution.
• Responsiveness — Programs feel smoother. For example, a browser: one thread loads a page, another
plays music, another handles user input.
• Resource Sharing — Threads in the same process share memory and files, so communication is faster
than between processes.
Single core + multithreading = concurrency (one by one but fast) — time slicing / context switching.
Multi core + multithreading = real parallelism.
4.2 Single-threaded vs Multi-threaded Process
Single-threaded process
• Only one thread of execution — the program does one task at a time.
• If it's busy with something (say, reading a file), it can't do anything else until that's done.
• Example: A basic calculator app — it waits for you to press a button, calculates, then waits again. No
"parallel" work.
Multi-threaded process
• The process has more than one thread running inside it.
• Threads can handle different tasks at once, but all share the same memory and resources.
• This makes programs faster and smoother.
• Example: A modern web browser — one thread loads the webpage, one thread plays a video, one
thread handles scrolling and clicks. All still part of the same "browser process."
4.3 Types of Threads
There are two main types of threads: User-Level Thread and Kernel-Level Thread.
1. User-Level Threads (ULT)
• Managed entirely in user space by a thread library.
• The OS kernel is not aware of them; it only sees a single process.
• Very fast to create and switch because no kernel involvement.
• Limitation: If one thread blocks (e.g., waiting for I/O), all other threads in that process are also blocked.
• Examples: Java Green Threads, POSIX Pthreads (when implemented in user space).
User-Level Threads (ULT)
2. Kernel-Level Threads (KLT)
• Managed directly by the operating system kernel.
• The kernel scheduler assigns CPU time to each thread.
• If one thread blocks, others can still continue.
• Downside: Switching is slower compared to user threads, because it involves kernel mode operations.
• Examples: Windows threads, Linux threads (created with the clone() system call).
Analogy:
• User threads → Students organize group work themselves; the teacher (OS) sees it as one project.
• Kernel threads → The teacher assigns and manages tasks for each student directly.
4.4 What is Multiprogramming, Multitasking, and Multiprocessing?
1. Multiprogramming
Definition:
Multiprogramming is an operating system technique in which multiple programs are loaded into the main
memory at the same time, but the CPU executes only one program at a time. Whenever the currently
executing program waits for an I/O operation, the operating system immediately switches the CPU to
another program. This ensures that the CPU remains busy instead of staying idle.
Purpose:
To maximize CPU utilization.
To reduce CPU idle time.
To improve system throughput.
Example:
Suppose three programs (P1, P2, and P3) are loaded into memory. If P1 requests data from the hard disk,
the CPU immediately starts executing P2 instead of waiting for P1 to finish its I/O operation.
2. Multitasking
Definition:
Multitasking is an operating system technique that allows a user to run multiple tasks or applications
simultaneously. The CPU allocates a small amount of time (called a time slice or time quantum) to each task
and switches rapidly between them. Due to this high-speed switching, users feel that all applications are
running at the same time.
Purpose:
To improve user responsiveness.
To allow users to perform multiple activities simultaneously.
To provide a better user experience.
Example:
A user is editing a document, listening to music, downloading a file, and browsing the internet at the same
time on a laptop.
3. Multiprocessing
Definition:
Multiprocessing is an operating system technique in which two or more processors (or CPU cores) work
together to execute multiple processes simultaneously. Unlike multiprogramming and multitasking,
multiprocessing provides true parallel execution, resulting in higher speed and better overall performance.
Purpose:
To increase processing speed.
To improve system reliability.
To execute multiple processes in parallel.
Example:
A modern quad-core computer can simultaneously render a video, compile source code, run a web browser,
and play music, with different CPU cores handling different tasks.
Difference between all Multiprogramming, Multitasking, and Multiprocessing
Important Questions
1. Define Shell and Kernel. Differentiate between Shell and Kernel with suitable examples.
2. Explain the different types of Kernels with neat diagrams and examples.
3. What is a Process? Explain the Process Life Cycle with a neat Process State Transition
Diagram.
4. Explain the Process Memory Layout (Text, Data, Heap, and Stack) with a neat diagram.
5. What is a Process Control Block (PCB)? Draw and explain the structure and contents of PCB.
6. What is Context Switching? Explain the context switching process with a suitable diagram.
7. What are Threads? Explain the need, advantages, and applications of Threads in an Operating
System.
8. Differentiate between Single-threaded and Multi-threaded Processes with suitable examples.
9. Differentiate between User-Level Threads (ULT) and Kernel-Level Threads (KLT).
[Link] among Multiprogramming, Multitasking, and Multiprocessing with suitable
examples.