IAE Academy

GATE CS

Memory management

Allocation, paging and segmentation

Memory management decides how the operating system places processes in main memory and turns the addresses a program generates into physical addresses. Typical problems are computations: splitting an address into page number and offset, sizing page tables, counting page-table levels, finding effective access time with a TLB, and translating a segment address.

10 previous-year questions · 10 solved · 5 practice questions

Addresses, binding and the MMU

A logical (virtual) address is the address a program generates. A physical address is the address seen by the memory unit. The set of all logical addresses is the logical address space, and the set of all physical addresses is the physical address space.

Binding of instructions and data to addresses can happen at compile time (absolute code, the load address is fixed), at load time (relocatable code, addresses fixed when the program is loaded) or at execution time (addresses can change while the program runs). Only execution-time binding allows a process to be moved in memory after it has started, and it needs hardware support.

The memory-management unit (MMU) is the hardware that maps a logical address to a physical address on every reference. In the simplest scheme a relocation register (base) is added to every logical address, and a limit register is compared against it first: if the logical address is not less than the limit, the hardware traps to the operating system.

EXAM TIP

Compile-time and load-time binding give identical logical and physical addresses. With execution-time binding the two differ: the process works only with logical addresses, and the MMU produces the physical address on each reference.

Contiguous allocation and fragmentation

In contiguous allocation each process occupies one unbroken block of memory. With fixed partitions memory is divided into partitions of preset sizes and each holds at most one process; the unused part of a partition is internal fragmentation. With variable partitions a process gets exactly the memory it asks for, so there is no internal fragmentation, but as processes finish the free memory breaks into scattered holes. Total free memory may be enough for a request while no single hole is: this is external fragmentation.

Strategy

Rule for choosing a hole

Notes

First fit

First hole (in address order) that is large enough

Fast; leftovers pile up near the start of memory

Next fit

Like first fit but the search resumes where the last one ended

Variant of first fit

Best fit

Smallest hole that is large enough

Needs a search of all holes (unless sorted); leaves tiny useless holes

Worst fit

Largest hole

Needs a search of all holes; leaves the biggest leftover

Compaction shifts the allocated blocks together so that all free memory becomes one hole. It removes external fragmentation but is possible only with execution-time binding, because processes change address. Paging avoids external fragmentation altogether.

Trace for variable partitions. Free holes in address order are 150, 450, 250, 350 and 600 KB. Requests arrive in the order 200, 400, 120 and 480 KB, and the holes left over after each allocation stay in place. For best fit and worst fit, ties are broken by the lower address (none occur in this trace). The table shows the hole chosen for each request.

Request (KB)

First fit

Best fit

Worst fit

200

450 (leaves 250)

250 (leaves 50)

600 (leaves 400)

400

600 (leaves 200)

450 (leaves 50)

450 (leaves 50)

120

150 (leaves 30)

150 (leaves 30)

400 (leaves 280)

480

No hole large enough (largest is 350)

600 (leaves 120)

No hole large enough (largest is 350)

EXAM TIP

No strategy is best for every request sequence. In the trace above best fit places all four requests while first fit and worst fit fail on the last, but a different sequence can reverse the order, so always simulate.

Paging

Paging divides the logical address space into fixed-size pages and physical memory into frames of the same size. A process may be placed in any free frames, so there is no external fragmentation. The only waste is internal fragmentation in the last page of a process (under the usual assumption that process sizes are random, about half a page per process on average). Page size is a power of two so that the address splits cleanly into bits.

A logical address of bits with page size bytes splits into a page number (the high bits) and an offset (the low bits). The page number indexes the page table, whose entry holds the frame number (plus valid, protection and other bits). The physical address is the frame number followed by the same offset. Convention: memory is byte-addressable, , , unless stated.

ADDRESS SPLIT

n is the logical address width in bits; the page offset has d bits and the page number n-d bits.

FRAMES

Physical address = frame number bits followed by d offset bits.

SINGLE-LEVEL PAGE TABLE SIZE

e is the size of one page-table entry in bytes; the number of entries equals the number of pages in the logical address space.

MINIMUM PAGE-TABLE ENTRY WIDTH

Extra bits are those the question lists (valid, dirty, protection and so on). Round up to whole bytes; round to a power of two only if the question says so.

PAGE SIZE MINIMISING OVERHEAD

