0% found this document useful (0 votes)
12 views68 pages

Process Management in Operating Systems

Module 2 of the Advanced Operating System course focuses on Process Management, explaining the distinction between a program and a process, the role of process management in an operating system, and the lifecycle of processes. It details characteristics of processes, their states, and operations such as creation, scheduling, and termination. Additionally, it covers resource allocation, inter-process communication, and synchronization to ensure efficient operation of multiple processes.

Uploaded by

ks.sanchula
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views68 pages

Process Management in Operating Systems

Module 2 of the Advanced Operating System course focuses on Process Management, explaining the distinction between a program and a process, the role of process management in an operating system, and the lifecycle of processes. It details characteristics of processes, their states, and operations such as creation, scheduling, and termination. Additionally, it covers resource allocation, inter-process communication, and synchronization to ensure efficient operation of multiple processes.

Uploaded by

ks.sanchula
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Operating System Module 2

Module 2 : Process Management


• A process is a program in execution.
• It means the program is actively running on the computer.

i) Program vs Process:
• A program is a set of instructions (code) that is stored on the disk. It is
passive, meaning it doesn’t do anything on its own.
• A process is the running instance of a program. It is active, meaning it is
performing tasks when executed.
Difference between process and the program

S. No Process Program
1 A process is actively Program is a set of
running software or a instructions which are
computer code. Any executed when the
procedure must be certain task is allowed
carried out in a precise to complete that certain
order. An entity that task
helps in describing the
fundamental work unit
that must be
implemented in any
system is referred to as
a process
2 Process is Dynamic in Program is Static in
Nature Nature
3 Process is an Active in Program is Passive in
Nature Nature
4 Process is created Program is already
during the execution existed in the memory
and it is loaded directly and it is present in the
into the main memory secondary memory.
Program is already
existed in the memory

1
Advanced Operating System Module 2

and it is present in the


secondary
5 Process has its own Program does not have
control system known any control system. It is
as Process Control just called when
Block specified and it
executes the whole
program when called
6 Process changes from Program cannot be
time to time by itself changed on its own. It
Program cannot be must be changed by the
changed on programmer.
7 A process needs extra Program is basically
data in addition to the divided into two parts.
program data needed One is Code part and
for management and the other part is data
execution. part.
8 Processes have A program just needs
significant resource memory space to store
demands; they require its instructions; no
resources like Memory further resources are
Addresses, Central needed.
Processing Unit, Input
or Output until their
presence or existence
in the Operating
System.

Example:
• When you write a program in C or C++, it is saved as a file. This file is the
program.
• When you compile this program, the compiler creates an executable file
(like a .exe file). This executable file is still just a program.
• When you run the .exe file, it becomes a process because it is now being
executed by the system.

2
Advanced Operating System Module 2

Multiple Processes from One Program:


• A single program can create many processes. For example, if you open
the same .exe file multiple times, you create multiple instances of that
program running at the same time (multiple processes).

Active vs Passive:
• Active Entity: A process is active, meaning it consumes CPU time,
memory, and other resources to perform its tasks.
• Passive Entity: A program is passive, meaning it is just stored data until it
is executed.

Practical Example:
• Think of a text editor like Notepad on your computer. Notepad itself is a
program stored on your hard drive.
• When you open Notepad, it becomes a process because it is now
running on your computer.
• If you open Notepad multiple times, each time you open it, a new
process is created.

ii) What is Process Management?


• Process management is an important part of an operating system (OS).
It manages all the programs that are currently running on your
computer, known as processes.

Role of Process Management:


• The operating system controls how processes run, stops them when
needed, and decides which processes get priority (more attention).
• This helps the computer run smoothly by handling all active processes
efficiently.

Tasks of the Operating System:


• Starting and stopping processes (running or closing programs).

3
Advanced Operating System Module 2

• Scheduling processes to decide when and which process should run.


• Managing resources like CPU, memory, and input/output devices so that
processes don’t conflict with each other.

Deadlock Prevention:
• The OS also ensures that the processes don’t block each other (known
as deadlock), which could freeze the system.

Inter-Process Communication (IPC):


• The OS helps processes communicate with each other when needed,
like when a web browser and a download manager need to share data.

Process Synchronization:
• The OS ensures that processes work in sync with each other, especially
when multiple processes are sharing data or resources.

Efficient Resource Allocation:


• The OS manages system resources (CPU, memory, etc.) to maximize
performance and ensure that every process gets enough resources
without conflicts.

System Responsiveness:
• Good process management allows multiple applications to run at the
same time, making the computer more responsive and efficient.

Example:
• When you are running a web browser, music player, and a game at the
same time, process management ensures they all run smoothly by
giving each one the right amount of system resources.

4
Advanced Operating System Module 2

iii) Process in Memory


When a program runs and becomes a process, it is loaded into the computer's
memory (RAM). In memory, a process is divided into different parts, each
having a specific purpose. Here's what it looks like:

1. Text Section (Code Section):

• This part contains the instructions or code of the program. It tells the
computer what tasks to perform.
• It also includes the Program Counter (PC), which keeps track of which
instruction is being executed at any moment.

5
Advanced Operating System Module 2

2. Stack:

• The stack holds temporary data. This includes:


1. Function parameters (values passed to functions),
2. Return addresses (where the program should go after a function
ends),
3. Local variables (variables created inside functions).
• The stack grows and shrinks as functions are called and completed.

3. Data Section:

• This part holds global variables. These are variables that can be
accessed by the entire program, not just a single function.
4. Heap Section:

• The heap is used for dynamic memory allocation. This means that
memory is allocated to the process while it's running.
• For example, if the program needs more memory for a new task, it can
request more from the heap during its execution.

iv) Characteristics of a Process


Every process (a program running on your computer) has certain features or
attributes that help the operating system manage it. These attributes are
stored in a special structure called the Process Control Block (PCB).
Here are the key characteristics of a process:
1. Process ID:
• Each process has a unique number called the Process ID (PID),
assigned by the operating system. This helps the OS identify and
manage the process.

6
Advanced Operating System Module 2

2. Process State:
• The process can be in different states such as:
➢ Ready: The process is ready to run but waiting for the CPU.
➢ Running: The process is currently being executed by the CPU.
➢ Waiting: The process is waiting for some event to happen (like
input from the user).
3. CPU Registers:
• These are small memory locations inside the CPU. Important data
like the Program Counter (PC) is stored here.
• The PC keeps track of the next instruction to execute. When a
process is paused (swapped out), the CPU saves this information, so
it can resume the process later from the same point.
4. Accounts Information:
• This includes details like:
➢ How much CPU time the process has used.
➢ Time limits on the process.
➢ The Execution ID of the process (who started it).
5. I/O Status Information:
• Information about the input/output devices used by the process. For
example, the files the process has opened or devices (like printers) it
is using.
6. CPU Scheduling Information:
• This includes the priority of the process. For example:
➢ Some processes have higher priority, meaning they will be
executed before lower-priority processes.
➢ A shorter process might get high priority in certain scheduling
methods (like Shortest Job First scheduling).
Process Control Block (PCB):
❖ All the above information is stored in a Process Control Block (PCB).
❖ Each process has its own PCB, which contains everything the OS needs
to manage the process.

7
Advanced Operating System Module 2

v) States of a Process
When a program becomes a process (a running program), it goes through
different stages or states during its life cycle.

1. New:
• This is the starting state. When a process is created, it starts in the
New state.
• Example: When you double-click to open an application, the process
is in the “New” state while it’s being created.
2. Ready:
• After a process is created, it moves to the Ready state. This means
the process is ready to run but is waiting for the CPU to be free.
• Example: Your application is loaded and ready to run, but the CPU is
still busy with other tasks, so it is waiting for its turn.

8
Advanced Operating System Module 2

