0% found this document useful (0 votes)
23 views67 pages

Unit 1

unit 1 fos BCA sem 3

Uploaded by

useracc85943515
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)
23 views67 pages

Unit 1

unit 1 fos BCA sem 3

Uploaded by

useracc85943515
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
You are on page 1/ 67

COURSE TITLE Fundamentals of Operating System

COURSE CODE CC-204


TEACHING
UNIT TOPIC / SUB TOPIC HOURS
Introduction to Operating System & Memory Management 10
• Introduction to Operating System
o What is Operating System?
o Operating system software 2
o Types of Operating System
• Memory Management: Early System
o Single User Contiguous Scheme
o Fixed Partitions 3
o Dynamic Partitions
o Allocation and deallocation methods
1 o Relocatable Dynamic Partitions
• Memory Management: Virtual Memory
o Paged Memory Allocation
o Demand Paging 5
o Page Replacement Algorithms
First In First Out
Least Recently Used
o Segmented Memory allocation
o Segmented/Demand Paged Memory allocation
o Virtual Memory
7/25/2024 L J College of Computer Applications
What is an Operating System?
Defination: An operating system, or "OS,"is the part of the computer
system which manages all of the software and hardware on the computer. It
is software that communicates with the hardware and allows other
programs to run. In other words we can say that it controls every file, every
device, every part of the memory, every nanosecond of processing.
Memory Manager
Operating system Processor Manager
Device Manager
software
File Manager

7/25/2024 L J College of Computer Applications


Operating System Software:
So OS is a software or a huge collection of programs, to be precise a system software which
manages the entire computer system.
OS:
Huge collection of programs

Operating System works as


Allocation of memory Monitors devices
Keep track of allocation Memory Device Allocates devices
Deallocate memory Deallocates devices
Manager Manager
(incharge of
RAM)

Processor File
Handles incoming jobs Manager Manager Manages all files
Allocate CPU (manages CPU) Creation
Keep track of status of Restrictions on files
Process Deletion
Deallocates the CPU

7/25/2024 L J College of Computer Applications


All the managers of the OS work together to carry out the operations of the computer system.
For example:
User types a command

Device Manager decodes the keystrokes to form a command.


This command is then sent to command user interface.

Processor Manager then validates the command and sends


acknowledgement to user through monitor. Checks location of
program.

If program is in memory, File Manager finds it location, asks


device manager to retrieve the file and sends it to the Memory
manager which alloactes location to this file and makes note of
the address of location

When program is in memory the Memory Manager keeps track


of it while it is being executed by the Processor Manager.

When program is complete the Processor Manager sends


message to Device Manager which is responsible to display the
7/25/2024 execution complete message
L J College on monitor
of Computer for the user.
Applications
Types of Operating System:

1. Batch systems: In a batch operating system the OS collects the programs in a


batch before processing starts. Jobs are batched together by the type of job and
their requirements or jobs with similar needs.

2. Interactive or Time Sharing Systems: The user directly interacts with the
system through an interface. In these systems each process is allocated a
particular time span and the process has to finish its execution within that time
span. After the time span is over the CPU is allocated to the next process for
execution. Here the response time is less as compared to batch systems

3. Real time systems: This type of OS deals with real time appliactions. It is
used in time critical environment where the data needs to be processed very
fast without any buffer delays because some immediate decisions needs to be
taken based on the output. Such OS’s are used in space flights, air traffic
control, field of medicine etc.

4. Hybrid systems: It is a combination of batch and interactive systems. It can


work in the interactive mode
7/25/2024
with fast response time and at the same time
L J College of Computer Applications
carry out batch jobs inn the background.
MEMORY MANAGEMENT

Types of Memory Allocation Scheme to manage memory in early systems

Single user Fixed partition Dynamic partition Relocatable


Contiguous scheme Dynamic partition

Single User Contiguous scheme: Entire Program to be processed is loaded in the memory.
Program is loaded at contiguous locations.

Drawbacks:
1. If the size of the program is more than the available memory, the program cannot get
executed. So either memory is increased or program is modified to be made more compact.
2. It does not support multiprogramming.

7/25/2024 L J College of Computer Applications


Fixed Partition Scheme : (supported multiprogramming)
 The entire memory was divided into partitions of fixed sizes. The sizes of these partitions
were defined by the OS when the system is switched on. Once the partitions are created
the sizes cannot be changed till the system is restarted.
 One job was loaded on a one partition. OS had to take care to protect the boundaries of
each partition. Entire program gets loaded at contiguous locations.
when Job requests memory OS checks for partition to fit job completely

if job size > size of partition


then
Reject Job
else
if partition is free
then
load job into partition
change status of partition as “busy”
else (if partition is not free)
Put Job in waiting queue.

7/25/2024 L J College of Computer Applications


7/25/2024 L J College of Computer Applications
Advantage over Single User Contiguous Scheme
Many programs can be in memory at a time (Multiprogramming)

Disadvanatage:
• Entire Program needs to be loaded on the memory
• The program uses continuous locations of the memory.
• If the partition sizes are too small : large jobs will rejected.
• If partition sizes are too big : memory space within the partition is wasted as the job does
not occupy the entire partition. This unused memory space within the partition cannot be
given to any other job. So a fragment of the partition goes waste and this unused space
within the partition is known as Internal Fragmentation, which is the major drawback of
Fixed Partition scheme.

Partition size 50 K

