Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is paging?

Paging is explained here, slide #6 :

http://www.cs.ucc.ie/~grigoras/CS2506/Lecture_6.pdf

in my lecture notes, but I cannot for the life of me understand it. I know its a way of translating virtual addresses to physical addresses. So the virtual addresses, which are on disks are divided into chunks of 2^k. I am really confused after this. Can someone please explain it to me in simple terms?

like image 744
John Curtsy Avatar asked May 11 '11 23:05

John Curtsy


People also ask

What is paging in operating system?

What is paging? Paging is a storage structure that enables the operating framework to fetch processes from the secondary storage into the main memory in the form of pages. In the Paging method, the main memory is split into small fixed-size blocks of physical memory, which is known as frames.

What is the paging method of memory?

In the Paging method, the main memory is split into small fixed-size blocks of physical memory, which is known as frames. The size of a frame must be preserved the same as that of a page to have maximum use of the main memory and to prevent external fragmentation.

What are the advantages and disadvantages of page paging?

Here, are advantages of using Paging method: Swapping is easy between equal-sized pages and page frames. Here, are drawback/ cons of Paging: Page tables consume additonal memory. Multi-level paging may lead to memory reference overhead.

What is the difference between paging and external fragmentation?

Therefore, the free memory space is separated into small pieces which may create a problem of external fragmentation Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the main memory in the form of pages.


2 Answers

Paging is, as you've noted, a type of virtual memory. To answer the question raised by @John Curtsy: it's covered separately from virtual memory in general because there are other types of virtual memory, although paging is now (by far) the most common.

Paged virtual memory is pretty simple: you split all of your physical memory up into blocks, mostly of equal size (though having a selection of two or three sizes is fairly common in practice). Making the blocks equal sized makes them interchangeable.

Then you have addressing. You start by breaking each address up into two pieces. One is an offset within a page. You normally use the least significant bits for that part. If you use (say) 4K pages, you need 12 bits for the offset. With (say) a 32-bit address space, that leaves 20 more bits.

From there, things are really a lot simpler than they initially seem. You basically build a small "descriptor" to describe each page of memory. This will have a linear address (the address used by the client application to address that memory), and a physical address for the memory, as well as a Present bit. There will (at least usually) be a few other things like permissions to indicate whether data in that page can be read, written, executed, etc.

Then, when client code uses an address, the CPU starts by breaking up the page offset from the rest of the address. It then takes the rest of the linear address, and looks through the page descriptors to find the physical address that goes with that linear address. Then, to address the physical memory, it uses the upper 20 bits of the physical address with the lower 12 bits of the linear address, and together they form the actual physical address that goes out on the processor pins and gets data from the memory chip.

Now, we get to the part where we get "true" virtual memory. When programs are using more memory than is actually available, the OS takes the data for some of those descriptors, and writes it out to the disk drive. It then clears the "Present" bit for that page of memory. The physical page of memory is now free for some other purpose.

When the client program tries to refer to that memory, the CPU checks that the Present bit is set. If it's not, the CPU raises an exception. When that happens, the CPU frees up a block of physical memory as above, reads the data for the current page back in from disk, and fills in the page descriptor with the address of the physical page where it's now located. When it's done all that, it returns from the exception, and the CPU restarts execution of the instruction that caused the exception to start with -- except now, the Present bit is set, so using the memory will work.

There is one more detail that you probably need to know: the page descriptors are normally arranged into page tables, and (the important part) you normally have a separate set of page tables for each process in the system (and another for the OS kernel itself). Having separate page tables for each process means that each process can use the same set of linear addresses, but those get mapped to different set of physical addresses as needed. You can also map the same physical memory to more than one process by just creating two separate page descriptors (one for each process) that contain the same physical address. Most OSes use this so that, for example, if you have two or three copies of the same program running, it'll really only have one copy of the executable code for that program in memory -- but it'll have two or three sets of page descriptors that point to that same code so all of them can use it without making separate copies for each.

Of course, I'm simplifying a lot -- quite a few complete (and often fairly large) books have been written about virtual memory. There's also a fair amount of variation among machines, with various embellishments added, minor changes in parameters made (e.g., whether a page is 4K or 8K), and so on. Nonetheless, this is at least a general idea of the core of what happens (and it's still at a high enough level to apply about equally to an ARM, x86, MIPS, SPARC, etc.)

like image 184
Jerry Coffin Avatar answered Oct 03 '22 15:10

Jerry Coffin


Simply put, its a way of holding far more data than your address space would normally allow. I.e, if you have a 32 bit address space and 4 bit virtual address, you can hold (2^32)^(2^4) addresses (far more than a 32 bit address space).

like image 27
soandos Avatar answered Oct 03 '22 15:10

soandos