3. Running:
• The process moves to the Running state when it is currently being
executed by the CPU.
• Only one process can be in the Running state at a time on a single
processor system.
• Example: When you start typing in your text editor, the process is
actively running in the CPU.
4. Wait (or Block):
• The process enters the Wait or Blocked state when it needs to wait
for some input/output operation, like reading a file or waiting for user
input.
• Example: When you’re downloading a file, the process might enter
the Wait state until the file finishes downloading.
5. Complete (or Terminated):
• When the process finishes its task or is terminated by the system or
user, it moves to the Complete (or Terminated) state.
• Example: After you close an application, the process enters the
“Terminated” state because its execution is finished.
6. Suspended Ready:
• When the Ready Queue (the list of processes waiting to run)
becomes too full, some processes are moved to the Suspended
Ready state.
• This means they are ready to run but have been temporarily moved
out of the main memory to free up space.
• Example: If many programs are open on your computer, some might
be moved to the suspended state to free up memory for more
important processes.

9
Advanced Operating System Module 2

7. Suspended Block:
• If a process is in the Wait state (waiting for an I/O operation) but the
Wait Queue (list of waiting processes) becomes full, the process is
moved to the Suspended Block state.
• This means it is waiting for an event (like a file to be loaded) but is
suspended to free up memory space.
• Example: If multiple background tasks are waiting for user input,
some might be suspended to free up system resources.

vi) Process Operations


Process operations in an operating system involve several key steps that
manage the lifecycle of processes. The operations on process in OS ensure
efficient utilization of system resources, multitasking, and a responsive
computing environment. The primary process operations in OS include:
a) Process Creation
b) Process Scheduling
c) Context Switching
d) Process Execution
e) Inter-Process Communication (IPC)
f) Process Termination
g) Process Synchronization
h) Process State Management
i) Process Priority Management
j) Process Accounting and Monitoring

a) Creating

• Process creation is a fundamental operation within an operating system


that involves the creation and initialization of a new process.
• The process operation in OS is crucial for enabling multitasking,
resource allocation, and concurrent execution of tasks.

10
Advanced Operating System Module 2

• The process creation operation in OS typically follows a series of steps


which are as follows:

11
Advanced Operating System Module 2

1. Request:
• The process of creation begins with a request from a user or a system
component, such as an application or the operating system itself, to
start a new process.
2. Allocating Resources:
• The operating system allocates necessary resources for the new
process, including memory space, a unique process identifier (PID),
a process control block (PCB), and other essential data structures.
3. Loading Program Code:
• The program code and data associated with the process are loaded
into the allocated memory space.
4. Setting Up Execution Environment:
• The OS sets up the initial execution environment for the process.
5. Initialization:
• Any initializations required for the process are performed at this
stage. This might involve initializing variables, setting default values,
and preparing the process for execution.
6. Process State:
• After the necessary setup, the new process is typically in a “ready” or
“waiting” state, indicating that it is prepared for execution but hasn’t
started running yet.
b) Dispatching/Scheduling

• Dispatching/scheduling, is a crucial operation within an operating


system that involves the selection of the next process to execute on
the central processing unit (CPU).
• This operation is a key component of process management and is
essential for efficient multitasking and resource allocation.
• The dispatching operation encompasses the following key steps:

12
Advanced Operating System Module 2

1. Process Selection:

• The dispatching operation selects a process from the pool of ready-


to-execute processes. The selection criteria may include factors
such as process priority, execution history, and the scheduling
algorithm employed by the OS.

2. Context Switching:

• Before executing the selected process, the operating system


performs a context switch. This involves saving the state of the
currently running process, including the program counter, CPU
registers, and other relevant information, into the process control
block (PCB).
3. Loading New Process:

• Once the context switch is complete, the OS loads the saved state of
the selected process from its PCB. This includes restoring the
program counter and other CPU registers to the values they had
when the process was last preempted or voluntarily yielded to the
CPU.
4. Execution:

• The CPU begins executing the instructions of the selected process.


The process advances through its program logic, utilizing system
resources such as memory, I/O devices, and external data.
5. Timer Interrupts and Preemption:

• During process execution, timer interrupts are set at regular


intervals. When a timer interrupt occurs, the currently running
process is preempted, and the CPU returns control to the operating
system.

13
Advanced Operating System Module 2

6. Scheduling Algorithms:

• The dispatching operation relies on scheduling algorithms that


determine the order and duration of process execution.
[Link] Allocation:

• The dispatching operation is responsible for allocating CPU time to


processes based on the scheduling algorithm and their priority. This
ensures that high-priority or time-sensitive tasks receive appropriate
attention.
c) Blocking

• In an operating system, a blocking operation refers to a situation


where a process in OS is temporarily suspended or “blocked” from
executing further instructions until a specific event or condition
occurs.
• This event typically involves waiting for a particular resource or
condition to become available before the process can proceed.
• Blocking operations are common in scenarios where processes need
to interact with external resources, such as input/output (I/O)
devices, files, or other processes.
• When a process operation in OS initiates a blocking operation, it
enters a state known as “blocked” or “waiting.”
• The operating system removes the process from the CPU’s execution
queue and places it in a waiting queue associated with the resource
it is waiting for.
• The process remains in this state until the resource becomes
available or the condition is satisfied.
• Blocking operations is crucial for efficient resource management and
coordination among processes.
• They prevent processes from monopolizing system resources while
waiting for external events, enabling the operating system to
schedule other processes for execution.

14
Advanced Operating System Module 2

• Common examples of blocking operations include:


1. I/O Operations:
• When a process requests data from an I/O device (such as
reading data from a disk or receiving input from a keyboard), it
may be blocked until the requested data is ready.
2. Synchronization:
1. Processes often wait for synchronization primitives like
semaphores to achieve mutual exclusion or coordinate their
activities.
3. Inter-Process Communication:
• Processes waiting for messages or data from other processes
through mechanisms like message queues or pipes may enter
a blocked state.
4. Resource Allocation:
• Processes requesting system resources, such as memory or
network connections, may be blocked until the resources are
allocated.

15
Advanced Operating System Module 2

d) Preemption

• Preemption in an operating system refers to the act of temporarily


interrupting the execution of a currently running process to allocate the
CPU to another process.
• This interruption is typically triggered by a higher-priority process
becoming available for execution or by the expiration of a time slice
assigned to the currently running process in a time-sharing
environment.

16
Advanced Operating System Module 2

Key aspects of preemption include:


[Link]-Based Preemption:

• Processes with higher priorities are given preference in execution. When


a higher-priority process becomes available, the OS may preempt the
currently running process to allow the higher-priority process to
execute.
2. Time Sharing:

• In a time-sharing or multitasking environment, processes are allocated


small time slices (quantum) of CPU time. When a time slice expires, the
currently running process is preempted, and the OS selects the next
process to run.
3. Interrupt-Driven Preemption:

• Hardware or software interrupts can trigger preemption. For example,


an interrupt generated by a hardware device or a system call request
from a process may cause the OS to preempt the current process and
handle the interrupt.
[Link] and Responsiveness:

• Preemption ensures that no process is unfairly blocked from accessing


CPU time. It guarantees that even low-priority processes get a chance to
execute, preventing starvation.
5. Real-Time Systems:

• Preemption is crucial in real-time systems, where tasks have strict


timing requirements. If a higher-priority real-time task becomes ready to
run, the OS must preempt lower-priority tasks to ensure timely
execution.

17
Advanced Operating System Module 2

e) Termination of Process

• Termination of a process operation in an operating system refers to the


orderly and controlled cessation of a running process’s execution.
• Process termination occurs when a process has completed its intended
task, when it is no longer needed, or when an error or exception occurs.
• This operation involves several steps to ensure proper cleanup and
resource reclamation:
1. Exit Status:
• When a process terminates, it typically returns an exit status or
code that indicates the outcome of its execution. This status
provides information about whether the process was completed
successfully or encountered an error.
2. Resource Deallocation:
• The OS releases the resources allocated to the process, including
memory, file handles, open sockets, and other system resources.
This prevents resource leaks and ensures efficient utilization of
system components.
3. File Cleanup:
• If the process has opened files or created temporary files, the OS
ensures that these files are properly closed and removed,
preventing data corruption and freeing up storage space.
4. Parent Process Notification:
• In most cases, the parent process (the process that created the
terminating process) needs to be informed of the termination and
the exit status.
5. Process Control Block Update:
• The OS updates the process control block (PCB) of the terminated
process, marking it as “terminated” and removing it from the list
of active processes.

