linux memory management – virtual memory and demand paging

8
12/5/13 Linux Memory Management – Virtual Memory and Demand Paging www.thegeekstuff.com/2012/02/linux-memory-management/ 1/8 32 Like 11 Home Free eBook Start Here Contact About Linux Memory Management – Virtual Memory and Demand Paging by Himanshu Arora on February 17, 2012 Tweet Memory management is one of the most complex activity done by Linux kernel. It has various concepts/issues associated with it. This article is part of our on-going UNIX kernel overview series. In the previous article of the kernel series, we discussed about the UNIX process overview , and Reentrant Kernels . In this article we will try to touch base on virtual memory and demand paging as these are some of the important concepts related to memory management. Virtual Memory The concept of virtual memory is one of the very powerful aspects of memory management. Since the initial era of computers the need of memory more than the existing physical memory has been felt. Over the years, many solutions were used to overcome this issue and the most successful of them has been the concept of virtual memory. Virtual memory makes your system appear as if it has more memory than it actually has. This may sound interesting and may prompt one to as how is this possible. So, lets understand the concept. To start, we must first understand that virtual memory is a layer of memory addresses that map to physical addresses. In virtual memory model, when a processor executes a program instruction, it reads the instruction from virtual memory and executes it. But before executing the instruction, it first converts the virtual memory address into physical address. This conversion is done based on the mapping of virtual to physical addresses that is done based on the mapping information contained in the page tables (that are maintained by OS).

Upload: ankit-sanghvi

Post on 20-Oct-2015

25 views

Category:

Documents


6 download

DESCRIPTION

description about memory management in linux

TRANSCRIPT

Page 1: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 1/8

32 Like 11

Home

Free eBook

Start Here

Contact

About

Linux Memory Management – Virtual Memoryand Demand Paging

by Himanshu Arora on February 17, 2012

Tweet

Memory management is one of the most complex activity done by Linux kernel. It has various concepts/issuesassociated with it.

This article is part of our on-going UNIX kernel overview series.

In the previous article of the kernel series, we discussed about the UNIX process overview, and Reentrant

Kernels.

In this article we will try to touch base on virtual memory and demand paging as these are some of the important

concepts related to memory management.

Virtual Memory

The concept of virtual memory is one of the very powerful aspects of memory management. Since the initial era

of computers the need of memory more than the existing physical memory has been felt. Over the years, many

solutions were used to overcome this issue and the most successful of them has been the concept of virtualmemory.

Virtual memory makes your system appear as if it has more memory than it actually has. This may sound

interesting and may prompt one to as how is this possible. So, lets understand the concept.

To start, we must first understand that virtual memory is a layer of memory addresses that map to physical

addresses.

In virtual memory model, when a processor executes a program instruction, it reads the instruction from

virtual memory and executes it.

But before executing the instruction, it first converts the virtual memory address into physical address.

This conversion is done based on the mapping of virtual to physical addresses that is done based on the

mapping information contained in the page tables (that are maintained by OS).

Page 2: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 2/8

The virtual and physical memory is divided into fixed length chunks known as pages. In this paged model, a

virtual address can be divided into two parts :

An offset (Lowest 12 bits)

A virtual page frame number (rest of the bits)

When ever the processor encounters a virtual address, it extracts the virtual page frame number out of it. Then it

translates this virtual page frame number into a physical page frame number and the offset parts helps it to go to

the exact address in the physical page. This translation of addresses is done through the page tables.

Theoretically we can consider a page table to contain the following information :

A flag that describes whether the entry is valid or not

The physical page frame number as described by this entry

Access information regarding the page (like read-only, read-write etc)

A page table is accessed through virtual page frame number using it as offset for entries in the page table. For

example, a virtual page frame number of ’2′ points to the entry ’1′ in the page table (the entry numbers begin with’0′ ).

In the image below, VPFN stands for Virtual page frame number, and PFN indicates the physical page frame

number.

It may happen that a processor goes to a processes page table entry with a virtual page frame number and findsthe entry as invalid. In this case it is the processor’s responsibility to pass the control to kernel and ask it to fixthe problem. Different processors pass the control in different ways but this phenomenon is known as a ‘page

fault’. But if the entry was valid then processor takes the physical page frame number, multiplies with the size ofthe page to get the base address of the physical page and then adds the offset to get to the exact physical

Page 3: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 3/8

32 Tweet Like 11

address.

So now we understand that through the concept of virtual memory, each process thinks that it has all range of

virtual address at its disposal and hence this concepts make the system appear as if it has more physical memorythan actually available.

Demand Paging

In the previous sectioned we learned that if the processor goes to the processes page table with a virtual page

frame number for which no entry was present in the table then two cases arise.

1. Either the process has tried to access an invalid memory address2. The physical page corresponding to the virtual address was not loaded into physical memory

Out of the two cases above, the case 1 is the case where the process tries to memory address which it is not

allowed. In this case a page fault is generated and the kernel terminates the process.

While in case ’2′, as already explained, the physical page corresponding to the virtual address is not yet loadedinto physical memory. In this case also a page fault is generated and the kernel then tries to bring the requiredmemory page into physical memory from hard disk.

Since this operation of bringing a page from hard disk into physical memory is time consuming so by this time acontext switch between processes happens and some other process is brought into execution. Meanwhile thepage of the earlier process is brought into physical memory and the page tables are updated and then this

process is brought back into execution again from the same instruction that caused the ‘page fault’.