Job
Job 12 (35 K)
(25K)
Unused space (15K)

Internal fragmenatation of 15 K
7/25/2024 L J College of Computer Applications
Dynamic Partition Scheme : (supported multiprogramming)
When a job is loaded for processing it is allocated as much memory as it requests for i.e. a
partition is created which is equal to the size of the job and is allocated to the job.

A major advantage of dynamic partition over fixed partition:


Since the size of job = size of partition no memory space is wasted within the partition. So
no internal fragmentation.
Jobs coming in into the system keep occupying partitions. Completed jobs leave the partition
making them free or available for other jobs coming in.

If there are many free partitions on the system but they are not allocated to jobs as the size of
the partition is less than the size of the job then that memory space goes waste. This fragment
or block of memory which cannot be allocated to any job because its size is less than the size
of the job goes waste and this is known as External Fragmentation, which is the major
drawback of Dynamic Partition Scheme.

Back

7/25/2024 L J College of Computer Applications


External
fragmentation

7/25/2024 L J College of Computer Applications


BEST FIT & FIRST FIT ALLOCATION

In case of fixed and dynamic partition the memory manager part of OS needs to keep list of
busy/free memory locations. This list is checked whenever a new job comes into the system.
Empty locations are allocated based on:
 First Fit
 Best Fit

First Fit: In this allocation scheme the memory manager maintains the list of busy/free
partitions. When a job is to be loaded , the first free partition which fits the requirement is used
for loading this job. Advantage here is that allocation is done fast.

Best Fit: In this allocation scheme the memory manager maintains the list of busy/free
partitions in order of size (eg. Smallest to largest). When a job is to be loaded , the partition
whose size closely matches the size of the job (minimum or no wastage of memory) is used for
loading this job. Advantage here is there is no or minimum wastage of memory space.
First Fit

Best Fit

7/25/2024 L J College of Computer Applications


Examples to do

First Fit:

Total Available: 115 k


Total Used: 40K
Unused (Internal Fragmentation+ Free partition): (20K+5K+30K)+20K free partition)=75K
So 757/25/2024
K of space is unused but still a Lfile J3 of
J College ofComputer
size 30K cannot be loaded.
Applications
Example of Best Fit

Total Available: 115 k Total Used: 70K


Total Unused (Internal Fragmentation): 115K-70K = 45K
So 45 K of space is unused and all four jobs are there on the memory.

With four Jobs J1, J2, J3, J4…. Comparison between First Fit and Best Fit
First Fit Best Fit
Total Available 115 K 115 K
Total Used 40K 70K
Number of Jobs in memory
7/25/2024
3 L J College of Computer Applications
4
Internal fragmentation 75K 45K
Practice Examples 1

1. Job List
J1 40K
J2 30K
J3 20K
J4 35K

Memory Block Size


M1 20K
M2 50K
M3 40K
M4 30K

Solve using First Fit and Best Fit

7/25/2024 L J College of Computer Applications


Practice Example 2

1. Job List
J1 30K
J2 40K
J3 50K

Memory Block Size


M1 70K
M2 50K
M3 40K

Solve using First Fit and Best Fit

7/25/2024 L J College of Computer Applications


Practice Example 3
Job List
J1 50K
J2 40K
J3 30K
J4 35K
Memory Block Size
M1 30K
M2 60K
M3 50K
M4 40K
Solve using First Fit and Best Fit

Practice Example 4
1. Job List
J1 40K
J2 50K
J3 60K

Memory Block Size


M1 80K
M2 60K
M3 50K

Solve 7/25/2024
using First Fit and Best Fit L J College of Computer Applications
DEALLOCATION OF MEMORY

Deallocation of Memory takes place when the job has finished execution and releases memory.

Deallocation in case of fixed Partition:Slide 8


When the job leaves the system the status of the partition where it was stored is changed from
Busy to Free eg. A bit can be used to indicate presence of a job in a partition. The bit 0 indicates
free partition and the bit having 1 indicates the partition to be Busy.

Deallocation in case of dynamic Partition: has three situationsSlide 11 J1


1. when the block to be dealloacted is near another free block Free partition
for eg J1 has completed execution and is releasing memory J2
J3
2. when the block to be deallocated is between two free blocks
for eg J2 is releasing memory J1
Free partition

J2
Free partition

Free partition
3. when the block to be deallocated is isolated
for eg: J3 is releasing memory J2
J4
7/25/2024 J3
L J College of Computer Applications
Assume that the memory locations are numbered lowest to highest in the memory addressing
scheme.
1. Joining Two free blocks: when the block to be dealloacted is near another free block

when the block to be dealloacted is near


other free block for eg J1 has completed
execution and is releasing memory
Block to
be freed
J1
Free partition
Free block
J2
J3
This is a free list.
A block starting from 7600 of size 200 (ie.
From 7600-7799) has to be deallocated. This
block is adjacent to a free block starting
from 7800 of size 5.
After deallocation these two blocks are
combined to form one large block of size
205 (200+5) and the staring address of the
block is 7600

7/25/2024 L J College of Computer Applications