18
Advanced Operating System Module 2

6. Reclamation of System Resources:


• The OS updates its data structures and internal tables to reflect
the availability of system resources that were used by the
terminated process.

vii) Process Control Block


• A Process Control Block (PCB) is a data structure used by the Operating
System to manage and keep track of processes. It helps in organizing
how processes are carried out and ensures proper use of memory and
system resources.
• The PCB stores important information about a process, such as its
current state, program counter (which shows the next instruction),
memory usage, open files, and CPU scheduling details.
• When a new process is created, a PCB is made for it.
• The purpose of the PCB is to help the Operating System keep track of all
the processes and manage them effectively. By monitoring each
process through the PCB, the OS can allocate resources, like CPU time
and memory, to run processes efficiently.
• Every process gets its own PCB, which stores all the key details the OS
needs to manage and execute it properly.

19
Advanced Operating System Module 2

1. Process State: The PCB stores the current state of a process, which helps
the Operating System manage and schedule it. A process can be in
different states like “running,” “waiting,” “ready,” or “terminated.”
2. Process ID: Every process gets a unique number called a Process ID when
it’s created. This ID helps the Operating System keep track of and identify
different processes.

20
Advanced Operating System Module 2

3. Program Counter: When a process is interrupted, the Program Counter


saves the last instruction that was executed. This helps the process
resume from where it left off when it starts running again.
4. CPU Registers: The PCB keeps a copy of the CPU registers (which hold
important data for the process) so that the process can continue correctly
when it resumes.
5. Memory Information: The PCB stores details about how much memory is
allocated to the process and where it starts. This helps in managing
memory efficiently.
6. Process Scheduling Information: The PCB holds information like the
priority of the process or the scheduling method. This helps the OS decide
the order in which processes should run.
7. Accounting Information: The PCB stores data like how much CPU time or
memory the process has used. This helps the OS track the performance of
the process.
Operations Carried out through PCB
1. Process Scheduling: The OS uses information like process priority,
process state, and resources used to decide when a process will run.
The scheduler looks at this information to figure out the best time for the
process to be executed.
2. Multitasking: The OS can run multiple processes at the same time by
managing resources, scheduling processes, and making sure processes
work together smoothly.
3. Context Switching: When the OS switches from one process to
another, it saves the current process’s state in the CPU register and
stores a copy in the PCB. When the CPU returns to that process, it
retrieves the saved state from the PCB so the process can continue
from where it left off.

21
Advanced Operating System Module 2

4. Resource Sharing: The PCB stores information about the resources a


process is using, like open files and memory. This helps the OS allow
other processes to share those resources, making sure everything runs
smoothly.
Advantages of Using a Process Control Block (PCB):
1. The PCB stores all the important information about a process, allowing the
Operating System to handle tasks like process scheduling and context
switching.
2. With the help of the PCB, the Operating System can schedule processes
efficiently and make sure CPU resources are used properly.
3. The PCB helps in using resources efficiently by storing details about how
each process uses resources, allowing for better sharing of resources
between processes.
4. Information like CPU registers and stack pointers in the PCB helps the OS
save the process state, making context switching easier.
5. The PCB can help synchronize processes by storing details like the waiting
status of a process and the resources it needs, helping processes work
together smoothly.
6. The PCB is used to schedule which process runs next by tracking the state
and resource usage of each process.

Disadvantages of Using a Process Control Block (PCB):


1. Storing a PCB for each process uses a lot of memory, especially when there
are many processes running at the same time.
2. Using PCBs adds complexity to the system, making it harder to scale up or
manage more processes easily.
3. Managing PCBs for each process adds extra work for the Operating System,
which can slow down system performance.
4. The PCB adds complexity to the Operating System, making it more difficult
to develop, maintain, and ensure stability when managing multiple
processes.

22
Advanced Operating System Module 2

viii) What is Process Scheduling?


Process scheduling is how the Operating System decides which process runs
on the CPU at any given time. It involves two main actions:
1. Stopping the Current Process: When a process is running, the Operating
System may need to pause it to allow another process to run. This is
called removing the running process from the CPU.
2. Selecting a New Process: The Operating System chooses another
process to run based on a specific strategy. This strategy could depend
on factors like priority, waiting time, or fairness.
Why is Process Scheduling Important?

• Process scheduling is crucial for Multiprogramming Operating Systems.


These systems can have multiple processes loaded into memory
simultaneously. This means that more than one process is ready to run,
and the Operating System must manage which one gets to use the CPU
at any time.
• Using a technique called time multiplexing, the CPU shares its time
between different processes. This allows for efficient use of CPU
resources, ensuring that no process is left waiting too long and that the
system runs smoothly.
• In simple terms, process scheduling is like managing a line of people
waiting for a turn at a counter. The Operating System decides who gets
to use the CPU based on certain rules, making sure everyone gets a
chance to run and that the system operates efficiently.

23
Advanced Operating System Module 2

Categories of Scheduling
Scheduling is divided into two main categories:

1. Non-Preemptive:
• In this type, a process cannot be interrupted once it starts
running. It will finish its task before the Operating System can
take its resources.
• When the process finishes, it may switch to a waiting state, at
which point resources can be reassigned.
2. Preemptive:
• Here, the Operating System gives a process resources for a set
amount of time.
• If a higher-priority process needs to run, the OS can interrupt
the current process and switch it back to a ready state. This
allows the OS to prioritize more important tasks.

24
Advanced Operating System Module 2

Types of Process Schedulers


1. Long-Term or Job Scheduler:
• This scheduler brings new processes into the ready state and
controls how many processes are in the ready state at once.
• It aims to balance I/O-bound processes (which spend a lot of time
on input/output) and CPU-bound processes (which use the CPU a
lot). This balance helps improve overall efficiency, especially in
batch-processing systems.
2. Short-Term or CPU Scheduler:
• This scheduler selects one process from the ready state to run on
the CPU. It doesn’t actually start the process; that’s done by
another part called the dispatcher.
• The short-term scheduler uses various algorithms to make sure
no process gets stuck waiting too long.

25
Advanced Operating System Module 2

3. Medium-Term Scheduler:
• This scheduler manages suspending and resuming processes.
• It moves processes between main memory and disk when needed,
which helps manage memory use and maintain a balance between
I/O and CPU-bound processes.
• It can reduce the number of processes running at once to keep things
efficient.
Some Other Schedulers
1. I/O Schedulers:

• These manage how I/O operations (like reading from or writing to disks)
are handled.
• They use different methods, like First-Come, First-Served (FCFS) or
Round Robin (RR), to decide the order of operations.
2. Real-Time Schedulers:

• In systems that need to complete tasks on time, real-time schedulers


prioritize tasks to make sure critical operations finish within a set
deadline.
• They use methods like Earliest Deadline First (EDF) or Rate Monotonic
(RM) to schedule tasks effectively.

ix) Scheduling Queues

• Scheduling Queues are data structures used by an operating system to


manage the execution of processes or threads.
• They help organize processes based on their state and scheduling
needs, allowing the system to determine which process to run next.

26
Advanced Operating System Module 2

Types of Scheduling Queues


Scheduling queues are important for managing how processes or threads are
executed in computer systems. Each type of queue serves a specific purpose
based on the situation and follows a CPU scheduling algorithm.
1. Job Queue

• This queue holds all the processes or jobs waiting to be processed.


• For example, in a program like PASW Statistics 18, when a job is
created, it goes into the job queue until it’s ready to be processed.
• Characteristics:
➢ Contains all submitted jobs.
➢ Processes wait here until they can start execution.
➢ It is the initial storage for new jobs.
2. Ready Queue

• This queue contains processes that are ready to run but are waiting for
CPU time.
• When a process starts, it joins the ready queue until the CPU is free to
execute it.
• Characteristics:
➢ Uses a First-In-First-Out (FIFO) method, meaning the first process to
enter the queue is the first to get CPU time.
➢ Processes are chosen for execution based on scheduling algorithms
like First-Come, First-Served (FCFS), Shortest Job First (SJF), or
Priority Scheduling.