s is the average process size and e the entry size in bytes; overhead = se/p (page table) + p/2 (internal fragmentation); valid under these two assumptions only.

  1. Step 1: Find the offset width d from the page size.

  2. Step 2: Page number = address divided by page size (integer division); offset = remainder.

  3. Step 3: Look up the frame number for that page in the page table.

  4. Step 4: Physical address = frame number times page size plus offset.

Example of the split: with 4 KB pages the logical address has offset (the low 12 bits, three hex digits) and page number . If the page table maps page to frame , the physical address is the frame number followed by the offset: .

The page table is kept in main memory. The page-table base register (PTBR) points to it and, where used, a page-table length register (PTLR) holds the number of entries; a page number not less than the PTLR value traps. A context switch only reloads the PTBR. Paging also makes sharing easy: reentrant (read-only) code can be shared by making entries in several processes' page tables point to the same frames, so one copy in memory serves them all.

WORKED EXAMPLE

A system has a 32-bit logical address, 4 KB pages and 1 GB of byte-addressable physical memory. Each page-table entry holds the frame number plus 4 extra bits (valid, dirty and two protection bits), rounded up to a whole number of bytes. Find (a) the number of pages, (b) the number of frame-number bits, (c) the size of a single-level page table.

Solution
  1. 4 KB = 2^12, so d = 12 and the page number has 32 - 12 = 20 bits.

  2. (a) Number of pages = 2^20.

  3. (b) Frames = 2^30 / 2^12 = 2^18, so the frame number has 18 bits.

  4. Entry width = 18 + 4 = 22 bits, which rounds up to 3 bytes.

  5. (c) Size = 2^20 x 3 B = 3 MB.

Answer: (a) 2^20 pages; (b) 18 bits; (c) 3 MB.

EXAM TIP

When a question states the page-table entry size, use it as given, even if it is larger than the frame number needs. Compute the entry width from the frame-number bits only when the size is not given.

Large address spaces: multi-level, inverted and hashed tables

A single-level table for a large address space can itself be huge. Multi-level (hierarchical) paging pages the page table: the page number is split into several parts, the first indexes an outer table, and so on, and only the tables that are actually in use need to exist. The standard GATE assumption is that every table at every level fits exactly in one page.

NUMBER OF LEVELS

k is the minimum number of levels when each table occupies one page; n-d is the number of page-number bits. If n-d is not a multiple of the bits per level, the convention used here gives the leftover bits to the outermost table, which then occupies less than a page.

Inverted page table: one entry per physical frame rather than per page, holding the process id and page number stored in that frame. The table size depends on physical memory, not on the virtual address space, and so does not grow with the number of processes. Lookup by page number needs a search, so a hash is usually used to find the entry. A drawback is that sharing a frame between processes is awkward, because each frame has only one entry and so one (process, page) pair.

INVERTED PAGE TABLE SIZE

One entry per frame; e is the entry size in bytes (an entry holds the process id and page number).

Hashed page table: the page number is hashed to select a bucket holding a chain of entries (page number, frame number, next pointer); the chain is searched for a match. It is used for address spaces larger than 32 bits.

Scheme

Table size depends on

Memory references per translation (no TLB)

Single-level

Size of virtual address space

2 (table, then data)

k-level

Only the parts of the space in use

k + 1

Inverted

Physical memory

At least 1 to find the entry (more on hash collisions), then 1 for data

Hashed

Pages actually in use

1 per chain entry examined, then 1 for data

WORKED EXAMPLE

A system has a 40-bit logical address, 4 KB pages and 8-byte page-table entries. Every page table must fit in exactly one page. (a) What is the size of a single-level page table? (b) How many levels of paging are needed?

Solution
  1. Offset bits: 4 KB = 2^12, so d = 12 and the page number has 40 - 12 = 28 bits.

  2. (a) Entries = 2^28; size = 2^28 x 8 B = 2^31 B = 2 GB.

  3. (b) Entries per page = 4096 / 8 = 512 = 2^9, so each level resolves 9 bits.

  4. Three levels resolve 27 bits, which is less than 28, so three levels are not enough; four levels resolve up to 36 bits.

  5. Levels = ceil(28 / 9) = 4. (The top level uses only 1 bit if the others take 9 each.)

Answer: (a) 2 GB; (b) 4 levels.