Free- Busy List to be deallocated
Beginning Memory Address 4000
Address block size Status Job locations Free
free
4000 100 Free (4000-4099) 4100
4100 50 Busy J4 (4100-4149) 50 lines (J4)
4150 200 Busy J2 (4150-4349) 4150
4350 150 Busy J1 (4350-4499) 200 lines (J2)
4500 40 Free (4500-4539) 4350
4540 100 Busy J5 (4540-4640) 150 lines (J1)
J1 (150 lines)
……………….. 4500
……………….. Free
free (40
(40 lines)
lines)
4540
100 lines(J5)
4640
Free- Busy List After J1 deallocates memory 4000
Beginning Memory Address
Address block size Status Job locations J4
4000 100 Free (4000-4099) 4150
4100 50 Busy J4 (4100-4149)
4150 200 Busy J2 (4150-4349) J2
4350 190 Free (4350-4539) 4350
4540 100 Busy J5 (4540-4640) Free 190 lines
………………..
………………..
7/25/2024 L J College of Computer Applications 4540
2. Joining Three free blocks: when the block to be deallocated is between two free blocks
for eg J2 is releasing memory J1
Free partition

J2
Free partition

Free Block
Here a block with starting address
Block to be deallocated 7580 and of size 20 has to be
deallocated. It is sandwiched by
Free Block
two free blocks at location 7560
of size 20 and another at location
7600 of size 205. When the block
at 7580 is deallocated the block
above and below it gets
combined to form one big free
block of size 245 (20+20+205).

This free block gets a starting


address of the lower order
memory i.e 7560.

7/25/2024 L J College of Computer Applications


3. Deallocating an isolated Block: when the block to be deallocated is isolated
Free partition
for eg: J3 is releasing memory
The OS maintains a busy and a free list as : J2
LIST BEFORE DEALLOCATION J4
J3

Busy
Busy Blocks
Blocks The block whose starting address is 8805
is the block which has to be deallocated.
This block is isolated i.e, it does not have
any free block adjacent to it.

LIST AFTER DEALLOCATION

On deallocation a null is entered against


that block in the Busy list.
and because that block having beginning
address of 8805 and is of size 445 is now
free, its entry is made in the Free list.

usy
7/25/2024 L J CollegeUll
of entry
Computer Applications
usy
RELOCATABLE DYNAMIC PARTITION dp Back Main

The major disadvantage of Dynamic Partition scheme is External Fragmentation. The small
blocks of memory cannot be given to any job as the size of fragment is smaller than the size
of the Job. Such blocks of memory are scattered and go waste.
10K
Job 1 Job 1
15K
External Job 4
Job 4 fragmentation
(unused space)
10K
Job 6

Job 6 Job 5

Job 5 10K+15K + 10K + 5K


5K combined = 40K free space

A job of size 20K cannot be loaded All empty blocks collected to create one large
block of free space which can now accommodate
a Job of size 20
This is known as COMPACTION or GARBAGE COLLECTION or DEFRAGMENTATION . This is
performed
7/25/2024 by the OS to remove small chunks
L J College of waste
of Computer memory.
Applications
COMPACTION or GARBAGE COLLECTION or DEFRAGMENTATION results in shifting the
location of programs in the memory which means they are relocated.
Memory Address Before Compaction Memory Address After Compaction
J1 60 50
J4 115 90
J6 225 190
10K
Job 1 Job 1
15K
Job 4
Job 4

10K Job 6

Job 6 Job 5

Job 5 10K+15K + 10K + 5K


5K = 40K free space

After compaction the memory address of all the jobs changes.


For Example J1 was at 60 now it is at 50 i.e., 10 positions back
J5 was at 375 now it is at 340 i.e., 35 positions back.
And now we have a large free block of size 40K.
7/25/2024 L J College of Computer Applications
Consider the Job J4: All the instructions of the program change their address on relocation
The program on Line 115 shifts to 90 (shift by 25 lines)
116 shifts to 91 (shift by 25 lines)
117 shifts to 92 and so on line 215 shifts to 190 (shift by 25 lines)
For a program J1 all its instructions shift 25 lines up
Before Compaction After Compaction

J1
J1 90
91
115 190
200
J4
116
J4 215 400 J3
400
401
J3 .
600
Similarly J3 before compaction was from 400 to 600. After compaction it is relocated and is
stored from 200 to 400
The program on Line 400 shifts to 200 (shift by 200 lines)
401 shifts to 201 (shift by 200 lines)
7/25/2024 600 shifts to 400 (shift
L J College by 200Applications
of Computer lines)
So we see that each program moves by a specific number of address locations
For a program J1 all its instructions shift 25 lines up
For a program J3 all its instructions shift 200 lines up

So, The memory manager part of the OS keeps a register for each Job which stores the number
of locations (up or down) it has been shifted or relocated after compaction. This register is
called the RELOCATION REGISTER.

So for a Job J1 value of Relocation Register is -25 (minus because instructions shift up)
for Job J3 value of Relocation Register is -200.

After relocation when the Instructions of Job 1 have to be read it is calculated as


Old address location+ Value of relocation register = New address location
Eg. First instruction : 115 + (-25) = 90
Second Instruction : 116 + (-25) = 91 and so on
….. Last Instruction : 215 + (-25) = 190

Simmilarly for J3
Old address location+ Value of relocation register = New address location
Eg. First instruction : 400 + (-200) = 200
Second Instruction : 401 + (-200) = 201 and so on
….. Last Instruction : 600 + (-200) = 400
7/25/2024 L J College of Computer Applications
A RELOCATION REGISTER can have a value positive , negative or Zero

If the Job has moved up after relocation (towards lower address i.e., 400 to 350 ) the
relocation register will hold a negative value