3. Waiting Queue

• This queue holds processes waiting for a specific event, like an I/O
operation (e.g., waiting for data from a disk).
• A process remains here until the resource it needs becomes available.
• Characteristics:

27
Advanced Operating System Module 2

➢ Contains processes waiting for resources that aren’t currently


available.
➢ Once the resource is free, the process moves back to the ready
queue.
4. Priority Queue

• In this queue, processes are prioritized. Higher priority processes can


interrupt lower priority ones.
• For example, an urgent task will get CPU time before a less urgent one.
• Characteristics:
➢ Processes are prioritized based on urgency.
➢ Higher priority jobs are run first, even if lower priority ones are still
running.
5. Round-Robin Queue

• This scheduling method gives each process a small, fixed amount of


CPU time called a time quantum.
• When a process runs out of its time, it goes to the back of the queue.
• For example, if a process has 10 milliseconds to run, it will go back to
the end of the queue after using that time.
• Characteristics:
➢ Ensures every process gets a fair chance at using the CPU.
➢ Works well in time-sharing systems, where multiple users interact
with the system at the same time.
6. Multi-Level Queue

• This queue has several levels, each with different priority levels.
• Processes are assigned to a specific queue based on their importance.
• For example, high-priority tasks like system updates might go into one
queue, while lower-priority tasks like background processes go into
another.
• Characteristics:

28
Advanced Operating System Module 2

➢ Multiple queues are organized by priority.


➢ Different scheduling algorithms can be used for each queue,
allowing flexible management of processes.

x) Context Switching

• Context switching is like pausing one task and switching to another on a


computer.
• When a process (a running program) is using the CPU (the computer's
brain), the operating system can temporarily stop it to let another
process run. This way, many processes can share the CPU efficiently.
Example of Context Switching :

• Imagine there are several processes waiting to use the CPU.


• When the CPU is busy with one process, it might need to switch to
another process that is more urgent.
• To do this, the system saves the current process's state (like where it left
off) and then loads the state of the new process to start running it. This
saving and loading take some time, known as context switch time,
which is just the overhead that doesn’t accomplish any useful work.

29
Advanced Operating System Module 2

Need for Context Switching

• Context switching is essential because:


1. It allows multiple processes to share one CPU.
2. It ensures that each process can resume exactly where it left off.
3. It helps the operating system manage resources effectively.
Triggers for Context Switching
Context switching can happen for several reasons:
1. Interrupts: If something needs immediate attention (like reading data
from a disk), the CPU will switch to handle it.

30
Advanced Operating System Module 2

2. Multitasking: The operating system can switch tasks so that all


processes get a chance to run.
3. User/Kernel Switch: This happens when the system needs to switch
between user mode (normal tasks) and kernel mode (system-level
tasks).
Process Control Block (PCB)

• The Process Control Block (PCB) is like a detailed file that holds all the
information about a process. It includes details such as:
1. The process's current state (running, waiting, etc.).
2. The CPU registers (the tiny memory spots where current data is
held).
3. Memory management information.
4. Process identification.
5. When a process is created, the operating system sets up a PCB to
keep track of everything related to that process.
Steps in Context Switching

When context switching happens, these steps generally occur:


1. Save the Current Process State: The system saves where the current
process is at so it can return to it later.
2. Store Information in PCB: All necessary information about the current
process is recorded in its PCB.

3. Select a New Process: The system looks for another process in the
waiting list that is ready to run.
4. Load the New Process State: The system retrieves the saved state of
the new process from its PCB and starts running it.
This process helps keep everything organized and allows the computer to
handle many tasks at once efficiently.

31
Advanced Operating System Module 2

xi) Inter Process Communication (IPC)

• Interprocess Communication (IPC) is a method that allows different


processes (programs) running on the same computer to exchange data
and communicate with each other.
• IPC helps multiple programs to function together, speeding up tasks
and ensuring that they are performed correctly and in order.

Why is IPC Necessary?

• IPC is vital because:

1. Speeds up execution: Multiple programs can run at the same time,


sharing data, and this makes tasks finish faster.
2. Ensures correct order: IPC ensures that tasks are completed in the
right sequence, helping avoid errors or confusion between programs.
How Operating Systems Use IPC

• Operating systems rely on IPC to:


➢ Exchange data: Communicate with components like the keyboard,
mouse, and graphical user interface (GUI).
▪ For example, pressing a key sends data from the keyboard
to the operating system.
➢ Run multiple programs: IPC allows multiple programs (like a web
browser and a word processor) to run simultaneously and share
information efficiently.
▪ For instance, a system uses IPC to inform the windowing
system about the status of windows on the screen.
Real-Life Example of IPC

• Imagine a large company where different teams (like sales, finance,


and tech) work together.

32
Advanced Operating System Module 2

• To finish a project quickly, they need to communicate and share


information. If these teams work separately without sharing data, the
project will take longer.
• In the same way, an operating system uses IPC to enable different
programs to share data and work together more efficiently, improving
overall performance.

Advantages of IPC
1. Efficient communication: Different applications can communicate
and exchange data without interfering with each other.
2. Glitch-free data sharing: Information can be shared smoothly
between processes.
3. Simplifies maintenance and debugging: Since programs are divided
into smaller parts that work independently, it’s easier to identify and fix
errors.
4. Multitasking: Users can perform multiple tasks (like listening to
music, writing code, and editing documents) at the same time.
5. Parallel task execution: Tasks can be divided and handled by
different processors, improving speed and efficiency.
Disadvantages of IPC
1. Shared memory issues: Processes using shared memory need to
ensure they don’t write to the same memory location at the same time,
which can lead to data corruption.
2. Synchronization and protection: Shared storage models can cause
synchronization issues, which require careful management to prevent
data loss or conflicts.
3. Slower than direct function calls: Communicating through IPC is
generally slower compared to calling a function directly within the same
program.

33
Advanced Operating System Module 2

How IPC Works

• IPC works when:

1. A program sends a message to the operating system,


requesting data or performing a task.
2. The operating system forwards the message to a specific IPC
mechanism (like shared memory or message passing).
3. The IPC mechanism processes the message and sends a
response back to the program.
▪ Example: Suppose you are editing a document while
playing music. The music player and the document editor
are different processes running in parallel.
▪ IPC allows the operating system to manage them both,
ensuring the music plays without interruption while you
type.
▪ The system shares resources (like CPU time and memory)
between these processes, ensuring they run smoothly.

IPC Mechanisms
1. Shared Memory: Different programs access the same portion of
memory to exchange data.
2. Message Passing: Programs send messages to each other through
the operating system, which helps in communication between
processes that don’t share memory.
1. Shared Memory

• The shared memory system is a fundamental method for inter-process


communication (IPC).
• This system allows different processes to communicate by accessing a
specific region of memory that is shared between them.

34
Advanced Operating System Module 2

• The concept is that one process creates a shared memory region, and
another process can access this region to exchange data.
• Key Features:
➢ Fastest IPC method: Shared memory is the fastest way for processes
to communicate since it avoids the need for the operating system to
mediate each exchange, like in other IPC methods (e.g., message
passing).
➢ Direct communication: Processes communicate directly by reading
and writing to the shared memory space.
How it Works: Step-by-Step Explanation

Let’s walk through an example of two processes, P1 and P2, exchanging data:
Step 1: Process P1 Creates Shared Memory

• P1 wants to share data with P2.


• To begin, P1 creates a shared memory region in its address space (its
own memory).

35
Advanced Operating System Module 2

• P1 stores the data it wants to share in this shared memory region.

Step 2: Process P2 Attaches to P1’s Shared Memory

• P2 needs the information stored by P1.


• To access this, P2 attaches itself to the shared memory region created
by P1. This means P2 connects to the memory region where P1 has
stored the data.
• P2 can now read the data from P1’s shared memory.

Step 3: Data Exchange Between P1 and P2

• Both processes, P1 and P2, can now exchange information by reading


from or writing to the shared memory region.
• For example, P2 might write a response back into the shared memory
for P1 to read.