TLB and effective memory-access time

With paging, each logical reference costs extra memory accesses to read the page-table entries. The translation look-aside buffer (TLB) is a small, fast associative cache of recent page-number to frame-number mappings. On a TLB hit the frame is known immediately and only the data access to memory is needed. On a miss the page table is walked in memory and the entry is then loaded into the TLB. The hit ratio is the fraction of references that hit.

EFFECTIVE ACCESS TIME WITH A TLB

t = TLB lookup time, m = main-memory access time (same unit as t), k = number of page-table levels walked on a miss. The TLB is searched first on every reference, and a miss then costs k extra memory accesses. Assumes no page faults and page-table entries not cached elsewhere. If a question says the TLB lookup overlaps or is ignored, set t = 0.

ACCESS TIME WITHOUT A TLB

k page-table accesses plus one data access.

WORKED EXAMPLE

A system uses three-level paging. A TLB lookup takes 20 ns and a main-memory access takes 100 ns. The TLB lookup is always performed first and the hit ratio is 90%. Find the effective access time in ns.

Solution
  1. Hit: 20 + 100 = 120 ns.

  2. Miss: 20 ns for the TLB, 3 page-table accesses and 1 data access = 20 + 4 x 100 = 420 ns.

  3. EMAT = 0.9 x 120 + 0.1 x 420 = 108 + 42 = 150 ns.

Answer: 150 ns

The TLB holds mappings of the running process. On a context switch it must be flushed, unless each entry carries an address-space identifier (ASID) naming its process. With ASIDs, entries of several processes can stay in the TLB together.

EXAM TIP

Read the wording for the cost of a miss: some questions add the TLB time on a miss, some say the TLB and page-table lookups happen in parallel. Use exactly what the question states, and if memory time is given in different units from TLB time, convert first.

Segmentation and segmentation with paging

Segmentation follows the programmer's view: a program is a set of variable-length segments (code, stack, data and so on). A logical address is a pair (segment number , offset ). The segment table has one entry per segment with a base (starting physical address) and a limit (segment length). The segment-table base register (STBR) locates the table in memory and the segment-table length register (STLR) holds the number of segments.

  1. Step 0: If s is not less than STLR, trap (illegal segment).

  2. Step 1: Use s to find the entry (base, limit) in the segment table.

  3. Step 2: If o is greater than or equal to the limit, trap (addressing error).

  4. Step 3: Otherwise the physical address is base + o.

SEGMENT TRANSLATION

Offsets run from 0 to limit - 1; an offset equal to the limit is out of range.

Segmentation has no internal fragmentation but suffers external fragmentation, since segments are variable-sized and are placed in contiguous memory. Segmentation with paging pages each segment: the segment number selects a segment-table entry that points to that segment's page table, and the offset is then split into page number and page offset. This removes external fragmentation while keeping segment-level protection and sharing.

Property

Paging

Segmentation

Unit size

Fixed (page)

Variable (segment)

Visible to the programmer

No

Yes

Internal fragmentation

Yes (last page)

No

External fragmentation

No

Yes

Address

Page number + offset

Segment number + offset

Table entry

Frame number

Base and limit

EXAM TIP

In segmentation the offset is checked against the limit; in paging the offset can never be out of range because it always fits in the page. A page-number check against the table length is a separate check.

PREVIOUS-YEAR QUESTIONS

10 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 2 · GATE CSE 2025 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2024 Set 1 · GATE CSE 2024 Set 2 · GATE CSE 2023 · GATE CSE 2022 · GATE CSE 2020

GATE 2026 CS2 Q.54

GATE 2026 · CS2Q.54 · 2 marks · NAT

A system has a Translation Lookaside Buffer (TLB) that has a reach of 1 MB. TLB reach is defined as the total amount of physical memory that can be accessed through the TLB entries. The paging system uses pages of size 4 KB. The virtual address space is 64 GB and physical address space is 1 GB. If each TLB entry stores a 4-bit process id, page number, frame number, and a 2-bit control field, then the size of the TLB (in bytes) is ___________. (answer in integer)

Note: 1K=2^10, 1M=2^20, 1G=2^30

Answer and solution
See the question in the paper
GATE 2026 CS2 Q.54: answer and solution

GATE 2026 CS2 Q.55

GATE 2026 · CS2Q.55 · 2 marks · NAT