If the Job has moved down after relocation (towards higher address i.e., 400 to 750 ) the
relocation register will hold a positive value

If the Job does not move on compaction value of the relocation register will be 0 (Zero).

Compaction can be done when:


 When large amount of memory becomes busy
 When many jobs are waiting to be allocated memory
 After specific amount of time

BOUNDS REGISTER
Another register used by the operating system is the Bounds register. This register contains a
the highest address value that can be accessed by a program during execution. Suppose a
program stores a value 375 in the Bounds register , during execution if the program has any
instruction like GO TO address value ,
For eg: GO TO 296 is a valid instruction as it is less than the value of the bound register 375
GO TO 435 is out of bounds.
7/25/2024 L J College of Computer Applications
MEMORY MANAGEMENT >>>>>>>>>>>>>RECENT SYSTEMS MM

Paged Memory Allocation


In Paged Memory Allocation the program which is to be loaded on the RAM is divided into parts
or sections called PAGES.
The secondary memory is divided into sections called Sectors or Blocks
The Program is divided into sections called Pages
The RAM is divided into sections called Page Frames

The Program divided into Pages Page Frames of RAM


are held by sectors of Secondary Memory is loaded onto
Secondary Memory Random Access Memory
J1 (page 0) J1 (page 1) PF 0

sectors J1 (page 1) PF 1

J1 (page 2) J2 (page 0) PF 2 PAGE


FRAMES
J2 (page 0) J1 (page 0) PF 3
J2 (page 1) PF 4

. J1 (page 2) PF 5

. J2 (page 1) PF 6
7/25/2024 . L J College of Computer Applications PF 7
The Program divided into Pages Page Frames of RAM
are held by sectors of Secondary Memory is loaded onto

THE PAGES AND THE PAGE FRAMES ARE OF THE SAME SIZE
The diagram below shows that Job 1 (J1) is divided into 3 pages, J2 is divided into two
pages and so on. These jobs when loaded on the RAM are entirely stored on the RAM
but not at continuous locations.

Secondary Memory Random Access Memory


J1 (page 0)
Pages of J1 J1 (page 1) PF 0

sectors J1 (page 1) PF 1

J1 (page 2) J2 (page 0) PF 2 PAGE


FRAMES
J2 (page 0) J1 (page 0) PF 3
J2 (page 1)
Pages of J2 PF 4

. J1 (page 2) PF 5

. J2 (page 1) PF 6
7/25/2024 . L J College of Computer Applications PF 7
All the pages and page frames are of the same size.
Here the RAM has 11 page
frames for user programs.
Page frame 0 and 1 are
system area.

All pages and page frames


have the capacity to store
100 lines.
Job 1 is of 350 lines, so
uses 4 pages. Last page
stores only 50 lines and 50
lines go waste.

So for all the Jobs loaded


on the RAM Internal
fragmentation can be
there if the Job page does
not fit into the page frame
completely. So Internal
Fragmentation if it exists
will always occur in the
last page.
In this figure only 7 page frames are remaining i.e., only 700 lines remaining. If a program of
720 lines arrives it will have to wait as in Paged memory allocation the ENTIRE program needs
to be stored.
7/25/2024 L J College of Computer Applications
How does the Memory Manager (part of OS) manage Paged Memory Allocation

The memory manager maintains three tables:


1. Job Table (JT)
2. Page Map Table (PMT)
3. Memory map Table (MMT)

1. JOB TABLE: It contains two entries, size of Job and address of its Page map table

When 3 Jobs on the system When 2 Jobs on the system


Size of Job address of PMT Size of Job address of PMT
370 4095 370 4095
400 7528
290 5002 290 5002

Every system has one Job Table

Every row of the Job table stores the details of the Jobs active on the same. So number of
entries in this table indicate number of active jobs on the system.

The size of the table increases and decreases as and when the Jobs come and leave the system

7/25/2024 L J College of Computer Applications


2. PAGE MAP TABLE : Every Job has its own Page Map Table, which contains the details of a
page like :
For example : If J1 has three pages its PMT will be like

Page Number Page Frame number


0 7
1 4
2 3

If J3 has five pages its PMT will be like

Page Number Page Frame number


0 5
1 2
2 1
3 6
4 8
As every Job has its own PMT, number of PMT in the system depends upon the number
of jobs in the system

The size of a PMT (number of rows)


7/25/2024 depends
L J College upon
of Computer the number of pages in the Job.
Applications
3. MEMORY MAP TABLE : Every system has one memory map table which stores the details of
the address of page frame and its status.

Page Frame number Status


0 Busy
1 Busy
2 Free
3 Busy
4 Free
…………………………
…………………………

Every table has one Memory Map table

Number of entries (number of rows) in a MMT depends upon the number of Page Frames in the
system

7/25/2024 L J College of Computer Applications


How is a line of program located?
Every Job is divided into pages
If J1 is a program of 350 lines then J1 is numbered from 0 to 349 (350 lines in all)
Page 0 contains the 1st hundred lines
Page 1 contains the 2nd hundred lines
Page 2 contains the 3rd hundred lines
Page 3 contains the last 50 lines, Resulting in internal fragmentation of 50 lines in last page

Displacement or Offset
This is a value which shows
how far a line is from the
beginning of a page.

Eg: if a page has 100 lines,


then the displacement of the
0th line of that page is 0,
displacement of the 1st line is
1, displacement of the 25th
line is 25 as the 25th line is 25
locations far from the
beginning of that page.