Example
Imagine a simple program where P1 is a sensor process that collects
temperature data and P2 is a monitor process that displays the data to users:
1. P1 (Sensor Process): Collects temperature data (e.g., 75°C) and stores it
in a shared memory region.
2. P2 (Monitor Process): Attaches to the shared memory and reads the
temperature data to display it on the screen.
The two processes continue exchanging information using the shared
memory, making the system fast and efficient.
Advantages of Shared Memory
1. Speed:

• Shared memory is the fastest form of inter-process communication


since data is directly read and written without involving the operating
system in each step.

36
Advanced Operating System Module 2

2. Concurrent Access:

• Multiple processes can access and share the same data


simultaneously, allowing for efficient parallel processing.
3. Improved System Performance:

• Shared memory increases the computation speed by dividing large


tasks into smaller sub-tasks that can run in parallel.

4. Modularity:

• Different processes can work independently on various sub-tasks while


using shared memory to exchange data, leading to better modular
design.
5. Multitasking:

• Users can run multiple processes simultaneously, each sharing data as


needed, leading to better utilization of system resources.
2. Message passing

• Message passing is a mechanism that allows processes to


communicate and synchronize their actions without sharing the same
memory space. This is particularly useful in distributed systems, where
processes might run on different machines.
Example:

• Imagine a chat application where users send and receive messages.


Here, the processes running on different machines need to exchange
messages without directly accessing each other’s memory.
• Let’s break down message passing step by step:
• Step-by-Step Process of Message Passing

Step 1: Message passing involves two basic operations:

37
Advanced Operating System Module 2

➢ Send message: A process sends a message to another process.


➢ Receive message: A process receives the message sent by the other
process.
➢ Messages can be either of fixed size or variable size.
Step 2: For fixed-size messages, the system-level implementation is
straightforward, but it can make the programming more complex
because the programmer needs to manage the message content strictly
according to the size limit.
Step 3: For variable-size messages, more system-level implementation
is needed to handle different message sizes. However, this makes it
easier for programmers to manage message content because they are
not restricted by a fixed size.
Step 4: When two processes, say P1 and P2, need to communicate,
they send and receive messages from each other. A communication link
is established between the two processes to allow message transfer.
Step 5: The communication link can be implemented in various ways,
and the system provides two key operations:
➢ Send(message): Allows one process to send a message.
➢ Receive(message): Allows the other process to receive the message.
Characteristics of Message Passing

The main characteristics of message passing are:

• Used for communication: It’s primarily used to enable communication


between processes.
• Suitable for distributed environments: Processes can communicate
even if they are running on different machines connected by a network.

38
Advanced Operating System Module 2

• No code required for synchronization: The system’s message-passing


facility automatically provides communication and synchronization
between processes.
• Time-consuming process: Message passing is slower because it
involves system calls to the operating system’s kernel.
• Useful for small data exchange: It is best suited for sharing small
amounts of data, where the risk of conflicts is minimal.
• Slower than shared memory: Message passing tends to be slower
compared to shared memory communication because it takes time to
set up connections and transfer data.
Advantages of Message Passing Model

• Easier to implement: The message-passing model is simpler to


implement compared to shared memory, as it avoids complex memory
management.
• Suitable for parallel systems: Message passing works well in parallel
hardware, even when there are delays in communication between
processes.
Disadvantages of Message Passing Model

• Slower communication: Message passing is slower than shared


memory because of the time taken to establish connections and send
data between processes.
Example

• Imagine two chat users, User1 and User2, who are chatting online. The
chat application uses message passing for communication.
• When User1 sends a message (send operation), the chat server receives
it and forwards it to User2 (receive operation).
• The communication between User1 and User2 happens without them
directly accessing each other’s devices (no shared memory).

39
Advanced Operating System Module 2

• In this scenario:
o The send message is the text User1 sends.
o The receive message is what User2 gets.
o The communication link is the connection between the two users
established by the chat server.

xii) Multithreaded programming


What is a Thread?

• Thread is a single sequence or flow of execution within a program. You


can think of it like a mini-task that can run within a larger program
(called a process). Each thread has:
➢ Its program counter, which tells it what instruction to execute next.
➢ Its own registers to store the current data it’s working with.
➢ Its own stack, which contains the history of instructions it has
executed.
• In simple terms, a thread is like a worker in a factory (the process) that
performs tasks. While there can be many workers (threads) working
simultaneously in a factory, they all share the same materials and tools
(memory and resources).
Characteristics of a Thread
1. Shared Resources: Threads within the same process share:
➢ Code: The instructions they execute.
➢ Data: Variables and files.
➢ Open files: Access to files the process has opened.
2. Independence: Each thread operates independently with its own
working space (program counter, registers, and stack), but they can see
changes made by other threads since they share memory.

40
Advanced Operating System Module 2

3. Lightweight: Threads are often called lightweight processes because


they require less overhead (less memory and resources) compared to
creating a new process. This makes threads faster and more efficient.

How Threads Work (With Example)

• Imagine you have a program that downloads multiple files from the
internet.
• If you didn't use threads, the program would have to download each file
one after the other, one at a time (like a single-threaded process). This
would be slow, especially if you have many files to download.
• But with multithreading, the program can create a thread for each file
download, allowing multiple downloads to happen at the same time.
• Now, instead of waiting for one file to finish before starting another, all
files can be downloaded simultaneously.
• Each thread handles one file download independently, but all threads
share the program's resources (like the internet connection, memory,
etc.).

Types of Processes: Single-Threaded vs. Multi-Threaded

• Single-Threaded Process: This type of process only has one thread.


The entire process runs in a single flow, meaning tasks are handled one
after another.
o Example: A text editor that opens one document at a time and
can only perform one operation at once (like saving or editing).
• Multi-Threaded Process: This type of process can have multiple
threads running at the same time. Different tasks can be handled
simultaneously.
o Example: A web browser that can load multiple tabs
simultaneously. Each tab can be handled by a separate thread,
allowing you to load pages in different tabs without waiting for one
to finish.

41
Advanced Operating System Module 2

Multithreading Models

• In some operating systems, threads can exist at both the user level and
the kernel level.
• Threads at the user level are managed by the application, while kernel-
level threads are managed by the operating system.
• By combining these two types of threads, the system can improve
performance and take advantage of multiprocessor systems.
• The three main types of multithreading models are:

1. Many-to-Many
2. Many-to-One
3. One-to-One

42
Advanced Operating System Module 2

1. Many-to-Many Model

• In this model, many user threads are mapped to many kernel threads.
This means that multiple user-level threads (the threads that are
managed by the application) are connected to kernel-level threads (the
threads that the operating system controls).
• Key Characteristics:
o Developers can create as many user threads as needed, without
worrying about the kernel-level threads.
o These user threads can run in parallel on a multiprocessor
system.
o If one user thread makes a blocking system call (like waiting for
input), the kernel can schedule another thread to run. This
ensures the entire process does not get stuck waiting.
• Example:
o Imagine a video game where multiple tasks are happening at the
same time (like rendering the game graphics, managing player
input, and controlling game logic).
o In the many-to-many model, each task can run as a separate user
thread, and the operating system can handle multiple threads at
once across different processors. This allows smooth gameplay
with multiple tasks running in parallel.

43
Advanced Operating System Module 2

[Link]-to-One Model

• In the many-to-one model, many user-level threads are mapped to one


kernel thread.
• All the thread management happens in user space, and only one thread
can communicate with the kernel at a time.
• Key Characteristics:
o Blocking problem: If one user thread makes a blocking system
call, the entire process is blocked because only one kernel thread
is available.
o No parallel execution: Since only one kernel thread exists, user
threads cannot run in parallel, even if the system has multiple
processors.
• Example:
o Imagine a simple text editor that opens and edits multiple
documents. Each document could be managed by a user thread.
However, if one document is saving and blocks the process, all
other documents have to wait.
o The many-to-one model does not allow tasks to run in parallel, so
the user has to wait for each task to finish one after the other.

44
Advanced Operating System Module 2

3. One-to-One Model

• In the one-to-one model, each user-level thread is mapped to one


