Implementing Operating System #8

Tharushi Chamalsha
2 min readSep 10, 2021

This is the last part of my Operating System article series. Last week we allocated a virtual memory to the Operating system. Today we are going to allocate page frames.

An important aspect of operating systems, virtual memory is implemented using demand paging. Demand paging necessitates the development of a page replacement algorithm and a frame allocation algorithm. Frame allocation algorithms are used if you have multiple processes; it helps decide how many frames to allocate to each process.

There are two types of algorithms commonly used to allocate frames to a process. They are :

  1. Equal allocation
  2. Proportional Allocation

The role of the page frame allocator is let the Os know which part of memory is free to use.

How much memory is there?

Now we should know how much memory is available to use. The easiest way to do this is to read it from the multiboot structure passed to us by GRUB. Another way is to export labels at the beginning and the end of the kernel binary from the linker script. Let's use the second way. In order to do that store the following script in the linker file.

Managing available memory

Memory management is a critical part of any operating system kernel. Providing a quick way for programs to allocate and free memory on a regular basis is a major responsibility of the kernel. There are several ways to do this: bitmaps, linked lists, trees, the Buddy System.

Accessing page frame

The page frame allocator delivers the page frame’s physical start address. There is no page table that points to this page frame since it is not mapped in. What is the best way to read and write data to the frame? We must map the page frame into virtual memory by modifying the kernel’s PDT and/or PT.

Now you can add log file to the directory. Make a new directory called utils. And locate a log.h file.

log.h

And add common files to the directory as well.

common.c
common.h

A Kernal Heap

Heap allocation is an allocation procedure in which heap is used to manage the allocation of memory. Heap helps in managing dynamic memory allocation. In heap allocation, creation of dynamic data objects and data structures is also possible as same as stack allocation. Heap allocation overcomes the limitation of stack allocation.

So let's create kheap files.

kheap.c
kheap.h

Now that we have a page frame allocator we can implement kmalloc and kmalloc_page to use in the kernel.

--

--