7/25/2024 L J College of Computer Applications


To find the address of any line of program , the line number is divided by the size of
the page. The quotient gives the page number and the remainder gives the offset or
displacement within that page.

Page Number
Size of the page Line number to be located
xxxx
xxxx
xxxx

Displacement
Role of the three tables:
When a Job enters the system the following happens in Paged memory allocation
 A Page Map table for that job is created
 An entry of the Job and address of its PMT is made in the Job Table
 The memory map table is searched to find the free page frames and the pages are
loaded onto the free page frames
 The entry of on which page frame a page is loaded is made in the PMT (Page Map Table)

7/25/2024 L J College of Computer Applications


To find the address of any line of program , the line number is divided by the size of the
page. The quotient gives the page number and the remainder gives the offset or
displacement within that page.
Consider this example :
The PMT looks like
Page Number Page Frame number
0 8
1 10
2 5
3 11

Suppose the line 280 has to be read from


the RAM.
Size of each page is 100 lines.
So:
2 Quotient / PAGE NUMBER
100 280
200
80
Size of Remainder / Offset
page
So after division we get that line 280 is at
Page number 2 (quotient) and within the page
7/25/2024 L J College of Computer Applications
the offset is 80 (remainder)
 So after division we get that line 280 is at Page number 2 (quotient) and within the page the
offset is 80 (remainder).

 The next step is that the Job table is searched to find the address of the PMT.

 Now the PMT is checked to see in which Page frame is page number 2
The PMT looks like
Page Number Page Frame number
0 8
1 10
2 5
3 11

 From the above PMT it is found that page number 2 is at Page Frame 5

 Now the address of the page frame is calculated as


address of page frame = Page frame number * size of page
= 5 *100
= 500
 The exact address now is obtained as
Exact address = address of page frame + displacement
= 500 +80
7/25/2024 = 580 this is theLexact
J Collegelocation
of Computerof RAM where we can get line number 280
Applications
Here we have an instruction
LOAD R1, 518
This means we have to read the instruction at
line 518 of the program . This program is now
in the RAM. So where is line 518 in the RAM
has to be calculated. Here the size of each
page and page frame is 512bytes.
Steps:
1 (page number)
 512 518
512
6 (offset)
here we get it is in Page 1 and within this
page the offset is 6 , which means 6 positions
further from the start of this page 1
 Next we search the PMT to get the Page frame number which is 3 .
 Now the address of the page frame is calculated as
address of page frame = Page frame number * size of page
= 3 * 512
= 1536
 The exact address now is obtained as
Exact address = address of page frame + displacement
= 1536 + 6
7/25/2024
= 1542 L J College of Computer Applications
this is the exact location of RAM where we can get line number 518
This process of calculating the exact address location of an instruction on the RAM is known as
ADDRESS RESOLUTION Or Resolving the Address.

Advantage of Paged Memory Allocation


 It allows the program to be stored at non- contiguous locations. This allows many programs
to be stored on the memory

Disadvantage of Paged Memory Allocation


 Lot of overhead is involved to carry out the entire process
 Internal Fragmentation is still there though only at the last page.
 If page size is too small , PMT’s will be large and programs will occupy many pages so more
overhead.
 If pages are large in size there is a possibility of lot of internal fragmentations

7/25/2024 L J College of Computer Applications


DEMAND PAGING

It is based on the two criteria:


 Entire program is not required to be there on the RAM for a job to start execution
This means if there are 8 page frames on the RAM
and if J1 has 4 pages
J3 has 8 pages
J4 has 12 pages
J5 has 10 pages and J2 has 5 pages
Total pages for all 5 jobs is 39 pages.
All these 5 Jobs can start their execution with one page of each in memory, thereby
occupying only 5 page frames and leaving 3 more empty

 The program is of course not at continuous locations.

7/25/2024 L J College of Computer Applications


J1 has 4 pages
J3 has 8 pages
J4 has 12 pages
J5 has 10 pages
J2 has 5 pages

Paged memory Allocation Demand Paging


PF 0 PF 0

PF 1 J2 Page 0 PF 1

J1 Page 0 PF 2 J1 Page 0 PF 2
J1 Page 2 PF 3 PF 3
PF 4 J5 Page 0 PF 4

J1 Page 1 PF 5 J3Page 0 PF 5

PF 6 J4 Page 0 PF 6

J1 Page 3 PF 7 PF 7

Here only J1 gets loaded All the Jobs get loaded with 1 page
J2, J3 has to wait, 3 page frames are still free
J4, J5 will not ever get loaded
7/25/2024 L J College of Computer Applications
How does Demand Paging work?
Initially no pages of any program are loaded.
When a program is to be executed only 1 page is loaded from secondary memory to RAM
Remaining pages are loaded as and when required. Secondary Memory
Random Access Memory (Hard Disk)
(RAM) Page Frames
PF 0

J3 Page 0 PF 1

PF 2 Pages
J1 Page 0 PF 3
J5 Page 0 PF 4
J1 P0 P1 P2 P3 P4 P5

PF 5
J2 P0 P1 P2

J4 Page 0 PF 6
Jobs J3 P0 P1 P2 P3
PF 7

PF 8
J4 P0 P1 P2 P3 P4

J2 Page 0 PF 9
J5 P0 P1

J6 Page 0 PF 10 J6 P0 P1 P2 P3 P4 P5 P6