kernel thread. This allows for high concurrency because the system can
run multiple kernel threads in parallel.
• Key Characteristics:
o Every time a user thread is created, a corresponding kernel thread
is created.
o Multiple threads can run in parallel on a multiprocessor system.
o If one thread makes a blocking system call, another thread can
continue running because each thread has its own kernel thread.
• Disadvantage:
o Overhead: Since creating a user thread also requires creating a
kernel thread, this model can consume more system resources
(like memory).
• Example:
o In modern operating systems like Windows, multiple tasks can be
performed in parallel.
o For instance, you can download a file, play music, and browse the
internet simultaneously. Each task is a separate thread, and they
can all run in parallel because of the one-to-one model.

45
Advanced Operating System Module 2

xiii) Scheduling Criteria


There are several different criteria to consider when trying to select the “best”
scheduling algorithm for a particular situation and environment, including:
1. CPU Utilization

• This measures how much of the CPU's time is being used. Ideally, we
want the CPU to be working as much as possible, so none of its time is
wasted.
• However, on a real system, CPU usage usually ranges from 40% to 90%,
depending on how many tasks are running.
• Example:
➢ Imagine you have a factory, and you want your workers (CPU) to be
busy all the time making products (processes).
➢ If your workers are only busy 40% of the time, the factory isn’t very
efficient. But if they’re working close to 90% of the time, it means the
factory is running well.
2. Throughput

• This is the number of processes completed in a given amount of time.


• A higher throughput means more work is getting done.
• Example:
➢ If you have 10 tasks and finish 5 tasks in an hour, your throughput is 5
tasks per hour.
➢ If another algorithm finishes all 10 tasks in the same hour, it has a
higher throughput.
[Link] Time

• This is the total time it takes for a process to complete, from when it’s
first submitted to when it finishes.
• This includes waiting in queues, getting processed, and finishing up.

46
Advanced Operating System Module 2

• Example:
➢ Imagine a student submits homework and the teacher takes two
days to review it.
➢ The turnaround time is two days. If another teacher reviews it in just
one day, the turnaround time is shorter.

4. Waiting Time

• This is the amount of time a process spends waiting in the ready queue
before it gets a chance to use the CPU.
• Reducing waiting time helps processes run faster.
• Example:
➢ If three students are waiting in line to ask a question and each has to
wait 30 minutes, their waiting time is 30 minutes.
➢ If we can reduce the waiting time to 10 minutes, students get their
questions answered faster.
5. Load Average

• This shows how many processes are waiting in the ready queue over a
period of time.
• Load average can be measured in 1-minute, 5-minute, and 15-minute
intervals.
• Example:
➢ If there are always 10 people waiting in line to ask a question, the
load average is high.
➢ But if only 2 people are waiting, the load average is lower, meaning
the system is less busy.
6. Response Time

• This is how long it takes from the moment a user submits a request (like
typing a command) to when the system starts responding.
• This is especially important for interactive systems where users expect
quick feedback.

47
Advanced Operating System Module 2

• Example:
➢ When you press a button on your phone and the app opens
immediately, the response time is short.
➢ If you press the button and have to wait several seconds, the
response time is long.

Scheduling Algorithms
There are various algorithms which are used by the Operating System to
schedule the processes on the processor in an efficient way.

1. FCFS (First-Come, First-Served) Scheduling

• FCFS is the simplest CPU scheduling algorithm.


• In this algorithm, processes are executed in the order they arrive.
• The first process that requests the CPU is the first to get the CPU, and
this continues in a “first in, first out” (FIFO) manner.
• Once a process is allocated the CPU, it runs to completion before
another process can use the CPU, meaning FCFS is non-preemptive.
• Non-preemptive scheduling means that once the CPU is given to a
process, that process keeps control of the CPU until it finishes its task or
requests I/O.
• Example:

➢ Imagine standing in line at a billing counter in a shopping mall.


➢ The first customer in the line gets billed first.
➢ After the first customer finishes, the next customer in line gets billed,
and this continues for each person waiting.
➢ There’s no special treatment for anyone in the middle of the queue,
meaning no priority is given, and everyone gets served on a “first-
come, first-served” basis.

• In CPU-scheduling problems some terms are used while solving the


problems, so for conceptual purpose the terms are discussed as follows

48
Advanced Operating System Module 2

o Arrival time (AT) – Arrival time is the time at which the process
arrives in ready queue.
o Burst time (BT) or CPU time of the process – Burst time is the unit
of time in which a particular process completes its execution.
o Completion time (CT) – Completion time is the time at which the
process has been terminated.
o Turn-around time (TAT) – The total time from arrival time to
completion time is known as turn-around time. TAT can be written
as,

Turn-around time (TAT) = Completion time (CT) – Arrival time (AT) or,
TAT = Burst time (BT) + Waiting time (WT)

o Waiting time (WT) – Waiting time is the time at which the process
waits for its allocation while the previous process is in the CPU for
execution. WT is written as,

Waiting time (WT) = Turn-around time (TAT) – Burst time (BT)

o Response time (RT) – Response time is the time at which CPU has
been allocated to a particular process first time.
▪ In case of non-preemptive scheduling, generally Waiting
time and Response time is same.
o Gantt chart – Gantt chart is a visualization which helps to
scheduling and managing particular tasks in a project. It is used
while solving scheduling problems, for a concept of how the
processes are being allocated in different algorithms.

49
Advanced Operating System Module 2

50
Advanced Operating System Module 2

Advantages of FCFS Scheduling:


1. Simple to implement: FCFS (First-Come, First-Served) is easy to set
up because it works like a basic line, where tasks are processed in the
order they arrive.
2. Fairness: Since it follows the "first in, first out" rule, every task gets a
fair chance. No special priority is given to any task, so it’s a fair system
for all.
Disadvantages of FCFS Scheduling:
1. Convoy effect: If a task that takes a long time (higher burst time) is at
the front of the queue, smaller tasks behind it will have to wait, even
though they could finish quickly. This leads to delays.
2. Not suitable for time-sharing: In systems where quick switching
between tasks is needed, FCFS is not very efficient. If a large task is
first, smaller tasks must wait a long time.

3. Non-preemptive: Once a task starts, it holds the CPU until it’s done.
Even if an urgent task arrives, it cannot interrupt the current task,
leading to potential delays.
[Link] Job First (or SJF) CPU Scheduling
The Shortest Job First (SJF) is a CPU scheduling algorithm that selects the
process with the shortest burst time (execution time) to run next. This method
reduces the waiting time for processes and improves the overall efficiency of
the system.
Characteristics of SJF Scheduling:
Shortest Job first has the advantage of having a minimum average waiting time
among all scheduling algorithms.

It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can
be solved using the concept of ageing.

51
Advanced Operating System Module 2

It is practically infeasible as Operating System may not know burst times and
therefore may not sort them.
While it is not possible to predict execution time, several methods can be
used to estimate the execution time for a job, such as a weighted average of
previous execution times.
SJF can be used in specialized environments where accurate estimates of
running time are available.
Algorithm:
• Sort all the processes according to the arrival time.
• Then select that process that has minimum arrival time and
minimum Burst time.
• After completion of the process make a pool of processes that
arrives afterward till the completion of the previous process and
select that process among the pool which is having minimum Burst
time.
How to compute below times in SJF using a program?
• Completion Time: Time at which process completes its execution.
• Turn Around Time: Time Difference between completion time and
arrival time.
Turn Around Time = Completion Time – Arrival Time
• Waiting Time(W.T): Time Difference between turn around time and
burst time.
Waiting Time = Turn Around Time – Burst Time

52
Advanced Operating System Module 2

Example-1: Consider the following table of arrival time and burst time for
five processes P1, P2, P3, P4 and P5.

The Shortest Job First CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time = 0,

• Process P4 arrives and starts executing

At time= 1,
• Process P3 arrives.
• But, as P4 still needs 2 execution units to complete.
• Thus, P3 will wait till P4 gets executed.

53
Advanced Operating System Module 2

At time =2,
• Process P1 arrives and is added to the waiting table
• P4 will continue its execution.

At time = 3,