Consider contiguous allocation of physical memory to processes using variable partitioning scheme. Suppose there are 8 holes in the memory of sizes 20 KB, 4 KB, 25 KB, 18 KB, 7 KB, 9 KB, 15 KB, and 12 KB. Assume that no two holes are adjacent. Two processes P1 of size 16 KB and P2 of size 9 KB arrive in that order, and they are allocated memory using the best-fit technique. After allocating space to P1 and P2, the number of holes of size less than 8 KB is ____________. (answer in integer)

Note: 1K=2^10

Answer and solution
See the question in the paper
GATE 2026 CS2 Q.55: answer and solution

GATE 2025 CS2 Q.58

GATE 2025 · CS2Q.58 · 2 marks · NAT

A computer system supports a logical address space of bytes. It uses two-level hierarchical paging with a page size of 4096 bytes. A logical address is divided into a -bit index to the outer page table, an offset within the page of the inner page table, and an offset within the desired page. Each entry of the inner page table uses eight bytes. All the pages in the system have the same size.

The value of is ___________ . (Answer in integer)

Answer and solution
See the question in the paper
GATE 2025 CS2 Q.58: answer and solution

GATE 2024 CS1 Q.62

GATE 2024 · CS1Q.62 · 2 marks · NAT

Consider a memory management system that uses a page size of 2 KB. Assume that both the physical and virtual addresses start from 0. Assume that the pages 0, 1, 2, and 3 are stored in the page frames 1, 3, 2, and 0, respectively. The physical address (in decimal format) corresponding to the virtual address 2500 (in decimal format) is __________

Answer and solution
See the question in the paper
GATE 2024 CS1 Q.62: answer and solution

GATE 2024 CS2 Q.24

GATE 2024 · CS2Q.24 · 1 mark · MSQ

Which of the following tasks is/are the responsibility/responsibilities of the memory management unit (MMU) in a system with paging-based memory management?

  1. (A)

    Allocate a new page table for a newly created process

  2. (B)

    Translate a virtual address to a physical address using the page table

  3. (C)

    Raise a trap when a virtual address is not found in the page table

  4. (D)

    Raise a trap when a process tries to write to a page marked with read-only permission in the page table

Answer and solution
See the question in the paper
GATE 2024 CS2 Q.24: answer and solution

GATE 2024 CS2 Q.64

GATE 2024 · CS2Q.64 · 2 marks · NAT

Consider a 32-bit system with 4 KB page size and page table entries of size 4 bytes each. Assume 1 KB = 2^10 bytes. The OS uses a 2-level page table for memory management, with the page table containing an outer page directory and an inner page table. The OS allocates a page for the outer page directory upon process creation. The OS uses demand paging when allocating memory for the inner page table, i.e., a page of the inner page table is allocated only if it contains at least one valid page table entry.

An active process in this system accesses 2000 unique pages during its execution, and none of the pages are swapped out to disk. After it completes the page accesses, let X denote the minimum and Y denote the maximum number of pages across the two levels of the page table of the process.

The value of X+Y is ________

Answer and solution
See the question in the paper
GATE 2024 CS2 Q.64: answer and solution

GATE 2023 CS Q.58

GATE 2023 · CSQ.58 · 2 marks · NAT

Consider a computer system with 57-bit virtual addressing using multi-level tree-structured page tables with L levels for virtual to physical address translation. The page size is 4 KB (1 KB = 1024 B) and a page table entry at any of the levels occupies 8 bytes.

The value of L is ______.

Answer and solution
See the question in the paper
GATE 2023 CS Q.58: answer and solution

GATE 2022 CS Q.38

GATE 2022 · CSQ.38 · 2 marks · MCQ

Which one of the following statements is FALSE?

  1. (A)

    The TLB performs an associative search in parallel on all its valid entries using page number of incoming virtual address.

  2. (B)

    If the virtual address of a word given by CPU has a TLB hit, but the subsequent search for the word results in a cache miss, then the word will always be present in the main memory.

  3. (C)

    The memory access time using a given inverted page table is always same for all incoming virtual addresses.

  4. (D)

    In a system that uses hashed page tables, if two distinct virtual addresses V1 and V2 map to the same value while hashing, then the memory access time of these addresses will not be the same.