PF 11
7/25/2024 L J College of Computer Applications
After some time the Jobs might demand for few more pages. When the demand
comes, more pages are loaded for that Job.

Random Access Memory Secondary Memory


(RAM) Page Frames (Hard Disk)

J6 Page 2 PF 0

J3 Page 0 PF 1

PF 2 Pages
J1 Page 0 PF 3
J5 Page 0 PF 4
J1 P0 P1 P2 P3 P4 P5

PF 5
J2 P0 P1 P2

J4 Page 0 PF 6
Jobs J3 P0 P1 P2 P3
J1 Page 4 PF 7

J3 Page 2 PF 8
J4 P0 P1 P2 P3 P4

J2 Page 0 PF 9
J5 P0 P1

J6 Page 0 PF 10 J6 P0 P1 P2 P3 P4 P5 P6

J4 Page 1 PF 11
7/25/2024 L J College of Computer Applications
Suppose the page and page frames are of size 100 lines each
J1 (size 360) has 4 pages,
J3 (size 840) has 8 pages,
J4 (size 1210) has 12 pages
J5 (size 1150) has 10 pages
J2(size 530) has 5 pages
Total number of pages for all jobs is 39 pages , which means 3900 lines of memory required.
Number of page frames are 8 i.e., 800 lines Demand Paging
Still the RAM can accommodate all Jobs. PF 0

The memory is small of 800 lines J2 Page 0 PF 1

And We feel that programs using 3900 lines are working J1 Page 0
PF 2
on the system.
PF 3

This happens because only a small portion of the J5 Page 0 PF 4

Program is loaded on memory and the user feels that J3Page 0 PF 5


their entire program is present in the memory when the
J4 Page 0 PF 6
program is being executed.
PF 7
This is the concept of VIRTUAL MEMORY

So Demand Paging gives rise to the concept of Virtual Memory.

7/25/2024 L J College of Computer Applications


HOW IS DEMAND PAGING ACHIEVED?

To do this, the Memory Manager part of OS maintains three tables:


1. JOB TABLE – It contains two entries, size of Job and address of its Page map table (same as
Paged memory Allocation)
Size of Job address of PMT
370 4095
400 7528
290 5002

2. PAGE MAP TABLE : Every Job has its own Page Map Table, which contains the details of a
page like : Page no., Page Frame No., Presence/Absence bit, Referenced bit, Modified bit

Page Number Page Frame number Presence status Referenced bit Modified bit
0 7 1 1 0
1 4 0 1 1
2 3 1 1 1

P/A bit indicates whether the page is still in memory


(1 is present , 0 is absent)
Referenced bit indicates whether the page has been referred
either for read or write operation (1 is yes , 0 is no)
Modified bit refers whether the contents of the page
7/25/2024 has
L J College been modified
of Computer since it has been loaded ( 1 is yes, 0 is no)
Applications
EVERY JOB HAS ITS OWN PAGE MAP TABLE
3. MEMORY MAP TABLE : Every system has one memory map table which stores the details
of the address of page frame and its status.
Page Frame number Status
0 Busy
1 Busy
2 Free
…………………………

Every system has one Memory Map table

7/25/2024 L J College of Computer Applications


Here total number of Page frames for user area
is 12.
All page frames are Busy.

Now suppose Job 4 requests for Page 3 which


is not in memory.

Here a PAGE FAULT occurs


A Page Fault occurs whenever a required Page
is not present in the memory (RAM)

What happens when a Page fault occurs?


When a Page Fault occurs , occurs the Page for
which this Page fault occurred (Page 3 in this
case) has to be bought in (swapped in) the
page frame of memory. As no Page frame is
empty , 1 page has to be removed (swapped
out) from the memory to vacate 1 page frame
and swap in the required page (page 3).
This entire Process of Page fault is handled by the Operating system. The Part of the OS which
handles a page fault is known as Page Fault Handler.

7/25/2024 L J College of Computer Applications


Before Page After Page
Fault Fault

Pages

Jobs

There are various criteria to decide which page has to be swapped out to free a page frame.
Suppose it is decided by OS to swap out Page 2 of Job 3 (present in Page frame 11)
The PMT of Job 4 is changed to show that Page 3 is loaded on RAM at page frame 11
The7/25/2024
PMT of Job 3 also changes to show that Page 2 in not present. And the Page frame 11 of
L J College of Computer Applications
RAM shows the that Page 3 of Job 4 has been loaded.
Whenever a page fault occurs, the page fault handler does the following :
 The MMT is checked to see which page frame is free to swap in the required page.
 If a page free is free the required page is swapped in. If page frame is not free it is checked to
see which page of a job can be removed to free a page frame, and then that page is swapped
out.
 The PMT of the table whose page has been swapped in : PMT of Job 4 is changed to show
that Page 3 is loaded on RAM at page frame 11
 The PMT of the table whose page has been swapped out: PMT of Job 3 changes to show
that Page 2 in not present.
 The MMT is also changed to update whether the page frames are busy or free.

The Page fault handler does this job of swapping : Bringing in a page (Swap In) and Removing a
page (Swap Out) from the RAM.
If Page faults occur very frequently
resulting in very frequent swap in and
swap out of pages, it is known as
THRASHING.

Thrashing reduces the efficiency of the


system.

7/25/2024 L J College of Computer Applications


Page Replacement Policies
A page is required to be selected to swap out if there is a page fault and no page frames are
free to accommodate the required page