This is known as demand paging where all the memory pages corresponding to a process are not present in thephysical memory at any given time. This saves the physical memory from clogging up with non-required memory

pages while when necessary these pages can be brought into physical memory through page fault (as explainedabove).

> Add your comment

Linux provides several powerful administrative tools and utilities which will help you tomanage your systems effectively. If you don’t know what these tools are and how to use them, you could bespending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you

understand system administration tools, which will help you to become an effective Linux system administrator.

Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

1. 50 Linux Sysadmin Tutorials

2. 50 Most Frequently Used Linux Commands (With

Examples)3. Top 25 Best Linux Performance Monitoring and

Awk Introduction – 7 Awk Print Examples

Advanced Sed Substitution Examples

8 Essential Vim Editor Navigation

Page 4: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 4/8

Debugging Tools4. Mommy, I found it! – 15 Practical Linux Find

Command Examples5. Linux 101 Hacks 2nd Edition eBook

Fundamentals25 Most Frequently Used Linux IPTables

Rules ExamplesTurbocharge PuTTY with 12 Powerful Add-

Ons

{ 6 comments… read them below or add one }

1 Narendra February 17, 2012 at 8:49 am

Very good article, keep writing.

2 Himanshu February 17, 2012 at 11:26 am

@Narendra

Thanks

3 Ansil Hameed February 20, 2012 at 7:22 am

Simple and clear…well done .expecting more

4 Dan Saint-Andre March 1, 2012 at 10:52 am

I would appreciate a similar discussion of the device detection, driver identification, module loading, “new

device” event processing. This is what happens in ‘upstart’ or ‘systemd’ as devices come and go.

We take this behavior for granted, but I have seen precious little that walks through how it is supposed to

happen. I believe, too, that there are variations on behavior that depend on distribution idiosyncrasies.

~~~ 8d;-/ Dan

5 Haroun Ansari February 8, 2013 at 2:24 am

Good work .. Well explained …

6 KK February 24, 2013 at 7:20 am

I would like to see with practical example.

Leave a Comment

Name

Page 5: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 5/8

E-mail

Website

Notify me of followup comments via e-mail

Submit

Previous post: XSS Attack Examples (Cross-Site Scripting Attacks)

Next post: 5 Practical Linux fuser Command Examples

RSS | Email | Twitter | Facebook | Google+

Search

COURSE

Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy

EBOOKS

Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build aStrong Foundation in Linux

Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting

Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk

Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim EditorNagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

POPULAR POSTS

12 Amazing and Essential Linux Books To Enrich Your Brain and Library

50 UNIX / Linux Sysadmin Tutorials

50 Most Frequently Used UNIX / Linux Commands (With Examples)How To Be Productive and Get Things Done Using GTD

Page 6: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 6/8

30 Things To Do When you are Bored and have a Computer

Linux Directory Structure (File System Structure) Explained with Examples

Linux Crontab: 15 Awesome Cron Job ExamplesGet a Grip on the Grep! – 15 Practical Grep Command Examples

Unix LS Command: 15 Practical Examples

15 Examples To Master Linux Command Line HistoryTop 10 Open Source Bug Tracking System

Vi and Vim Macro Tutorial: How To Record and Play

Mommy, I found it! -- 15 Practical Linux Find Command Examples

15 Awesome Gmail Tips and Tricks15 Awesome Google Search Tips and Tricks

RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams

Can You Top This? 15 Practical Linux Top Command Examples

Top 5 Best System Monitoring ToolsTop 5 Best Linux OS Distributions

How To Monitor Remote Linux Host using Nagios 3.0

Awk Introduction Tutorial – 7 Awk Print ExamplesHow to Backup Linux? 15 rsync Command Examples

The Ultimate Wget Download Guide With 15 Awesome Examples

Top 5 Best Linux Text Editors

Packet Analyzer: 15 TCPDUMP Command ExamplesThe Ultimate Bash Array Tutorial with 15 Examples

3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id

Unix Sed Tutorial: Advanced Sed Substitution Examples

UNIX / Linux: 10 Netstat Command ExamplesThe Ultimate Guide for Creating Strong Passwords

6 Steps to Secure Your Home Wireless Network

Turbocharge PuTTY with 12 Powerful Add-Ons

CATEGORIES

Linux Tutorials

Vim Editor

Sed Scripting

Awk Scripting

Bash Shell Scripting

Nagios MonitoringOpenSSH

IPTables Firewall

Apache Web Server

MySQL Database

Perl Programming

Google Tutorials

Ubuntu Tutorials

PostgreSQL DB

Page 7: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 7/8

Hello World ExamplesC Programming

C++ Programming

DELL Server Tutorials

Oracle Database

VMware Tutorials

Ramesh Natarajan

Follow

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to,

troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write

articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and

the blog.

Support Us

Support this blog by purchasing one of my ebooks.

Bash 101 Hacks eBook

Sed and Awk 101 Hacks eBook

Vim 101 Hacks eBook

Nagios Core 3 eBook

Contact Us

Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions

about this site. You can also simply drop me a line to say hello!.

Follow us on Google+

Follow us on Twitter

Become a fan on Facebook

Page 8: Linux Memory Management – Virtual Memory and Demand Paging

12/5/13 Linux Memory Management – Virtual Memory and Demand Paging

www.thegeekstuff.com/2012/02/linux-memory-management/ 8/8

Copyright © 2008–2013 Ramesh Natarajan. All rights reserved | Terms of Service