Computer Science › Section D · Operating systems › Topic D.3

The memory manager and the file manager

Two kernel components look after the computer's storage. The memory manager shares out main memory among running programs, and the file manager organises mass storage into files and folders that people and programs can find and protect.

Two kinds of storage, two managers

A computer keeps data in two very different places. Main memory is fast but limited, and it forgets everything when the power goes off. Mass storage, such as a solid-state drive, is slower but far larger and keeps data without power. The previous topic, Operating system architecture and booting, named the kernel component for each: the memory manager for main memory and the file manager for mass storage. Both share a scarce resource fairly and stop one program or user from damaging another's data.

The memory manager's duties

If a computer runs one task at a time, memory management is easy: load the program at a fixed place, run it, and replace it with the next. With many programs running at once, the memory manager has three jobs. It finds and assigns space for each program and its data. It keeps each program inside its own space, so one cannot overwrite another's memory or the OS itself. And it tracks space freed as programs finish, so it can be reused.

Virtual memory and paging

Modern systems give each process its own virtual address space, as if it had a large memory all to itself. That space is cut into fixed-size blocks called pages, commonly 4 KiB, and physical memory into frames of the same size. A page table for each process records which frame holds each page, or that the page is currently stored on disk. A hardware memory management unit (MMU) translates every virtual address into a physical one as the program runs. Pages not needed right now can wait on disk, so programs can together use more memory than the machine has. The large space this creates is virtual memory.

Worked example: translating an address

Pages are 4,096 bytes. A process's page table maps page 0 to frame 5, page 1 to frame 2, page 2 to frame 7, and marks page 3 as on disk. Where is virtual address 9,000? 1. Divide by the page size: 9,000 ÷ 4,096 = 2 remainder 808, so the address is in page 2 at offset 808. 2. Look up page 2: it is in frame 7. 3. Physical address = 7 × 4,096 + 808 = 29,480. 4. Now try 13,000: 13,000 ÷ 4,096 = 3 remainder 712, which is page 3, on disk, so the access causes a page fault. 5. Check: 29,480 − 28,672 = 808, the same offset, because translation changes the page number, never the offset.

Page faults and thrashing

A page fault happens when a program touches a page that is not in main memory. The OS pauses the program, loads the page from disk into a free frame, perhaps writing another page out to make room, updates the page table and resumes the program. Disk access is far slower than memory, so faults are costly. The pages a program is actively using form its working set. If all the working sets fit in memory, faults are rare. If not, the system can spend most of its time moving pages instead of working. This is thrashing; closing programs or adding memory cures it.

The file manager

The file manager keeps a record of every file in mass storage: where its data is, who may use it, and which areas of the drive are free for new files or for files that grow. These records are stored on the drive itself. Files are grouped into directories, also called folders, and directories can hold subdirectories, forming a tree. A path names a file by the chain of directories leading to it, such as /home/ana/notes.txt on Linux or macOS, or C:\Users\ana\notes.txt on Windows, which uses backslashes. Ordinary programs reach files only through the file manager.

Opening a file, and file system formats

Before a program can use a file it asks the file manager to open it. The file manager checks that the requested access is allowed, then returns the information the program needs to find and work with the data. Each file also carries metadata: its name, size, timestamps, owner and permissions. The layout of these records on a drive is a file system format: NTFS on Windows, APFS on macOS, ext4 on many Linux systems, and FAT32 or exFAT on USB drives that must work everywhere. NTFS and ext4 use journaling, noting intended changes before making them so they can recover after a crash; APFS gets similar safety from a copy-on-write design.

Permissions

The file manager also enforces who may do what. Unix-like systems such as Linux and macOS give every file three sets of permissions: for the owner, for the owner's group and for all others. Each set says whether that class of user may read (r), write (w) or execute (x) the file, shown as a nine-character string such as rw-r--r--. Each set can also be written as one octal digit, adding 4 for r, 2 for w and 1 for x. Windows instead attaches an access control list (ACL) to each file, naming users and groups and what each may do.

Worked example: reading a permission string

A file shows the permissions rwxr-xr--. Who can do what? 1. Split it into three groups of three: rwx, r-x and r--. 2. The owner has rwx: read, write and execute. 3. Members of the file's group have r-x: they can read and run it but not change it. 4. Everyone else has r--: read only. 5. As octal digits: rwx is 4 + 2 + 1 = 7, r-x is 4 + 1 = 5, r-- is 4, so the mode is 754. Check: a dash always means that permission is missing, and only the owner's set contains a w, so only the owner can edit the file.

Virtual machines and containers

The idea of private resources can be pushed further. A virtual machine runs a complete guest operating system on simulated hardware, managed by a hypervisor, so one physical server can host many isolated systems. A container, such as one run by Docker, is lighter: it shares the host's kernel but gives an application its own view of files and processes, so it starts quickly. Both underpin cloud computing.

Common mistakes to avoid

Virtual memory does not mean a program needs no physical memory; pages must be in main memory to be used. A page fault is a normal event, not a crash, although too many of them cause thrashing. Translation keeps the offset within a page; only the page number becomes a frame number. In a permission string the three sets belong to owner, group and others, in that order, so rwxr--r-x means others can read and run the file while the group can only read it. And a file's records live on the drive, not in the program that uses it.

Key takeaways

The memory manager assigns main memory, keeps each program within its own space and reclaims freed space. Virtual memory gives each process a private address space divided into pages; page tables and the MMU map pages to physical frames. A page fault loads a missing page from disk, and too many faults cause thrashing. The file manager records where files are, who may use them and what space is free, organises directories into a tree named by paths, and grants access when a file is opened. Formats include NTFS, APFS, ext4 and exFAT, and permissions or ACLs control access.

Flashcards

Flashcard

Memory manager

Kernel component that shares out main memory

Assigns, protects and reclaims space.

Test your understanding

Question 1 of 5

Pages are 1,024 bytes. In which page, and at what offset, is virtual address 3,100?

Question 2 of 5

A program reads a variable whose page is currently stored on disk. What happens next?

Question 3 of 5

A file has the permissions rw-r-----. Which statement is true?

Question 4 of 5

A computer becomes extremely slow, and its disk light is almost always on, after many large programs are opened. What is the most likely cause?

Question 5 of 5

What does journaling do in file systems such as NTFS and ext4?

← Previous topicNext topic →