The two policies for page replacement are:


1. FIFO (First In First Out)
2. LRU (Least Recently Used)

First In First Out


When a page fault occurs and one page has to be removed from the RAM to free a Page frame:
The Page which came in first…. Goes out first
This means that the page which has been in the memory for the longest time is swapped out.

Let us see how it works and what is its efficiency

7/25/2024 L J College of Computer Applications


Eg:
Consider a Job with 4 pages (Page A, Page B, Page C, Page D) FIFO method
There are only 2 page frames on the RAM
B is removed as
The requests or demand for pages come in this order: it is older than A is removed as
A, B, A, C, A, B, D, B, A, C, D D it is older than
Page A is C is removed as C
removed as it is it is older than
Initially both page frames are empty: oldest A

Page
Frame
1

A A A C C B B B A A D

Page
Frame
2

EMPTY
B B B A A D D D C C

Interrupt 1st 2nd No 3rd 4Page


th
B is
5 th 6Page
th
A is
No 7th 8th 9th
removed as it isApplications
removed as it is D is removed as
7/25/2024 L J College of Computer it is older than
older than C older than B
A
Total number of page requests = 11 ( A, B, A, C, A, B, D, B, A, C, D) FIFO
Total number of interrupts = 9
Failure Rate = Total Interrupts * 100 = 9 * 100 = 82%
(in percent) Total number of requests 11

Success Rate = Total Success (no interrupts) * 100 = 2 * 100 = 18%


(in percent) Total number of requests 11

Page
Frame
1

A A A C C B B B A A D

Page
Frame
2

EMPTY
B B B A A D D D C C

Interrupt 1st 2nd No 3rd 4th 5th 6th No 7th 8th 9th
7/25/2024 L J College of Computer Applications
LEAST RECENTLY USED: Job with 4 pages and 2 page frames
Total number of page requests = 11 ( A, B, A, C, A, B, D, B, A, C, D)
Total number of interrupts = 9
Failure Rate = Total Interrupts 100 = 8 100 = 73%
(in percent) Total number of requests 11

Success Rate = Total Success (no interrupts) 100 = 3 100 = 27%


(in percent) Total number of requests 11
Page
Frame
1

A A A A A A D D A A D

Page
Frame
2

empty B B C C B B B B C C
Interrupt 1st 2nd NO 3rd NO 4th 5th NO 6th 7th 8th
7/25/2024 L J College of Computer Applications
Consider a Job with 4 pages (Page A, Page B, Page C, Page D) B is removed as it is
There are only 3 page frames on the RAM older than C
The requests or demand for pages come in this order:
A, B, A, C, A, B, D, B, A, C, D A is removed as it is
oldest page

FIFO method

A A A A A A D D D D D
empty

PF 2
B B B B B B B A A A
empty

PF 3 PF 3 PF 3
C C C C C C C C
empty empty empty

Int 1st 2nd NO 3rd NO NO 4th NO 5th NO NO

Failure Ratio= 5 success Ratio = 6


7/25/2024
100 = 45 %L J College of Computer Applications 100 = 55 %
11 11
Consider a Job with 4 pages (Page A, Page B, Page C, Page D)
There are only 3 page frames on the RAM
The requests or demand for pages come in this order:
A, B, A, C, A, B, D, B, A, C, D B is removed as it is
not used recently
used
LRU method

A A A A A A A A A A A
empty

PF 2
B B B B B B B B B D
empty

PF 3 PF 3 PF 3
C C C D D D C C
empty empty empty

Int 1st 2nd NO 3rd NO NO 4th NO NO 5th 6th


Failure Ratio = 6/11 = 54% C is removed as it is D is removed as it is
7/25/2024 L J College of Computernot
Applications
used recently not used recently
Success Ratio = 5/11 = 46% used used
Considerfour page frames. The program requests are in the following order:
D, C, B, A, D, C, E, D, C, B, A, E
LRU Algorithm D swapped out

D D D D D D D D D D D E

PF 2
C C C C C C C C C C C
empty

PF 3 PF 3
B B B B E E E E A A
empty empty

PF 4 PF 4 PF 4
A A A A A A B B B
empty empty empty

Int: 1st 2nd 3rd 4th No No 5th No No 6th 7th 8th


Failure Ratio= 8/12 = 66%
7/25/2024 L J College of Computer Applications A swapped out
B swapped out
Success Ratio = 34 % E swapped out
Mechanics of Paging
Page Map table :

The Status bit indicates whether the Page is there on Ram or not. A 1 in this bit
indicates that all the pages are present in the page frames of the memory

The Referenced bit indicates whether the page has been called or referred for either a
read or write operation. The LRU algorithm uses this bit to choose a page to swap out
at time of a page fault. Usually a page where referenced bit is 0 is swapped out as it
has not been called or referred.

The Modified bit indicates whether the contents of the page have been modified
after they were loaded from hard disk to RAM.
7/25/2024 L J College of Computer Applications
If modified bit is 0 , the page need not be written back to the hard disk at the time of swap out
as the copy of it is already there on the hard disk and it is same as the one present on RAM (as it
was not modified). ex

If modified bit is 1, it means that the page has been modified on the RAM after it was swapped
in from the secondary memory. So the contents of the RAM and secondary memory are not
same. In this case if this page is chosen to be swapped from the RAM then it has to be rewritten
on the Secondary memory to update the changes. This is a time taking process.