• Process P4 will finish its execution.


• Then, the burst time of P3 and P1 is compared.
• Process P1 is executed because its burst time is less as compared to
P3.

54
Advanced Operating System Module 2

At time = 4,

• Process P5 arrives and is added to the waiting Table.


• P1 will continue execution.

At time = 5,
• Process P2 arrives and is added to the waiting Table.
• P1 will continue execution.

55
Advanced Operating System Module 2

At time = 6,

• Process P1 will finish its execution.


• The burst time of P3, P5, and P2 is compared.
• Process P2 is executed because its burst time is the lowest among all.

At time=9,
• Process P2 is executing and P3 and P5 are in the waiting Table.

56
Advanced Operating System Module 2

At time = 11,

• The execution of Process P2 will be done.


• The burst time of P3 and P5 is compared.
• Process P5 is executed because its burst time is lower than P3.

At time = 15,

• Process P5 will finish its execution.

57
Advanced Operating System Module 2

At time = 23,

• Process P3 will finish its execution.


Gantt chart for above execution:

Now, let’s calculate the average waiting time for above example:

• P4 = 0 – 0 = 0
• P1 = 3 – 2 = 1
• P2 = 9 – 5 = 4
• P5 = 11 – 4 = 7
• P3 = 15 – 1 = 14
• Average Waiting Time = 0 + 1 + 4 + 7 + 14/5 = 26/5 = 5.2

58
Advanced Operating System Module 2

Advantages of SJF:

• SJF is better than the First come first serve(FCFS) algorithm as it


reduces the average waiting time.
• SJF is generally used for long term scheduling
• It is suitable for the jobs running in batches, where run times are already
known.
• SJF is probably optimal in terms of average turnaround time.

Disadvantages of SJF:

• SJF may cause very long turn-around times or starvation.


• In SJF job completion time must be known earlier, but sometimes it is
hard to predict.
• Sometimes, it is complicated to predict the length of the upcoming CPU
request.
• It leads to the starvation that does not reduce average turnaround time.

[Link] Scheduling Algorithm

• Priority Scheduling is a CPU scheduling algorithm that assigns priorities


to processes. The CPU is allocated to the process with the highest
priority.
• If two processes have the same priority, they are scheduled according to
another algorithm (e.g., First-Come, First-Served).
• Types of Priority Scheduling
o Preemptive: If a new process arrives with a higher priority than the
currently executing process, it preempts the running process.
o Non-preemptive: The currently running process is allowed to
finish its CPU burst even if a higher-priority process arrives.

59
Advanced Operating System Module 2

How it Works (Example)


Consider the following set of processes with arrival time, burst time
(execution time), and priority:

Process Arrival Time Burst Time Priority


P1 0 4 2
P2 1 3 1
P3 2 5 3

Step 1: At time 0, P1 arrives and starts executing since it’s the only process.
Step 2: At time 1, P2 arrives with a higher priority. If preemptive priority
scheduling is used, P1 is preempted, and P2 starts executing.
Step 3: P2 finishes at time 4. Now, the CPU selects P1 (since its priority is
higher than P3).
Step 4: P1 finishes at time 6, and then P3 starts executing.
Advantages of Priority Scheduling

• Flexibility: Processes can be assigned different priorities based on their


importance, ensuring that critical tasks are executed first.
• Efficient for Critical Tasks: It ensures that urgent tasks get CPU time as
soon as possible.

Disadvantages of Priority Scheduling

• Starvation: Lower-priority processes may suffer from indefinite waiting if


higher-priority processes keep arriving (known as starvation).
• Complexity: Managing priorities, especially with dynamic priorities, can
be more complex compared to simpler algorithms like First-Come, First-
Served.

60
Advanced Operating System Module 2

4. Round Robin Scheduling


Round Robin (RR) is a CPU scheduling algorithm designed for time-sharing systems, where
multiple processes or jobs need to share the CPU resources effectively. In this method,
each process is given a fixed amount of time, called a time quantum or time slice, to
execute. After this time expires, the CPU moves on to the next process in the ready queue.

How Does Round Robin Work?

• Time Quantum:
o A fixed time period is determined (e.g., 100 milliseconds). This is how long
each process can use the CPU before it must yield to another process.
• Queue System:
o Processes are placed in a ready queue in the order they arrive. Each process
waits for its turn based on this order.
• Execution Cycle:
o The CPU picks the first process in the queue and gives it the CPU for the
duration of the time quantum.
o If the process finishes before the time quantum ends, it is removed from the
queue.
o If the process does not finish, it is moved to the end of the queue, and the
CPU moves on to the next process.
• Repeat:
o This cycle continues until all processes are completed.

Characteristics of Round Robin Scheduling

1. Fairness: Each process gets an equal share of the CPU, making it fair
for all processes.
2. Preemptive: The scheduler can interrupt a process that has not
finished executing when its time quantum expires.
3. Easy to Implement: The algorithm is straightforward, making it easy to
implement in various systems.

4. Starvation-Free: No process is allowed to starve; every process gets a


chance to execute.

61
Advanced Operating System Module 2

Advantages of Round Robin Scheduling

• Fairness: Every process receives an equal opportunity to utilize the


CPU.
• Responsive: It ensures that short processes can be executed quickly.
• Time-Sharing: Suitable for time-sharing environments, giving an illusion
of parallelism.

Disadvantages of Round Robin Scheduling

• Higher Waiting Time: Due to context switching, processes may wait


longer to complete.
• Low Throughput: More context switches can lead to lower overall
throughput of the system.
• Gantt Chart Size: If the time quantum is too small (e.g., 1 ms), the
Gantt chart can become very large and complex.
• Time-Consuming: If the quantum is small, it can lead to frequent
switching, wasting CPU cycles on managing processes rather than
executing them.
[Link] Queue (MLQ) CPU Scheduling

• Multilevel Queue (MLQ) CPU Scheduling is a method of organizing


processes in the ready queue into different categories, or queues,
based on their priority and scheduling needs.
• This approach helps ensure that processes with varying requirements
are managed efficiently.

62
Advanced Operating System Module 2

Key Features of MLQ Scheduling:


1. Multiple Queues:

• Processes are divided into multiple queues, each with a different


priority level.
• Example:
• Queue 1: System Processes (highest priority)
• Queue 2: Interactive Processes (medium priority)
• Queue 3: Batch Processes (lowest priority)

2. Priorities Assigned:

• Each queue is assigned a priority based on the nature of its


processes.
• Interactive processes, which require immediate attention (like
user input), have a higher priority than batch processes that can
be executed later.

3. Preemption:

• If a higher-priority process enters the system, it can preempt a


currently running lower-priority process.
• This ensures that urgent tasks are completed promptly.

4. Scheduling Algorithms:

• Different queues can use different scheduling algorithms based


on their needs.
• Example:
• Queue 1 (System Processes): First Come First Serve (FCFS)
• Queue 2 (Interactive Processes): Round Robin
• Queue 3 (Batch Processes): FCFS

63
Advanced Operating System Module 2

5. Feedback Mechanism:

• Processes can have their priorities adjusted over time based on


their wait times and behaviors.
• For instance, if an interactive process is waiting too long, its
priority may be raised.
6. Efficient CPU Allocation:

• MLQ scheduling allocates CPU time efficiently, ensuring high-


priority tasks get executed promptly while still allowing lower-
priority processes to run when possible.
7. Fairness:

• Each type of process receives a fair amount of CPU time


according to its priority level and requirements.
8. Customizable:

• MLQ scheduling can be tailored to suit specific needs of different


types of processes.

Advantages of MLQ CPU Scheduling:


[Link] Scheduling Overhead:
Processes are permanently assigned to queues, minimizing the
overhead in switching between them.
[Link] Allocation:
Higher-priority processes are executed quickly, optimizing CPU
utilization.

64
Advanced Operating System Module 2

[Link] Allocation:
Ensures that different types of processes receive appropriate CPU
time.

[Link] Algorithms:
Different algorithms can be tailored for each queue to meet
specific process needs.
[Link]:
Important processes are executed on time due to assigned
priorities.

