Q1. Define a Process.
Explain the various Process Elements and Attributes in
detail.
Answer:
A process is formally defined as a program in execution. While a program is a passive
entity (like a file stored on a disk), a process is an active, dynamic entity that has been
loaded into main memory and is actively competing for or using the processor. It is the
basic unit of work in a modern time-sharing system.
When a process is running, it comprises the program code (which may be shared
among multiple users), a set of data, and a variety of attributes that the OS uses to
control and track it. These elements include:
● Identifier (Process ID / PID): Every process is assigned a unique numerical
identifier upon creation. This acts as a primary key, allowing the OS to uniquely
distinguish it from all other processes currently residing in the system.
● State: A process is not always executing. The state attribute indicates the current
condition of the process at any given fraction of a second (e.g., whether it is
currently running on the CPU, waiting in a queue, or blocked waiting for a printer).
● Priority: In a multiprogramming environment, not all processes are equally
important. The priority attribute is a quantitative value used by the CPU scheduler
to determine which process should be granted processor time next. System-level
processes generally hold higher priority than user-level applications.
● Program Counter (PC): This is a crucial hardware register pointer. It holds the
exact memory address of the next instruction that the CPU needs to fetch and
execute for this specific process. When a process is interrupted, saving the PC is
critical so it can resume exactly where it left off.
● Memory Pointers: These are pointers that define the physical and logical
boundaries of the process within the main memory (RAM). They point to the code
segment, the data segment, and the stack, ensuring the OS can translate logical
addresses to physical addresses and prevent memory boundary violations.
● Context Data: This refers to the data currently residing in the processor's registers
while the process is executing. When a process transitions out of the running state,
this data must be saved so the execution context is preserved.
● I/O Status Information: A process frequently requires peripheral devices. This
attribute maintains a live, updated list of all outstanding I/O requests, the I/O
devices currently allocated to the process, and a list of all files the process
currently has open.
● Accounting Information: The OS must track resource consumption. This includes
the total amount of processor time consumed, clock time elapsed since creation,
time limits imposed, and the user account that initiated the process.
Q2. What is a Process Control Block (PCB)? Explain Context Switching and the role
of the Dispatcher.
Answer:
The Process Control Block (PCB)
Because the OS must manage multiple processes concurrently, it cannot memorize the
attributes of every process. Therefore, it maintains a dedicated, complex data structure
for every single process called the Process Control Block (PCB), sometimes referred to
as a Task Control Block.
The PCB acts as the central repository of information for a process. It contains all the
process elements discussed above (State, Program Counter, CPU Registers, Scheduling
Information, Memory-Management Information, and I/O Status). The PCB is created
when the process is born and is exclusively managed by the OS kernel to prevent
user-level interference. It allows the OS to pause a process, store its entire execution
context inside the PCB, and later reload it seamlessly.
The Dispatcher
While the CPU scheduler decides which process should run next, the dispatcher is the
actual module that gives control of the CPU to the selected process. The dispatcher's
responsibilities include:
1. Switching the context.
2. Switching the system hardware to user mode.
3. Jumping to the proper location in the user program (using the updated Program
Counter) to restart execution.
The time it takes for the dispatcher to stop one process and start another is a
critical metric called Dispatch Latency.
Context Switching
When the CPU switches to another process, the system must perform a context
switch. The OS saves the state of the old process into its PCB and reloads the saved
state from the PCB of the new process. Context-switch time is pure overhead; the
system performs no useful work during this time, and the speed is highly dependent on
hardware support
Exam Note: Context switching is considered pure overhead. The system performs no
useful computational work for user applications during this time. Therefore, modern OS
architectures and hardware are heavily optimized to execute context switches as
rapidly as possible.
Q3. Explain the Process Lifecycle using the Five-State Model and explain the
concept of Suspended Processes.
Answer:
As a process progresses through its lifecycle, it continuously changes its state based
on OS scheduling and I/O requirements.
The Five-State Process Model
A standard multiprogramming OS utilizes a five-state model to track process lifecycles:
1. New: The process is currently in the stage of being created by the OS. The OS is
allocating a PID, building the PCB, and setting up memory, but the program is not
yet loaded into the active ready queue.
2. Ready: The process has been fully loaded into main memory and possesses all the
resources it needs to execute, except for the CPU. It waits in the ready queue for
the short-term scheduler to select it.
3. Running: The CPU is actively fetching and executing the instructions of this
process. In a single-processor system, only one process can be in the running
state at any given microsecond.
4. Blocked (Waiting): The process cannot proceed further because it is waiting for a
specific external event to occur. This is typically an I/O operation (like reading from
a hard drive) or waiting for a synchronization signal. The process yields the CPU
because it cannot do any useful work until the event completes.
5. Exit (Terminated): The process has finished executing its final instruction or has
been forcefully aborted due to an error. The OS reclaims its memory and deletes
its PCB.
Suspended Processes and Swapping
In a practical system, the CPU executes instructions vastly faster than I/O devices can
retrieve data. This speed mismatch often results in a situation where all processes
residing in main memory (RAM) end up in the "Blocked" state, simultaneously waiting
for I/O.
Since RAM is expensive and limited, keeping blocked processes in RAM wastes valuable
space. To optimize memory utilization, the OS employs a technique called swapping. It
takes a completely blocked process and moves (swaps) its entire memory footprint
from RAM to the secondary storage (hard disk).
This introduces new states called Suspend States:
● Blocked/Suspend: The process has been swapped to the hard disk and is still
waiting for its I/O event to occur.
● Ready/Suspend: The I/O event the process was waiting for has finally completed.
However, the process is still residing on the hard disk. It is functionally ready to
execute, but the OS must first swap it back into main memory before the CPU can
run it.
Q4. Describe the various Process Scheduling Queues and the different types of
Schedulers.
Answer:
Process Scheduling Queues
To efficiently manage processes as they transition between states, the OS maintains
various scheduling queues. These are typically implemented as linked lists, containing
pointers to the first and last PCBs in the line.
1. Job Queue: This is the master administrative list containing the PCBs of every
single process currently existing in the system, regardless of their state.
2. Ready Queue: This queue contains the set of all processes that are residing in
main memory and are fully ready and waiting to execute. The short-term scheduler
selects the next process to run from this queue.
3. Device Queues: Because multiple processes may request access to the same I/O
device (e.g., a printer or a specific disk drive) simultaneously, each device has its
own dedicated queue. Processes waiting for a specific device wait in that device's
queue.
Types of Schedulers
The OS uses three distinct types of schedulers to move processes between these
queues:
1. Short-term Scheduler (CPU Scheduler): This scheduler makes the most
frequent decisions. It constantly monitors the Ready Queue and selects which
process should be allocated the CPU next. Because it executes every few
milliseconds, its primary requirement is extreme speed and efficiency to minimize
overhead.
2. Long-term Scheduler (Job Scheduler): This scheduler acts as the gatekeeper. It
selects which newly submitted processes should be brought from the disk into the
Ready Queue in main memory. It executes much less frequently (seconds or
minutes). Its primary goal is to control the degree of multiprogramming (the total
number of processes in memory) and to ensure a healthy mix of I/O-bound and
CPU-bound processes.
3. Medium-term Scheduler: This scheduler is responsible for the swapping
mechanism discussed earlier. If main memory becomes overly congested, it
removes processes from memory and suspends them to the disk, dynamically
reducing the degree of multiprogramming. Later, it brings them back to resume
execution.
Q5. Explain Advanced Queuing Algorithms: Multilevel Queue and Multilevel
Feedback Queue Scheduling.
Answer:
Standard scheduling algorithms (like First-Come-First-Serve) treat all processes
equally, which is inefficient in a diverse system containing system apps, interactive user
apps, and background batch jobs.
Multilevel Queue Scheduling
To handle diverse processes, the OS permanently partitions the singular Ready Queue
into several separate, distinct queues.
● Classification: Processes are classified and permanently assigned to a specific
queue upon creation based on their properties (e.g., memory size, process priority,
or process type). A common division is segregating foreground (interactive)
processes from background (batch) processes.
● Queue-Specific Algorithms: Each individual queue can employ its own
customized scheduling algorithm. For instance, the foreground queue might use
Round Robin for quick response times, while the background queue uses FCFS.
● Inter-Queue Scheduling: There must also be a mechanism to schedule CPU time
between the queues. This is often done using fixed-priority preemptive scheduling
(e.g., the foreground queue always has absolute priority over the background
queue).
● Drawback: The major flaw of a strict Multilevel Queue is Starvation. If high-priority
queues are continuously filled with processes, the processes in the lower-priority
background queues may never receive CPU time.
Multilevel Feedback Queue Scheduling
This algorithm is designed specifically to solve the starvation problem and provide
maximum flexibility. Unlike the static multilevel queue, a Multilevel Feedback Queue
allows a process to dynamically migrate between the various queues based on its
runtime behavior.
● The Aging Mechanism: The movement between queues is used to implement a
concept called "aging." If a process waits in a low-priority queue for an extended
period, the OS artificially "ages" it, upgrading it to a higher-priority queue to ensure
it eventually gets executed.
● CPU Hog Demotion: Conversely, if a process is placed in a high-priority queue but
consumes too much continuous CPU time (indicating it is a heavy computation job
rather than a quick interactive task), the OS will demote it to a lower-priority
queue.
● Example: A process enters the highest priority queue (Queue 0) which grants an
8-millisecond time slice. If the process does not complete its burst within 8ms, it is
preempted and pushed down to Queue 1 (which grants a 16ms slice). If it still does
not finish, it is demoted again to a standard FCFS background queue.
Q6. Define Threads. Differentiate between User-Level Threads (ULT) and
Kernel-Level Threads (KLT), and explain the Multithreading Models.
Answer:
Concept of a Thread
A thread is the most basic, fundamental unit of CPU utilization. While a traditional,
"heavyweight" process has only a single thread of execution, modern applications
require concurrent operations. A multithreaded process contains multiple threads
executing simultaneously within the same process environment.
● Resource Sharing: Threads are lightweight because they share the heavy
resources of their parent process. All threads within a process share the same
executable code section, data section, and OS resources (like open files and
network connections).
● Individual State: However, to execute independently, each individual thread must
maintain its own Thread ID, its own Program Counter (so it can be at a different
point in the code), its own CPU register set, and its own stack for local variables.
● Benefits: The primary benefits of multithreading include greatly enhanced
application responsiveness, highly efficient resource sharing, economical creation
(creating a thread is much faster than creating a full process), and the ability to
scale and run in parallel on multiprocessor architectures.
User-Level Threads (ULT) vs. Kernel-Level Threads (KLT)
1. User-Level Threads (ULT): These threads are implemented entirely in user space
via a thread library (e.g., POSIX Pthreads or Java threads). The operating system
kernel is completely unaware of their existence.
○ Advantages: ULTs are exceptionally fast to create and switch. Because the OS
is not involved, a thread switch does not require a hardware mode switch to
kernel mode, saving massive overhead. Furthermore, scheduling can be
tailored to the specific application.
○ Disadvantages: The major flaw is that if one single user thread executes a
blocking system call (such as requesting a file from a slow disk), the OS kernel,
seeing only the overall process, will block the entire process. All other healthy
threads within that process will be frozen. Also, they cannot truly run in parallel
on a multiprocessor because the OS only allocates one CPU core to the
process.
2. Kernel-Level Threads (KLT): These threads are directly supported, recognized,
and managed by the operating system kernel itself (e.g., Windows threads).
○ Advantages: Because the kernel is aware of every thread, it can schedule
multiple threads from the same process on multiple CPU cores simultaneously.
Most importantly, if one KLT blocks for I/O, the kernel can simply suspend that
specific thread and schedule another thread from the same process to keep
running.
○ Disadvantages: KLTs are significantly slower and more resource-intensive to
create and manage than ULTs. Every thread operation requires a system call,
forcing a context switch to kernel mode, which increases OS overhead.
Multithreading Models
To utilize threads, the system must map the user-level threads created by the
application to the kernel-level threads managed by the hardware. There are three
primary mapping models:
1. Many-to-One Model: Many user-level threads are mapped to a single, solitary
kernel thread. Thread management is highly efficient as it is done in user space.
However, because there is only one kernel connection, if one thread blocks, the
entire process blocks. True parallel execution on multiprocessors is impossible
2. .
3. One-to-One Model: Every individual user-level thread is strictly mapped to its
own dedicated kernel thread. This provides maximum concurrency, allowing
another thread to run if one blocks, and supports true parallel processing. The
drawback is heavy overhead, as creating a user thread forces the OS to expend
resources creating a corresponding kernel thread.
4.
5. Many-to-Many Model: This hybrid model multiplexes many user-level threads
onto a smaller or equal number of kernel threads. It provides the best balance: the
user can create as many lightweight threads as needed, while the OS provides a
sufficient pool of kernel threads to ensure that if one thread blocks, another kernel
thread is available to continue execution on multiple processors.
6.