HW806 6
HW806 6
Operating System:
Definitions:
An Operating System is a System software that manages all the resources of the computing
deice.
Acts as an interface between the software and different parts of the computer or
the computer hardware.
Manages the overall resources and operations of the computer.
Controls and monitors the execution of all other programs that reside in the
computer, which also includes application programs and other system software of
the computer.
Examples of Operating Systems are Windows, Linux, macOS, Android, iOS, etc.
OS Components:
There are various components of an Operating System to perform well defined tasks. Though
most of the Operating Systems differ in structure but logically they have similar components.
Each component must be a well-defined portion of a system that appropriately describes the
functions, inputs, and outputs.
1. Process Management
2. I/O Device Management
3. File Management
4. Network Management
5. Main Memory Management
6. Secondary Storage Management
7. Security Management
8. Command Interpreter System
Process Management
A process is program or a fraction of a program that is loaded in main memory. A process needs
certain resources including CPU time, Memory, Files, and I/O devices to accomplish its task. The
process management component manages the multiple processes running simultaneously on
the Operating System.
The operating system is responsible for the following activities in connection with process
management:
One of the purposes of an operating system is to hide the peculiarities of specific hardware
devices from the user. I/O Device Management provides an abstract level of H/W devices and
keep the details from applications to ensure proper use of devices, to prevent errors, and to
provide users with convenient and efficient programming environment.
File Management
File management is one of the most visible services of an operating system. Computers can
store information in several different physical forms; magnetic tape, disk, and drum are the
most common forms.
A file is defined as a set of correlated information and it is defined by the creator of the file.
Mostly files represent data, source and object forms, and programs. Data files can be of any
type like alphabetic, numeric, and alphanumeric.
The operating system implements the abstract concept of the file by managing mass storage
device, such as types and disks. Also files are normally organized into directories to ease their
use. These directories may contain files and other directories and so on.
The operating system is responsible for the following activities in connection with file
management:
Network Management
Network management comprises fault analysis, maintaining the quality of service, provisioning
of networks, and performance management.
Network administration
Network maintenance
Network operation
Network provisioning
Network security
Memory is a large array of words or bytes, each with its own address. It is a repository of
quickly accessible data shared by the CPU and I/O devices.
Main memory is a volatile storage device which means it loses its contents in the case of system
failure or as soon as system power goes down.
The operating system is responsible for the following activities in connections with memory
management:
Keep track of which parts of memory are currently being used and by whom.
Decide which processes to load when memory space becomes available.
Allocate and deallocate memory space as needed.
The main purpose of a computer system is to execute programs. These programs, together with
the data they access, must be in main memory during execution. Since the main memory is too
small to permanently accommodate all data and program, the computer system must provide
secondary storage to backup main memory.
Most modern computer systems use disks as the principle on-line storage medium, for both
programs and data. Most programs, like compilers, assemblers, sort routines, editors,
formatters, and so on, are stored on the disk until loaded into memory, and then use the disk as
both the source and destination of their processing.
The operating system is responsible for the following activities in connection with disk
management:
Security Management
The operating system is primarily responsible for all task and activities happen in the computer
system. The various processes in an operating system must be protected from each other’s
activities. For that purpose, various mechanisms which can be used to ensure that the files,
memory segment, cpu and other resources can be operated on only by those processes that
have gained proper authorization from the operating system.
For example, memory addressing hardware ensure that a process can only execute within its
own address space. The timer ensure that no process can gain control of the CPU without
relinquishing it. Finally, no process is allowed to do it’s own I/O, to protect the integrity of the
various peripheral devices.
Command Interpreter System executes a user command by calling one or more number of
underlying system programs or system calls.
Many commands are given to the operating system by control statements. A program which
reads and interprets control statements is automatically executed. This program is called the
shell and few examples are Windows DOS command window, Bash of Unix/Linux or C-Shell of
Unix/Linux.
Evolution of operating system:
Operating systems have progressed from slow and expensive systems to today's technology,
which has exponentially increased computing power at comparatively modest costs. So let's
have a detailed look at the evolution of operating systems.
Example: Mac OS X 10.6.8 snow leopard and OS X 10.7.5 Lion are some examples
of macintosh OS.
Multiprogramming:
Multiprogramming means more than one program can be active at the same time. Before the
operating system concept, only one program was to be loaded at a time and run. These
systems were not efficient as the CPU was not used efficiently. For example, in a single-
tasking system, the CPU is not used if the current program waits for some input/output to
finish. The idea of multiprogramming is to assign CPUs to other processes while the current
process might not be finished. This has the below advantages.
1) The user gets the feeling that he/she can run multiple applications on a single CPU even if
the CPU is running one process at a time.
2) CPU is utilized better
All modern operating systems like MS Windows, Linux, etc are multiprogramming operating
systems.
Features of Multiprogramming
Need Single CPU for implementation.
Context switch between process.
Switching happens when current process undergoes waiting state.
CPU idle time is reduced.
High resource utilization.
High Performance.
Multitasking:
Multi tasking operating systems allow multiple users to perform multiple tasks at the same
time. The allocation of system resources such as input/output
devices, CPU and memory among processes can be easily managed by multi-tasking operating
system. There are various states through which a processor passes to complete particular or
multiple executions. Multitasking is the ability of an OS to execute more than
one task simultaneously on a CPU machine as shown in the below diagram.
Features of Multi-Tasking Operating System
Time Sharing – In this, many processes are allocated with resources of computer in
respective time slots, processors time is shared with multiple processes.
Context Switching – context switching is a process of saving the context of one
process and loading the context of another process. In simpler terms it is loading
another process when the prior process has finished its execution.
Multi-Threading – Multithreading is the ability of a program or an operating
system to enable more than one user at a time without requiring multiple copies
of the program running on the computer.
Hardware Interrupt – When a process or an event requires urgent
attention, hardware or software will signal with an interrupt. It informs the
processor that a high-priority task has arisen that necessitates interrupting the
running process.
Types of Multi-Tasking Operating System
Two types of Multi-Tasking Operating System available as shown below:
Pre-emptive Multi-Tasking Operating System: In pre-emptive multitasking, the
operating system can initiate a context switching from the running process to
another process. In other words, the operating system allows stopping the
execution of the currently running process and allocating the CPU to some other
process. The OS uses some criteria to decide for how long a process should
execute before allowing another process to use the operating system. The
mechanism of taking control of the operating system from one process and giving
it to another process is called pre-emption. Here are some Examples UNIX,
Windows 95, Windows NT operating system.
Non-pre-emptive Multi-Tasking Operating System: Non-pre-emptive Multi-
Tasking Operating System is also known as cooperative multitasking, this operating
system never initiates context switching from the running process to another
process. A context switch occurs only when the processes voluntarily yield control
periodically or when idle or logically blocked to allow multiple applications to
execute simultaneously. Also, in this multitasking, all the processes cooperate for
the scheduling scheme to work. Example – Macintosh OS version 8.0-9.2.2 and
Windows 3.x operating system.
Client-Server:
The Client-server model is a distributed application structure that partitions tasks or
workloads between the providers of a resource or service, called servers, and service
requesters called clients. In the client-server architecture, when the client computer sends a
request for data to the server through the internet, the server accepts the requested process
and delivers the data packets requested back to the client. Clients do not share any of their
resources. Examples of the Client-Server Model are Email, World Wide Web, etc.
How Does the Client-Server Model Work?
In this article, we are going to take a dive into the Client-Server model and have a look at how
the Internet works via, web browsers. This article will help us have a solid WEB foundation
and help us easily work with WEB technologies.
Client: When we say the word Client, it means to talk of a person or an
organization using a particular service. Similarly in the digital world, a Client is a
computer (Host) i.e. capable of receiving information or using a particular service
from the service providers (Servers).
Servers: Similarly, when we talk about the word Servers, It means a person or
medium that serves something. Similarly in this digital world, a Server is a remote
computer that provides information (data) or access to particular services.
Peer-to-peer:
A peer-to-peer (P2P) operating system is a network architecture that allows devices to share
resources without a central server. In a P2P network, each device can act as both a server and a
client.
Features
Decentralized: Each device stores a portion of the shared data locally.
Equal participants: All devices have the same capabilities and can initiate
communication sessions.
Distributed storage: Eliminates reliance on a single point of failure.
Anonymized routing: Can provide anonymized routing of network traffic.
System calls:
System calls provide an interface to the services made by an operating system. The user
interacts with the operating system programs through System calls. These calls are normally
made available as library functions in high-level languages such as C, Java, Python etc. It
provides a level of abstraction as the user is not aware of the implementation or execution of
the call made. Details of the operating system is hidden from the user. Different hardware
and software services can be availed through system calls.
System calls are available for the following operations:
Process Management
Memory Management
File Operations
Input / Output Operations
The process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Categories of Scheduling
1. Non-preemptive: Here the resource can’t be taken from a process until the process
completes execution. The switching of resources occurs when the running process
terminates and moves to a waiting state.
2. Preemptive: Here the OS allocates the resources to a process for a fixed amount of
time. During resource allocation, the process switches from running state to ready state
or from waiting state to ready state. This switching occurs as the CPU may give priority
to other processes and replace the process with higher priority with the running
process.
Concept of a process:
A process is a program in execution. For example, when we write a program in C or C++ and
compile it, the compiler creates binary code. The original code and binary code are both
programs. When we actually run the binary code, it becomes a process.
A process is an 'active' entity instead of a program, which is considered a 'passive'
entity.
A single program can create many processes when run multiple times; for example,
when we open a .exe or binary file multiple times, multiple instances begin
(multiple processes are created). .
How Does a Process Look Like in Memory?
A process in memory is divided into several distinct sections, each serving a different purpose.
Here's how a process typically looks in memory:
Text Section: A text or code segment contains executable instructions. It is
typically a read only section
Stack: The stack contains temporary data, such as function parameters, returns
addresses, and local variables.
Data Section: Contains the global variable.
Heap Section: Dynamically memory allocated to process during its run time.
Attributes of a Process
A process has several important attributes that help the operating system manage and
control it. These attributes are stored in a structure called the Process Control Block
(PCB) (sometimes called a task control block). The PCB keeps all the key information about
the process, including:
1. Process ID (PID): A unique number assigned to each process so the operating
system can identify it.
2. Process State: This shows the current status of the process, like whether it is
running, waiting, or ready to execute.
3. Priority and other CPU Scheduling Information: Data that helps the operating
system decide which process should run next, like priority levels and pointers to
scheduling queues.
4. I/O Information: Information about input/output devices the process is using.
5. File Descriptors: Information about open files files and network connections.
6. Accounting Information: Tracks how long the process has run, the amount of CPU
time used, and other resource usage data.
7. Memory Management Information: Details about the memory space allocated to
the process, including where it is loaded in memory and the structure of its
memory layout (stack, heap, etc.).
These attributes in the PCB help the operating system control, schedule, and manage each
process effectively.
Process states:
A process is in one of the following states:
New: Newly Created Process (or) being-created process.
Ready: After the creation process moves to the Ready state, i.e. the process is
ready for execution.
Running: Currently running process in CPU (only one process at a time can be
under execution in a single processor).
Wait (or Block): When a process requests I/O access.
Complete (or Terminated): The process completed its execution.
Suspended Ready: When the ready queue becomes full, some processes are
moved to a suspended ready state
Suspended Block: When the waiting queue becomes full.
PCB: A Process Control Block, commonly referred to as PCB in OS, is a crucial data structure that
encapsulates the specifics about every process present within a computing system. When a
process is birthed, the operating system instantaneously establishes a unique PCB in OS for it.
This is done to ensure that the system can actively monitor the state of the process, efficiently
manage the resources it might require, and oversee its execution in a streamlined manner.
Essentially, the PCB in OS acts as the fingerprint of a process, providing the OS with all the
pertinent details required to manage and control the process optimally. Without the PCB in OS,
the operating system would be blind to the processes it needs to govern, making the PCB
indispensable for smooth OS functionality.
The process control block contains many attributes such as process ID, process state, process
priority, accounting information, program counter, CPU registers`, etc for each process.
When a new process is created by the user, the operating system assigns a unique ID i.e.
a process-ID to that process. This ID helps the process to be distinguished from other processes
existing in the system.
The operating system has a limit on the maximum number of processes it is capable of dealing
with, let's say the OS can handle most N processes at a time.
Now if a new process is created after process(N-1) then the Operating System will allot ID 0 to
this new process considering the older process at id 0 is already terminated. This is one of the
schemes for assigning the process IDs.
There is another scheme of assignment where the process IDs are not allocated in ascending
order.
Let's say a single PCB requires X bytes of memory and in total there can be N processes allowed
at a time. Then, the operating system will reserve N*X bytes for all the PCBs.
These PCBs are numbered from 0 to N-1. Please note here we are giving IDs to PCBs and not to
the processes.
Now whenever a process is triggered by the user a free PCB slot is allotted to that process and
the process ID of that process will be the same as the PCB slot number. So, the operating
system maintains a chain of free PCB slots. If the chain is empty no new process can be created.
2. Process State:
A process, from its creation to completion goes through different states. Generally, a process
may be present in one of the 5 states during its execution:
New: This state contains the processes that are ready to be loaded by the operating
system into the main memory.
Ready: This state contains the process that is both ready to be executed and is currently
in the main memory of the system. The operating system brings the processes from
secondary memory(hard disk) to main memory(RAM). As these processes are present in
the main memory and are waiting to be assigned to the CPU, the state of these
processes is known as the Ready state.
Running: This state contains the processes that are currently executed by the CPU in our
system. If there is a total x CPU in our system, then the maximum number of running
processes for a particular time is also x.
Block or wait: A process from its running state may transition to a block or wait-for state
depending on the scheduling algorithm or because of the internal behavior of the
process (the process explicitly wants to wait).
Termination: A process that completes its execution comes to its termination state. All
the contents of that process(Process control block) will also be deleted by the operating
system.
3. Process Priority:
Process priority is a numeric value that represents the priority of each process. The lesser the
value, the greater the priority of that process. This priority is assigned at the time of the
creation of the PCB and may depend on many factors like the age of that process, the resources
consumed, and so on. The user can also externally assign a priority to the process.
This attribute gives information on the resources used by that process in its lifetime. For
Example: CPU time connection time, etc.
5. Program Counter:
The program counter is a pointer that points to the next instruction in the program to be
executed. This attribute of PCB contains the address of the next instruction to be executed in
the process.
6. CPU registers:
A CPU register is a quickly accessible small-sized location available to the CPU. These registers
are stored in virtual memory (RAM)`.
6. Context Switching:
Context switching is a process that involves switching the CPU from one process or task to
another. It is the process of storing the state of a process so that it can be restored and resume
execution at a later point. This allows multiple processes to share a single CPU and is an
essential feature of a multitasking operating system.
So, whenever context switching occurs in the code execution the current state of that process is
stored temporarily in CPU registers. This helps in the fast execution of the process by not
wasting time-saving and retrieving state information from the secondary memory(hard disk).
8. PCB pointer:
This field contains the address of the next PCB, which is in ready state. This helps the operating
system to hierarchically maintain an easy control flow between parent processes and child
processes.
As the name suggests, It contains information on all the files that are used by that process. This
field is important as it helps the operating system to close all the opened files at the
termination state of the process.
In this field, the list of all the input/output devices that are required by that process during its
execution is mentioned.
Process lifecycle:
When you run a program (which becomes a process), it goes through different phases before
it completion. These phases, or states, can vary depending on the operating system, but the
most common process lifecycle includes two, five, or seven states. Here’s a simple
explanation of these states:
The Two-State Model
The simplest way to think about a process’s lifecycle is with just two states:
1. Running: This means the process is actively using the CPU to do its work.
2. Not Running: This means the process is not currently using the CPU. It could be
waiting for something, like user input or data, or it might just be paused.
When a new process is created, it starts in the not running state. Initially, this process is kept
in a program called the dispatcher.
Here’s what happens step by step:
1. Not Running State: When the process is first created, it is not using the CPU.
2. Dispatcher Role: The dispatcher checks if the CPU is free (available for use).
3. Moving to Running State: If the CPU is free, the dispatcher lets the process use the
CPU, and it moves into the running state.
4. CPU Scheduler Role: When the CPU is available, the CPU scheduler decides which
process gets to run next. It picks the process based on a set of rules called
the scheduling scheme, which varies from one operating system to another.
The Five-State Model
The five-state process lifecycle is an expanded version of the two-state model. The two-state
model works well when all processes in the not running state are ready to run. However, in
some operating systems, a process may not be able to run because it is waiting for something,
like input or data from an external device. To handle this situation better, the not running
state is divided into two separate states:
Operations on processes:
The states of a process are as follows:
New State: In this step, the process is about to be created but not yet created. It is
the program that is present in secondary memory that will be picked up by the OS
to create the process.
Ready State: New -> Ready to run. After the creation of a process, the process
enters the ready state i.e. the process is loaded into the main memory. The
process here is ready to run and is waiting to get the CPU time for its execution.
Processes that are ready for execution by the CPU are maintained in a queue called
a ready queue for ready processes.
Run State: The process is chosen from the ready queue by the OS for execution
and the instructions within the process are executed by any one of the available
processors.
Blocked or Wait State: Whenever the process requests access to I/O needs input
from the user or needs access to a critical region(the lock for which is already
acquired) it enters the blocked or waits state. The process continues to wait in the
main memory and does not require CPU. Once the I/O operation is completed the
process goes to the ready state.
Terminated or Completed State: Process is killed as well as PCB is deleted. The
resources allocated to the process will be released or deallocated.
Suspend Ready: Process that was initially in the ready state but was swapped out
of main memory(refer to Virtual Memory topic) and placed onto external storage
by the scheduler is said to be in suspend ready state. The process will transition
back to a ready state whenever the process is again brought onto the main
memory.
Suspend Wait or Suspend Blocked: Similar to suspend ready but uses the process
which was performing I/O operation and lack of main memory caused them to
move to secondary memory. When work is finished it may go to suspend ready.
Operation on a Process
The execution of a process is a complex activity. It involves various operations. Following are
the operations that are performed while execution of a process:
1. Creation
This is the initial step of the process execution activity. Process creation means the
construction of a new process for execution. This might be performed by the system, the
user, or the old process itself. There are several events that lead to the process creation.
Some of the such events are the following:
When we start the computer, the system creates several background processes.
A user may request to create a new process.
A process can create a new process itself while executing.
The batch system takes initiation of a batch job.
2. Scheduling/Dispatching
The event or activity in which the state of the process is changed from ready to run. It means
the operating system puts the process from the ready state into the running state.
Dispatching is done by the operating system when the resources are free or the process has
higher priority than the ongoing process. There are various other cases in which the process
in the running state is preempted and the process in the ready state is dispatched by
the operating system.
3. Blocking
When a process invokes an input-output system call that blocks the process, and operating
system is put in block mode. Block mode is basically a mode where the process waits for
input-output. Hence on the demand of the process itself, the operating system blocks the
process and dispatches another process to the processor. Hence, in process-blocking
operations, the operating system puts the process in a ‘waiting’ state.
4. Preemption
When a timeout occurs that means the process hadn’t been terminated in the allotted time
interval and the next process is ready to execute, then the operating system preempts the
process. This operation is only valid where CPU scheduling supports preemption. Basically,
this happens in priority scheduling where on the incoming of high priority process the
ongoing process is preempted. Hence, in process preemption operation, the operating system
puts the process in a ‘ready’ state.
5. Process Termination
Process termination is the activity of ending the process. In other words, process termination
is the relaxation of computer resources taken by the process for the execution. Like creation,
in termination also there may be several events that may lead to the process of termination.
Some of them are:
The process completes its execution fully and it indicates to the OS that it has
finished.
The operating system itself terminates the process due to service errors.
There may be a problem in hardware that terminates the process.
Context switch:
A context switching is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a
later time. Using this technique, a context switcher enables multiple processes to share a single
CPU. Context switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state
for the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At
that point, the second process can start executing.
Context switches are computationally intensive since register and memory state must be saved
and restored. To avoid the amount of context switching time, some hardware systems employ
two or more sets of processor registers. When the process is switched, the following
information is stored for later use.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Types of schedulers:
Schedulers
Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to
run. Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing. It selects processes from the queue and loads them into
memory for execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the
average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long term scheduler. When a process changes the state from new to
ready, then there is use of long-term scheduler.
It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.
Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling
the swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled
out. Swapping may be necessary to improve the process mix.
In operating systems, a process execution consists of alternating CPU bursts and I/O bursts:
1. CPU Burst: The period during which a process is executing instructions on the CPU
without waiting for input/output (I/O) operations. This involves computations, memory
access, and other processing tasks.
2. I/O Burst: The period during which a process is waiting for or performing input/output
operations, such as reading from a disk, writing to a file, or receiving user input. During
this time, the CPU is idle for that process, and the system may schedule another process
for execution.
A process typically experiences multiple CPU and I/O bursts throughout its execution, leading to
an alternating CPU-I/O burst cycle until completion.
Dispatcher:
Dispatcher is a special type of program whose work starts after the scheduler. When the
scheduler completes its task of selecting a process, it is the dispatcher that moves the process
to the queue it needs to go to.
The dispatcher is the module that hands over control of the CPU to the process that has been
selected by the short-term scheduler.
The following things happen in this function:
1. switching context: Cores the current process and restores the state of the process
to be run next.
2. Switching to User Mode: Makes sure that it runs in the user mode and not kernel
mode, which is for security and privilege break-
3. Jumps to the correct location in the user program from where the program can be
restarted.
Advantages of a Dispatcher
Fast Process Switching: Evaluates circumstances where a procedure shifts from the
ready queue to the execution phase in a way that causes less delay.
Efficient CPU Time Allocation: Is important in making sure that processes receive
CPU time, hence giving the necessary support for multitasking to occur.
Disadvantages of a Dispatcher
Dispatch Latency: Although the time taken is considerably small, this lateness in
dispatching the requests can slow down the system.
Dependent on Scheduler: The dispatcher hence cannot work on her own since she
is reliant on the decisions made by the scheduler.
Scheduling criteria:
There are several criteria that the CPU scheduler considers when selecting the next process to
be executed. These criteria help in evaluating the performance of different scheduling
algorithms & determining which one is best suited for a particular situation. The main criteria of
CPU scheduling are:
1. CPU Utilization: It refers to the percentage of time the CPU is busy executing processes. The
goal is to maximize CPU utilization & minimize idle time.
2. Throughput: It is the number of processes completed per unit time. A higher throughput
indicates that the system is able to execute more processes in a given time period.
3. Turnaround Time: It is the total time taken by a process from its submission to its
completion, including waiting time & execution time. The aim is to minimize the average
turnaround time.
4. Waiting Time: It is the amount of time a process spends waiting in the ready queue before it
gets executed. The objective is to minimize the average waiting time.
5. Response Time: It is the time between the submission of a request & the first response from
the system. The goal is to minimize the average response time.
Scheduling algorithms:
CPU scheduling is a process used by the operating system to decide which task or process
gets to use the CPU at a particular time. This is important because a CPU can only handle one
task at a time, but there are usually many tasks that need to be processed. The following are
different purposes of a CPU scheduling time.
Maximize the CPU utilization
Minimize the response and waiting time of the process.
Priority scheduling:
Priority scheduling is one of the most common scheduling algorithms used by the operating
system to schedule processes based on their priority. Each process is assigned a priority. The
process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on a first-come first served basis. Priority can
be decided based on memory requirements, time requirements or any other resource
requirement. Also priority can be decided on the ratio of average I/O to average CPU burst
time.
Priority Scheduling can be implemented in two ways:
Non-Preemptive Priority Scheduling
Preemptive Priority Scheduling
Non-Preemptive Priority Scheduling
In Non-Preemptive Priority Scheduling, the CPU is not taken away from the running process.
Even if a higher-priority process arrives, the currently running process will complete first.
Ex: A high-priority process must wait until the currently running process finishes.
In Preemptive Priority Scheduling, the CPU can be taken away from the currently running
process if a new process with a higher priority arrives.
Ex: A low-priority process is running, and a high-priority process arrives; the CPU immediately
switches to the high-priority process.
Deterministic Modeling
This evaluation method takes a predetermined workload and evaluates each algorithm using
that workload.
Assume we are presented with the following processes, which all arrive at time zero.
P1 9
P2 33
P3 2
P4 5
P5 14
Queuing Models
Another method of evaluating scheduling algorithms is to use queuing theory. Using data from
real processes we can arrive at a probability distribution for the length of a burst time and the
I/O times for a process. We can now generate these times with a certain distribution.
We can also generate arrival times for processes (arrival time distribution).
If we define a queue for the CPU and a queue for each I/O device we can test the various
scheduling algorithms using queuing theory.
Knowing the arrival rates and the service rates we can calculate various figures such as average
queue length, average wait time, CPU utilization etc.
One useful formula is Little's Formula.
n = λw Where
n is the average queue length
λ is the average arrival rate for new processes (e.g. five a second)
w is the average waiting time in the queue
Knowing two of these values we can, obviously, calculate the third. For example, if we know that
eight processes arrive every second and there are normally sixteen processes in the queue we
can compute that the average waiting time per process is two seconds.
The main disadvantage of using queuing models is that it is not always easy to define realistic
distribution times and we have to make assumptions. This results in the model only being an
approximation of what actually happens.
Simulations
Rather than using queuing models we simulate a computer. A Variable, representing a clock is
incremented. At each increment the state of the simulation is updated.
Statistics are gathered at each clock tick so that the system performance can be analysed.
The data to drive the simulation can be generated in the same way as the queuing model,
although this leads to similar problems.
Alternatively, we can use trace data. This is data collected from real processes on real machines
and is fed into the simulation. This can often provide good results and good comparisons over a
range of scheduling algorithms.
However, simulations can take a long time to run, can take a long time to implement and the
trace data may be difficult to collect and require large amounts of storage.
Implementation
The best way to compare algorithms is to implement them on real machines. This will give the
best results but does have a number of disadvantages.
· It is expensive as the algorithm has to be written and then implemented on real hardware.
· If typical workloads are to be monitored, the scheduling algorithm must be used in a live
situation. Users may not be happy with an environment that is constantly changing.
· If we find a scheduling algorithm that performs well there is no guarantee that this state will
continue if the workload or environment changes.
Unit – 3
Memory management:
The main memory is central to the operation of a Modern Computer. Main Memory is a large
array of words or bytes, ranging in size from hundreds of thousands to billions. Main memory
is a repository of rapidly available information shared by the CPU and I/O devices. Main
memory is the place where programs and information are kept when the processor is
effectively utilizing them. Main memory is associated with the processor, so moving
instructions and information into and out of the processor is extremely fast. Main memory is
also known as RAM (Random Access Memory). This memory is volatile. RAM loses its data
when a power interruption occurs.
Main Memory
Address binding:
The Address Binding refers to the mapping of computer instructions and data to physical
memory locations. Both logical and physical addresses are used in computer memory. It assigns
a physical memory region to a logical pointer by mapping a physical address to a logical address
known as a virtual address. It is also a component of computer memory management that the
OS performs on behalf of applications that require memory access.
There are mainly three types of an address binding in the OS. These are as follows:
Types of Partitions
Contiguous memory allocation can be achieved when we divide the memory into the following
types of partitions:
1. Fixed-Sized Partitions
Another name for this is static partitioning. In this case, the system gets divided into multiple
fixed-sized partitions. In this type of scheme, every partition may consist of exactly one process.
This very process limits the extent at which multiprogramming would occur, since the total
number of partitions decides the total number of processes. Read more on fixed-sized
partitions here.
2. Variable-Sized Partitions
Dynamic partitioning is another name for this. The scheme allocation in this type of partition is
done dynamically. Here, the size of every partition isn’t declared initially. Only once we know
the process size, will we know the size of the partitions. But in this case, the size of the process
and the partition is equal; thus, it helps in preventing internal fragmentation.
On the other hand, when a process is smaller than its partition, some size of the partition gets
wasted (internal fragmentation). It occurs in static partitioning, and dynamic partitioning solves
this issue. Read more on dynamic partitions here.
Definition
Static partitioning divides the memory into fixed-sized partitions at system startup. Each
partition can hold exactly one process, and its size remains constant throughout execution.
Characteristics
Advantages
Disadvantages
❌ Internal Fragmentation – If a process uses less memory than the partition size, the remaining
space is wasted.
❌ Fixed Number of Processes – Cannot exceed the predefined partition limit, even if memory is
available.
❌ Inefficient Utilization – Large processes may not fit in any partition, leading to underutilized
memory.
Dynamic partitioning allows memory to be allocated dynamically based on the process's size,
rather than using fixed partitions.
Characteristics
Advantages
Disadvantages
❌ External Fragmentation – Small free memory blocks form over time, making it difficult to
allocate large processes.
❌ Memory Compaction Overhead – OS may need to rearrange memory to merge free spaces,
increasing CPU load.
❌ Complex Implementation – Requires additional mechanisms to track memory allocation.
Fragmentation:
Fragmentation refers to a process of information storage where the memory space of the
system is used inadequately, thus reducing the overall efficiency or ability or both (sometimes).
The implications of the process of fragmentation depend entirely on the specific allocation of
storage space schemes in the operation along with the particular fragmentation types. In some
instances, fragmentation leads to some unused storage capacity. This concept is also applicable
to the generated unused space in this very situation.
The memory used for the preservation of the data set (like file formats) is very similar to the
other systems (like the FAT file system), irrespective of the amount of fragmentation (it
happens from null to the extreme).
Types of Fragmentation
External Fragmentation
Internal Fragmentation
Data Fragmentation (which exists beside or a combination)
Fragmentation can often be acknowledged when preferring enhancements, usability, or
inefficiency. Similar things might also happen for the other tools, like processors. Let us discuss
what’s the major difference between internal and external fragmentation.
Internal Fragmentation
Whenever a memory block gets allocated with a process, and in case the process happens to be
smaller than the total amount of requested memory, a free space is ultimately created in this
memory block. And due to this, the memory block’s free space is unused. This is what causes
internal fragmentation. Read more on Internal Fragmentation here.
External Fragmentation
Causes of Fragmentation
The user processes are unloaded and loaded from the main memory. Also, all the processes are
kept in the memory blocks in a system’s main memory. Various spaces are left after the loading
and swapping of processes that other processes can’t load because of their sizes. The main
memory is available, but the space isn’t sufficient in order to load other processes since the
allocation of the main memory processes is dynamic.
Fragmentation comes with various pros and cons. Let us look into some of the prominent ones
now.
Pros
Storage optimization
The fragmented systems might make potentially better use of the storage devices by using
every storage block available to us.
Fewer failures
If a system has insufficient sequential space, then it won’t support fragmentation and would
ultimately fail. No issues would arise.
The data written in a system supporting data fragmentation can be faster than the reorganizing
data storage. It enables data writes in a contiguous manner.
Cons
The storage device with more fragmentation would ultimately degrade over time. Thus, there
would be a necessary requirement for time-consuming operations of defragmentation.
The amount of time taken to read a non-sequential file may increase when a storage device
happens to become more fragmented.
Memory management keeps track of the status of each memory location, whether it is
allocated or free. It allocates the memory dynamically to the programs at their request and
frees it for reuse when it is no longer needed. Memory management meant to satisfy some
requirements that we should keep in mind.
These Requirements of memory management are:
1. Relocation – The available memory is generally shared among a number of
processes in a multiprogramming system, so it is not possible to know in advance
which other programs will be resident in main memory at the time of execution of
this program. Swapping the active processes in and out of the main memory
enables the operating system to have a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible
that when it is swapped back into main memory then it occupies the previous
memory location, since the location may still be occupied by another process. We
may need to relocate the process to a different area of memory. Thus there is a
possibility that program may be moved in main memory due to swapping.
The figure depicts a process image. The process image is occupying a continuous
region of main memory. The operating system will need to know many things
including the location of process control information, the execution stack, and the
code entry. Within a program, there are memory references in various instructions
and these are called logical addresses.
After loading of the program into main memory, the processor and the operating
system must be able to translate logical addresses into physical addresses. Branch
instructions contain the address of the next instruction to be executed. Data
reference instructions contain the address of byte or word of data referenced.
2. Protection – There is always a danger when we have multiple programs at the
same time as one program may write to the address space of another program. So
every process must be protected against unwanted interference when other
process tries to write in a process whether accidental or incidental. Between
relocation and protection requirement a trade-off occurs as the satisfaction of
relocation requirement increases the difficulty of satisfying the protection
requirement.
Prediction of the location of a program in main memory is not possible, that’s why
it is impossible to check the absolute address at compile time to assure protection.
Most of the programming language allows the dynamic calculation of address at
run time. The memory protection requirement must be satisfied by the processor
rather than the operating system because the operating system can hardly control
a process when it occupies the processor. Thus it is possible to check the validity of
memory references.
3. Sharing – A protection mechanism must have to allow several processes to access
the same portion of main memory. Allowing each processes access to the same
copy of the program rather than have their own separate copy has an advantage.
For example, multiple processes may use the same system file and it is natural to
load one copy of the file in main memory and let it shared by those processes. It is
the task of Memory management to allow controlled access to the shared areas of
memory without compromising the protection. Mechanisms are used to support
relocation supported sharing capabilities.
4. Logical organization – Main memory is organized as linear or it can be a one-
dimensional address space which consists of a sequence of bytes or words. Most of
the programs can be organized into modules, some of those are unmodifiable
(read-only, execute only) and some of those contain data that can be modified. To
effectively deal with a user program, the operating system and computer hardware
must support a basic module to provide the required protection and sharing. It has
the following advantages:
Modules are written and compiled independently and all the references
from one module to another module are resolved by `the system at run
time.
Different modules are provided with different degrees of protection.
There are mechanisms by which modules can be shared among
processes. Sharing can be provided on a module level that lets the user
specify the sharing that is desired.
5. Physical organization – The structure of computer memory has two levels referred
to as main memory and secondary memory. Main memory is relatively very fast
and costly as compared to the secondary memory. Main memory is volatile. Thus
secondary memory is provided for storage of data on a long-term basis while the
main memory holds currently used programs. The major system concern between
main memory and secondary memory is the flow of information and it is
impractical for programmers to understand this for two reasons:
Virtual Memory:
Virtual memory is a memory management technique used by operating systems to give the
appearance of a large, continuous block of memory to applications, even if the physical
memory (RAM) is limited. It allows larger applications to run on systems with less RAM.
The main objective of virtual memory is to support multiprogramming, The main
advantage that virtual memory provides is, a running process does not need to be
entirely in memory.
Programs can be larger than the available physical memory. Virtual Memory
provides an abstraction of main memory, eliminating concerns about storage
limitations.
A memory hierarchy, consisting of a computer system’s memory and a disk,
enables a process to operate with only some portions of its address space in RAM
to allow more processes to be in memory.
A virtual memory is what its name indicates- it is an illusion of a memory that is larger than
the real memory. We refer to the software component of virtual memory as a virtual memory
manager. The basis of virtual memory is the noncontiguous memory allocation model. The
virtual memory manager removes some components from memory to make room for other
components.
Demand paging:
Demand paging is a memory management scheme used in operating systems to improve
memory usage and system performance. Let’s understand demand paging with real life
example Imagine you are reading a very thick book, but you don’t want to carry the entire
book around because it’s too heavy. Instead, you decide to only bring the pages you need as
you read through the book. When you finish with one page, you can put it away and grab the
next page you need.
In a computer system, the book represents the entire program, and the pages are parts of the
program called “pages” of memory. Demand paging works similarly: instead of loading the
whole program into the computer’s memory at once (which can be very large and take up a
lot of space), the operating system only loads the necessary parts (pages) of the program
when they are needed.
This concept says that we should not load any pages into the main memory until we need
them, or keep all pages in secondary memory until we need them.
What is Demand Paging?
Demand paging is a technique used in virtual memory systems where pages enter main
memory only when requested or needed by the CPU. In demand paging, the operating system
loads only the necessary pages of a program into memory at runtime, instead of loading the
entire program into memory at the start. A page fault occurred when the program needed to
access a page that is not currently in memory.
The operating system then loads the required pages from the disk into memory and updates
the page tables accordingly. This process is transparent to the running program and it
continues to run as if the page had always been in memory.
What is Page Fault?
The term “page miss” or “page fault” refers to a situation where a referenced page is not
found in the main memory.
When a program tries to access a page, or fixed-size block of memory, that isn’t currently
loaded in physical memory (RAM), an exception known as a page fault happens. Before
enabling the program to access a page that is required, the operating system must bring it
into memory from secondary storage (such a hard drive) in order to handle a page fault.
In modern operating systems, page faults are a common component of virtual memory
management. By enabling programs to operate with more data than can fit in physical
memory at once, they enable the efficient use of physical memory. The operating system is
responsible for coordinating the transfer of data between physical memory and secondary
storage as needed.
What is Thrashing?
Thrashing is the term used to describe a state in which excessive paging activity takes place in
computer systems, especially in operating systems that use virtual memory, severely
impairing system performance. Thrashing occurs when a system’s high memory demand and
low physical memory capacity cause it to spend a large amount of time rotating pages
between main memory (RAM) and secondary storage, which is typically a hard disc.
It is caused due to insufficient physical memory, overloading and poor memory management.
The operating system may use a variety of techniques to lessen thrashing, including lowering
the number of running processes, adjusting paging parameters, and improving memory
allocation algorithms. Increasing the system’s physical memory (RAM) capacity can also
lessen thrashing by lowering the frequency of page swaps between RAM and the disc.
Pure Demand Paging
Pure demand paging is a specific implementation of demand paging. The operating system
only loads pages into memory when the program needs them. In on-demand paging only, no
pages are initially loaded into memory when the program starts, and all pages are initially
marked as being on disk.
Operating systems that use pure demand paging as a memory management strategy do so
without preloading any pages into physical memory prior to the commencement of a task.
Demand paging loads a process’s whole address space into memory one step at a time,
bringing just the parts of the process that are actively being used into memory from disc as
needed.
It is useful for executing huge programs that might not fit totally in memory or for computers
with limited physical memory. If the program accesses a lot of pages that are not in memory
right now, it could also result in a rise in page faults and possible performance overhead.
Operating systems frequently use caching techniques and improve page replacement
algorithms to lessen the negative effects of page faults on system performance as a whole.
Working Process of Demand Paging
Let us understand this with the help of an example. Suppose we want to run a process P
which have four pages P0, P1, P2, and P3. Currently, in the page table, we have pages P1 and
P3.
The operating system‘s demand paging mechanism follows a few steps in its operation.
Program Execution: Upon launching a program, the operating system allocates a
certain amount of memory to the program and establishes a process for it.
Creating Page Tables: To keep track of which program pages are currently in
memory and which are on disk, the operating system makes page tables for each
process.
Handling Page Fault: When a program tries to access a page that isn’t in memory
at the moment, a page fault happens. In order to determine whether the
necessary page is on disk, the operating system pauses the application and
consults the page tables.
Page Fetch: The operating system loads the necessary page into memory by
retrieving it from the disk if it is there.
The page’s new location in memory is then reflected in the page table.
Resuming The Program: The operating system picks up where it left off when the
necessary pages are loaded into memory.
Page Replacement: If there is not enough free memory to hold all the pages a
program needs, the operating system may need to replace one or more pages
currently in memory with pages currently in memory. on the disk. The page
replacement algorithm used by the operating system determines which pages are
selected for replacement.
Page Cleanup: When a process terminates, the operating system frees the memory
allocated to the process and cleans up the corresponding entries in the page
tables.
How Demand Paging in OS Affects System Performance?
Demand paging can improve system performance by reducing the memory needed for
programs and allowing multiple programs to run simultaneously. However, if not
implemented properly, it can cause performance issues. When a program needs a part that
isn’t in the main memory, the operating system must fetch it from the hard disk, which takes
time and pauses the program. This can cause delays, and if the system runs out of memory, it
will need to frequently swap pages in and out, increasing delays and reducing performance.
Common Algorithms Used for Demand Paging in OS
Demand paging is a memory management technique that loads parts of a program into
memory only when needed. If a program needs a page that isn’t currently in memory, the
system fetches it from the hard disk. Several algorithms manage this process:
FIFO (First-In-First-Out): Replaces the oldest page in memory with a new one. It’s
simple but can cause issues if pages are frequently swapped in and out, leading to
thrashing.
LRU (Least Recently Used): Replaces the page that hasn’t been used for the
longest time. It reduces thrashing more effectively than FIFO but is more complex
to implement.
LFU (Least Frequently Used): Replaces the page used the least number of times. It
helps reduce thrashing but requires extra tracking of how often each page is used.
MRU (Most Recently Used): Replaces the page that was most recently used. It’s
simpler than LRU but not as effective in reducing thrashing.
Random: Randomly selects a page to replace. It’s easy to implement but
unpredictable in performance.
What is Thrashing?
At any given time, only a few pages of any process are in the main memory, and therefore
more processes can be maintained in memory. Furthermore, time is saved because unused
pages are not swapped in and out of memory. However, the OS must be clever about how it
manages this scheme. In the steady state practically, all of the main memory will be occupied
with process pages, so that the processor and OS have direct access to as many processes as
possible. Thus when the OS brings one page in, it must throw another out. If it throws out a
page just before it is used, then it will just have to get that page again almost immediately.
Too much of this leads to a condition called Thrashing. The system spends most of its time
swapping pages rather than executing instructions. So a good page replacement algorithm is
required.
In the given diagram, the initial degree of multiprogramming up to some extent of
point(lambda), the CPU utilization is very high and the system resources are utilized 100%.
But if we further increase the degree of multiprogramming the CPU utilization will drastically
fall down and the system will spend more time only on the page replacement and the time
taken to complete the execution of the process will increase. This situation in the system is
called thrashing.
Causes of Thrashing
Thrashing occurs in a computer system when the CPU spends more time swapping pages in
and out of memory than executing actual processes. This happens when there is insufficient
physical memory, causing frequent page faults and excessive paging activity. Thrashing
reduces system performance and makes processes run very slowly. There are many cause of
thrashing as discussed below.
1. High Degree of Multiprogramming
If the number of processes keeps on increasing in the memory then the number of frames
allocated to each process will be decreased. So, fewer frames will be available for each
process. Due to this, a page fault will occur more frequently and more CPU time will be
wasted in just swapping in and out of pages and the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of processes = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per
process are decreased. Hence CPU time will be consumed just by swapping pages.
2. Lacks of Frames
If a process has fewer frames then fewer pages of that process will be able to reside in
memory and hence more frequent swapping in and out will be required. This may lead to
thrashing. Hence a sufficient amount of frames must be allocated to each process in order to
prevent thrashing.
Recovery of Thrashing
Do not allow the system to go into thrashing by instructing the long-term scheduler
not to bring the processes into memory after the threshold.
If the system is already thrashing then instruct the mid-term scheduler to suspend
some of the processes so that we can recover the system from thrashing.
A Page Fault occurs when you access a page that has been flagged as invalid. Then, while
translating the address across the page table, the paging hardware will notice that the incorrect
bit is set, triggering a trap in the operating system.
This trap is mostly caused by the Operating System's failure to put the desired page into
memory.
Let's look at the technique for dealing with a page fault, as depicted in the diagram above:
To begin, an internal table (typically the process control block) was created for this
process to identify whether the reference was valid or invalid memory access.
If the reference is invalid, the process will be terminated. If the reference is correct,
but we haven't yet purchased that page, we'll just page it in.
Then, in order to find the free frame, we locate the free frame list.
In order to read the required page into the newly allocated frame, a disc operation
is now scheduled.
When the disc is entirely read, the internal table that is kept with the process is
amended, and the page table, which primarily shows that the page is now in
memory, is updated.
We'll now restart the instruction that was interrupted due to the trap. The page can
now be accessed as if it had always been in memory by the process.
Page Miss: It happens when a program accesses a memory page that is not
currently residing in the main memory. This triggers a page fault and requires the
OS to bring the missing page into RAM from secondary storage
Page Fault Time: It refers to the total time taken to handle a page fault. It includes
the time to find the required page on disk, swap it into RAM, update data
structures, and restart the interrupted program
Page Fault Delay: This term denotes the time interval between the moment a page
fault occurs and the time when the corresponding page is fully loaded into the main
memory, allowing the program to continue its execution
Hard Page Fault: It takes place when the missing page is not present in any
available storage, including both RAM and secondary storage. This situation often
requires complex memory management techniques, such as swapping pages to disk
Soft Page Fault: It occurs when the required page is found elsewhere in memory or
storage but hasn't been assigned to the correct address space of the requesting
process. It's generally easier to resolve than a hard page fault
Minor Page Fault: It signifies that the page being accessed is already in memory,
but it is marked as not yet loaded. It typically happens when a program first
accesses a memory page, causing the OS to load it into the main memory for future
use.