Meaning of Bits in the PMT

For all the pages present in the page frames of RAM the status bit will be 1 showing their
presence on the RAM

7/25/2024 L J College of Computer Applications


The other two bits can be in the combination as:

In FIFO policy the modified bit and status bits are checked

The LRU policy checks all the three bits. It first checks the referenced bit as it swaps the page
which is least recently used and selects pages with Referenced bit 0
The first choice will be Case 1 : A page on RAM which is not modified and not referred.
Second choice will be Case 3 : A page not referenced but modified. (Not logically possible but
(as referenced bit is 0) this case exists)
Third choice will be Case 2 : As this page is referred but not modified , less time will be taken to
swap it out.
Last choice will be Case 4 : As it has been referred and modify, so it is the most active page.

7/25/2024 L J College of Computer Applications


Second choice will be Case 3 : A page not referenced but modified. (Not logically possible but
(as referenced bit is 0) this case exists)

A page not referenced but modified. How is this possible?


All the pages swapped onto the RAM have referenced bit as 1 whenever they are referred.
Assume that all the pages were referred at least once. Then all the reference bits will be 1.

Page no. Status Bit Referenced bit Modified Bit Page Frame No.

0 1 1 1 5
1 1 1 0 3
2 1 1 0 7
3 1 1 0 2

Under this situation the LRU policy will fail as it first picks up those pages where referenced bit is
0.

7/25/2024 L J College of Computer Applications


To avoid this situation the OS resets the referenced bit to 0 at regular intervals.
Now all the pages will have referenced bit as 0.

Page no. Status Bit Referenced bit Modified Bit Page Frame No.

0 1 0 1 5
1 1 0 0 3
2 1 0 0 7
3 1 0 0 2

This is the situation where


If now page 0, 2, 3 are referred ..they will have referenced bit as 1 and page 1 will have
reference bit of 0.
If a page fault occurs at this point of time the LRU policy will choose Page 1 as its reference bit
is 0 which indicates that it has not been referred.

Page no. Status Bit Referenced bit Modified Bit Page Frame No.

0 1 1 1 5
1 1 0 0 3
2 1 1 0 7
3 1 1 0 2
7/25/2024 L J College of Computer Applications
WORKING SET

A Working set is the set of pages in the RAM which can be executed without any page faults
occurring.

Initially when a program starts execution there are certain number of page faults to bring in
the required pages. After some time the execution stabilizes and the processing goes on
smoothly without any page fault. At this time the instructions present in one or multiple
pages form a working set.
If a set of instructions within this working set are referred to again and again, the reference
becomes localized to a particular area . This is known as LOCALITY OF REFERENCE.

PF 1

PF 2 P2

PF 3
Working set
PF 4 P3
.
.
.
.
. P1
A loop structure here is repeatedly executed many times …
.
. 7/25/2024 this is locality of reference
L J College of Computer Applications
Segmented Memory Allocation
In segmented Memory Allocation depending on the structure of the Job each Job is
divided into many parts called Segments. All these segments are of different size
For example a procedure or a function of a program can be one segment.
When these segments are loaded on the RAM, at that point of time depending on
the size of the segment, a portion of the RAM which is of the same size of the
segment is dynamically allocated to the segment. (As was the case in Dynamic
memory Allocation).
Not all the segments of a program are required to be stored on the RAM and neither
at continuous locations
Job 1 (secondary memory) RAM Other programs
0

Segment 0 Segment 0
(main)

399
0 Segment 1
Function 1 Other programs
99
0
Segment 1
Segment 2
Function 2
7/25/2024 L J College of Computer Applications Other programs
199
Every Job has a segment map table which is same as the page map table in demand paging with
additional fields like size of segment and access rights.
The entries are made in the Segment Map table as:

7/25/2024 L J College of Computer Applications


The memory manager uses three Tables:
1. Job Table which lists every Job in process (one Job Table for the whole system)
2. A segment Map Table which contains the details of each segment of Job
every Job has its own Segment Map table
3. A memory Map table which keeps track of memory allocation (One for the whole system)

To locate a particular line the address should contain the segment number and displacement
Address  (Segment number, displacement/offset)
For eg : (1, 35)
Now the Segment Map table will be searched
to find the beginning address of Segment 1 on
RAM

We get  7000
Actual address = beginning address + offset
= 7000+35
= 7035

7/25/2024 L J College of Computer Applications


Segmented / Demand Paged Memory Allocation
This scheme is a combination of segmentation and Demand Paging
Here the Jobs are divided as:

Job Segments Pages

In this method four tables are used


1. Job table : This table gives the list of all the jobs in process
There is only 1 Job table inn the system
2. Segment Map Table : It contains the details about each segment
There is one segment map table for each job
3. Page map Table: It contains details about every Page.
There is 1 Page map table for each segment
4. Memory Map table: Contains the detail of the page frames of RAM.
There is one Memory Map table for the whole system.

7/25/2024 L J College of Computer Applications


To access a location in the memory the address used is composed of three entries
Segment number --- Page number within that segment--- displacement within that page

Eg: ( 0, 1, 40)
First the address to the SMT is located for the Job which is in process, then for segment number
0 the address of the PMT is identified, after the PMT is located the page frame number for page
number 1 is located, which is 4 . Then in page frame 4, from the starting address the instruction
7/25/2024 L J College of Computer Applications
40 locations beyond the starting of page frame 4 is read.

You might also like