[Link]:
Higher-priority processes can interrupt lower-priority ones,
ensuring critical tasks are handled swiftly.
Disadvantages of MLQ CPU Scheduling:
[Link] Starvation:

Lower-priority processes may starve if high-priority queues are


consistently busy.
[Link]:
Once assigned to a queue, a process cannot change its queue,
which may not always be ideal.
[Link]:

Managing multiple queues and scheduling algorithms can add


complexity to system implementation.

65
Advanced Operating System Module 2

[Link] Feedback Queue (MLFQ) Scheduling


Multilevel Feedback Queue (MLFQ) Scheduling is an advanced CPU
scheduling algorithm that allows processes to move between different priority
queues based on their behavior and requirements.
This flexibility helps improve the responsiveness of the system, especially for
interactive processes.
Characteristics of MLFQ Scheduling
1. Multiple Queues:
Similar to Multilevel Queue scheduling, MLFQ uses multiple queues,
but processes can move between these queues.
2. Dynamic Priority Adjustment:
Processes are assigned priorities that can change over time based on
their execution history (e.g., CPU burst time).
If a process uses too much CPU time, it may be moved to a lower-
priority queue.
3. Feedback Mechanism:
Processes can receive feedback based on their execution behavior. For
example, if an interactive process is waiting too long, its priority may be
increased.
4. Different Scheduling Algorithms:

Each queue can use different scheduling algorithms, typically using:


Round Robin for higher-priority queues (to ensure responsiveness).
First Come First Serve (FCFS) for lower-priority queues (to maintain
fairness).

66
Advanced Operating System Module 2

5. Preemption:

A higher-priority process can preempt a currently running lower-priority


process, ensuring that urgent tasks are handled quickly.
6. Ageing:
A technique is often used to prevent starvation, where processes in
lower-priority queues gradually gain priority over time.
Advantages of MLFQ Scheduling

[Link] for Interactive Processes:


MLFQ is particularly effective for interactive applications, as it allows
quick responses to user inputs.
[Link] Priority Adjustment:
The ability to adjust process priorities based on their behavior allows for
a more efficient scheduling strategy.
[Link]:
Processes that require more CPU time can be demoted, while
interactive processes are prioritized, promoting a balanced allocation of
resources.
[Link] of Starvation:

The ageing mechanism helps prevent lower-priority processes from


starving by gradually increasing their priority.

67
Advanced Operating System Module 2

[Link]:
The scheduling algorithm can be tailored to specific system
requirements, allowing different strategies for different types of
processes.
Disadvantages of MLFQ Scheduling
[Link]:
Implementing MLFQ scheduling can be complex due to the need for
multiple queues and dynamic adjustments to process priorities.
[Link]:

The continuous monitoring and adjustment of process priorities can


introduce additional overhead in the system.
[Link] Difficulties:
Finding the right parameters (like time slices for each queue) can be
challenging and may require extensive testing to optimize performance.

[Link] for Starvation:


If not implemented carefully, lower-priority processes might still
experience starvation, especially if higher-priority processes keep
arriving.
[Link] with Long-Running Processes:
Long-running batch processes might not receive enough CPU time if
there are many short, high-priority processes, leading to inefficiency.

68

Common questions

Powered by AI

Priority scheduling allows processes to be scheduled based on assigned priority levels, ensuring that higher-priority tasks are executed before lower-priority ones. This can be beneficial for critical tasks that need immediate attention, as they can preempt lower-priority processes . However, this approach can lead to problems such as starvation, where lower-priority processes may never get scheduled if higher-priority processes continuously enter the system. This potential for indefinite waiting can lead to lower-priority processes being neglected, which may not be suitable for all environments, particularly when fairness among processes is required .

Context switching plays a crucial role in multitasking by allowing multiple processes to share the CPU effectively. During a context switch, the current state of a process, including its CPU registers and program counter, is saved in its PCB, and the state of the next scheduled process is loaded from its PCB. This mechanism enables the operating system to suspend and resume processes, ensuring efficient CPU utilization . However, context switches introduce overhead due to the time required to save and load process states. Frequent switching can degrade system performance as more CPU time is spent managing processes rather than executing them, particularly in systems with numerous context switches due to small time quanta in scheduling algorithms like Round Robin .

The design and implementation of PCBs contribute to the stability and efficiency of an operating system by organizing all necessary information about processes in a structured manner. Each PCB stores critical data like process state, memory usage, and scheduling information, allowing the OS to manage processes comprehensively and efficiently . This organization aids in smooth process scheduling, context switching, and resource management, which are essential for stable and efficient system operation. However, the complexity associated with managing numerous PCBs and the memory overhead they require can challenge system scalability and responsiveness, indicating a delicate balance must be maintained between organizational efficiency and system complexity .

The Process Control Block (PCB) provides several advantages for process management. It organizes critical information about a process, such as memory usage and CPU scheduling details, which helps the OS schedule processes efficiently and perform context switching smoothly. This results in optimized CPU usage and efficient multitasking . However, maintaining a PCB for each process increases memory usage and adds complexity to the operating system, especially as the number of processes grows. This can slow down performance, and the added complexity makes it challenging to scale and manage processes .

Round Robin scheduling ensures fairness by giving each process an equal time slice or quantum to execute, rotating through processes in a cyclic order. This approach prevents any single process from dominating CPU time, fostering a balanced execution environment . However, the trade-offs involve increased system overhead due to frequent context switches, especially if the time quantum is small. This can lead to lower throughput, as more CPU cycles are spent on managing processes rather than executing them. The need to manage the size of the time quantum carefully is crucial, as a very small quantum can cause excessive overhead, while a very large one can diminish the responsiveness intended by this scheduling technique .

The Multilevel Feedback Queue (MLFQ) prevents process starvation through a mechanism known as aging, where the priority of long-waiting processes is gradually increased. By adjusting priorities dynamically based on execution patterns, MLFQ ensures that processes with lower initial priorities eventually receive the CPU time they need, thereby preventing indefinite postponement . This prevention of starvation is critical for system performance because it maintains fairness and ensures that all processes, regardless of their initial priority, can eventually complete execution. This balance between responsiveness for high-priority tasks and fairness for all tasks enhances overall system stability and user satisfaction .

Multilevel Feedback Queue (MLFQ) scheduling introduced dynamic priority adjustment and the ability for processes to move between different priority queues based on their behavior. This flexibility allows the system to respond more dynamically to different process needs, improving responsiveness particularly for interactive applications. Unlike Multilevel Queue (MLQ) scheduling, where processes are fixed in a single queue, MLFQ adjusts a process's priority over time based on its execution history. This means if a process consumes significant CPU resources, its priority might be lowered, while interactive processes waiting too long might gain priority, thus enhancing overall responsiveness and ensuring critical tasks receive timely execution .

The Operating System uses the Process Control Block (PCB) to manage different states of a process effectively. The PCB records the current state of a process, which can be "running," "ready," "waiting," or "terminated." The information in the PCB allows the OS to schedule processes appropriately based on their state. For example, a process in the "ready" state may be scheduled to run next, while one in the "waiting" state is paused until its required event occurs. Additionally, context switching is facilitated by the PCB; when a process is paused, its state, including CPU registers and the program counter, is saved in the PCB, allowing the OS to resume it correctly later .

The PCB aids in resource sharing by keeping track of the resources each process is using, such as memory allocations and open files. This information facilitates the OS in managing how resources can be shared or accessed by other processes, ensuring resource conflicts are minimized . Additionally, for synchronization, the PCB records status information that can help the OS manage inter-process communication and coordinate access to shared resources efficiently. This ensures that processes can interact smoothly without deadlocks or race conditions, enhancing overall system stability and efficiency .

The stack is a part of a computer's memory that holds temporary data necessary for the execution of processes. It is dynamically managed, growing and shrinking as functions are called and completed. Its key roles include storing function parameters, return addresses, and local variables. When a function is called, the stack allocates space for these, ensuring that the program can return to the correct point after a function execution and that each function has the correct input data to operate. This dynamic nature and structured management of data allows the stack to facilitate function execution and return in an efficient and orderly manner .

You might also like