Answer and solution
See the question in the paper
GATE 2022 CS Q.38: answer and solution

GATE 2020 CS Q.11

GATE 2020 · CSQ.11 · 1 mark · MCQ

Consider allocation of memory to a new process. Assume that none of the existing holes in the memory will exactly fit the process's memory requirement. Hence, a new hole of smaller size will be created if allocation is made in any of the existing holes. Which one of the following statements is TRUE?

  1. (A)

    The hole created by first fit is always larger than the hole created by next fit.

  2. (B)

    The hole created by worst fit is always larger than the hole created by first fit.

  3. (C)

    The hole created by best fit is never larger than the hole created by first fit.

  4. (D)

    The hole created by next fit is never larger than the hole created by best fit.

Answer and solution
See the question in the paper
GATE 2020 CS Q.11: answer and solution

OTHER EXAMS

ISRO: 3 questions

From ISRO's Scientist/Engineer written test: ISRO CS 2023. They are not counted in the GATE numbers on this page.

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · NAT

A system has a 46-bit logical address, 8 KB pages and 8-byte page-table entries. What is the size, in GB, of a single-level page table covering the whole logical address space? (K = 2^10, M = 2^20, G = 2^30.)

Answer and solution

Answer:

  1. 8 KB = 2^13, so the offset has 13 bits and the page number has 46 - 13 = 33 bits.

  2. Number of entries = 2^33.

  3. Size = 2^33 x 8 B = 2^36 B.

  4. 2^36 / 2^30 = 2^6 = 64 GB.

PRACTICE 22 marks · MCQ

A system has a 48-bit logical address, 16 KB pages and 8-byte page-table entries. Each page table at every level occupies exactly one page. What is the minimum number of levels of paging needed?

  1. (A)

    2

  2. (B)

    3

  3. (C)

    4

  4. (D)

    5

Answer and solution

Answer: (C)

  1. 16 KB = 2^14, so the offset has 14 bits and the page number has 48 - 14 = 34 bits.

  2. Entries per table = 2^14 / 2^3 = 2^11, so each level resolves 11 bits.

  3. Three levels resolve 33 bits, which is less than 34; four levels resolve 44 bits.

  4. Levels = ceil(34 / 11) = 4.

PRACTICE 32 marks · NAT

A system uses three-level paging. The TLB lookup takes 20 ns, a main-memory access takes 80 ns, and the TLB is always searched first (on a miss the page-table walk follows). The TLB hit ratio is 75%. What is the effective memory-access time in ns?

Answer and solution

Answer:

  1. Hit: 20 + 80 = 100 ns.

  2. Miss: 20 + (3 + 1) x 80 = 340 ns.

  3. EMAT = 0.75 x 100 + 0.25 x 340 = 75 + 85 = 160 ns.

PRACTICE 41 mark · MCQ

A process has the segment table below (base, limit in bytes): segment 0 has base 1000 and limit 400; segment 1 has base 3000 and limit 200; segment 2 has base 5000 and limit 600. Offsets are counted from 0. Which logical address (segment, offset) maps successfully to physical address 5550?

  1. (A)

    (0, 450)

  2. (B)

    (1, 250)

  3. (C)

    (2, 550)

  4. (D)

    (2, 650)

Answer and solution

Answer: (C)

  1. (0, 450): offset 450 is not below limit 400, so it traps.

  2. (1, 250): offset 250 is not below limit 200, so it traps.

  3. (2, 550): 550 < 600, so physical address = 5000 + 550 = 5550.

  4. (2, 650): 650 is not below limit 600, so it traps.

PRACTICE 51 mark · MSQ

Which of the following statements are TRUE?

  1. (A)

    Paging does not cause external fragmentation.

  2. (B)

    In paging, internal fragmentation can occur only in the last page of a process.

  3. (C)

    Segmentation does not cause external fragmentation because every segment has a fixed size.

  4. (D)

    The size of an inverted page table depends on the size of physical memory and not on the size of the virtual address space.

Answer and solution

Answer: (A), (B), (D)

  1. A is true: any free frame can hold any page.

  2. B is true: every page except the last is completely filled by the process.

  3. C is false: segments are variable-sized and contiguous, so external fragmentation occurs.

  4. D is true: the inverted table has one entry per physical frame.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

Memory management

Preparing in Kolkata? IAE Academy's course for GATE CSE.