28

In his autobiography, Just for Fun, Linus mentions the "page-to-disk" feature that was pivotal in making Linux a worthy competitor to Minix and other UNIX clones of the day:

I remember that, in December, there was this guy in Germany who only had 2 megabytes of RAM, and he was trying to compile the kernel and he couldn't run GCC because GCC at the time needed more than a megabyte. He asked me if Linux could be compiled with a smaller compiler that wouldn't need as much memory. So I decided that even though I didn't need the particular feature, I would make it happen for him. It's called page-to-disk, and it means that even though someone only has 2 mgs of RAM, he can make it appear to be more using the disk for memory. This was around Christmas 1991.

Page-to-disk was a fairly big thing because it was something Minix had never done. It was included in version 0.12, which was released in the first week of January 1992. Immediately, people started to compare Linux not only to Minix but to Coherent, which was a small Unix clone developed by Mark Williams Company. From the beginning, the act of adding page-to-disk caused Linux to rise above the competition.
That's when Linux took off. Suddenly there were people switching from Minix to Linux.

Is he essentially talking about swapping here? People with some historical perspective on Linux would probably know.

  • 11
    Invented in the 1960s, possibly earlier. – Ex Umbris Aug 13 at 5:45
  • 6
    Swapping associated with virtual memory was first available on the Manchester Atlas, released in 1962. Non-paged swapping existed before that. – Stephen Kitt Aug 13 at 7:19
36

Yes, this is effectively swapping. Quoting the release notes for 0.12:

Virtual memory.

In addition to the "mkfs" program, there is now a "mkswap" program on the root disk. The syntax is identical: "mkswap -c /dev/hdX nnn", and again: this writes over the partition, so be careful. Swapping can then be enabled by changing the word at offset 506 in the bootimage to the desired device. Use the same program as for setting the root file system (but change the 508 offset to 506 of course).

NOTE! This has been tested by Robert Blum, who has a 2M machine, and it allows you to run gcc without much memory. HOWEVER, I had to stop using it, as my diskspace was eaten up by the beta-gcc-2.0, so I'd like to hear that it still works: I've been totally unable to make a swap-partition for even rudimentary testing since about christmastime. Thus the new changes could possibly just have backfired on the VM, but I doubt it.

In 0.12, paging is used for a number of features, not just swapping to a device: demand-loading (only loading pages from binaries as they’re used), sharing (sharing common pages between processes).

| improve this answer | |
  • 10
    Heh. Imagine barely having enough disk space to store the source code of GCC. – user253751 Aug 12 at 21:17
  • 5
    @user253751 Imagine having not having enough disk space to make a 512Kb partition for testing. – Ismael Miguel Aug 14 at 10:05
30

Yes, that's exactly the concept known as paging or swapping. (A long time ago these terms had slightly different meanings, but in the 21st century, they're synonymous except perhaps in the context of some non-Unix operating systems.)

To be clear, swapping wasn't an innovative feature: most “serious” Unix systems had it, and the feature is older than Unix. What swapping did for Linux was to turn it into a “serious” Unix, whereas MINIX was meant for educational purposes.

Swapping today is still the same concept. The heuristics for deciding which pages to save and when to save them have become a lot more complex, but the basic principle remains.

| improve this answer | |
  • 1
    “swapping wasn’t an innovative feature” Other answers talk about whole-image swapping. Was per-page swapping a thing before Linux or did Linus invent that? – Ethan Reesor Aug 13 at 20:57
  • 2
    Partial swapping had been a thing for a long time once MMUs were used (no point in unnecessarily putting a whole image to disk if the memory would just sit idle anyway); but before recoverable page faults, the whole image needed to be in before you could resume a process. Demand-paging was known and used before the 80386, but having the MMU on-chip was unusual at that time. It used to be an optional external component. Though of course "external" only makes sense with microprocessors, and demand-paging started on bigger things. – user428063 Aug 13 at 21:11
  • 8
    @EthanReesor Page swapping was a thing since the early 1960s. It's older than Unix, let alone Linux. The Wikipedia article that I cite has references. Even Windows had page swapping before Linux existed (Windows 3.0 was released in 1990). – Gilles 'SO- stop being evil' Aug 13 at 21:25
  • 2
    IIRC swapping was one of the fundamental features of Windows 1.0. It's what made it possible to use more than 640kB of memory. But since the 8086 didn't have a MMU with page tables, it swapped segments. – MSalters Aug 14 at 10:12
  • 1
    @MSalters I remember Borland in particular having success with manual swapping of functions and data, even in DOS. – Mark Ransom Aug 15 at 3:23
22

Swapping is a concept predating virtual memory and even memory protection: it just means putting a process on disk to make room for another. The original Unix had two quirks in that regard: "shared text" programs that kept the program code only once in memory and swapped out the data section only. And it had the "fork" system call that swapped out a process to disk while not replacing the memory image and instead keeping a copy (the child) running.

Page-to-disk, as opposed to swapping, allows for processes to run that do not fit the physical memory. It requires all of protectable memory, memory mapping of virtual addresses to physical addresses, and a restartable page fault mechanism that will allow to change the mapping from an unmapped virtual address to a reasonably selectable physical address and resuming the command that had to be aborted because of the missing mapping.

UNIX was able to run on 68000 processors (including swapping) without MMU, and it made good use of an MMU if available for memory protection, but it took the 68010 to actually have the mechanisms allowing for resuming a program after a page fault.

The 80386 was in many respects a crummy and outdated design. But its built-in MMU and the ability to properly page-fault made it immediately more viable for UNIX-like systems that were not merely able to swap but to page-to-disk.

It is sort of a historical irony that this great sacrifice of silicon (a full-fledged MMU and virtual-capable CPU design took quite a bit of die space) to the gods of modern systems was mainly taken up by a hobbyist, and the "big fish" like Xenix and OS/2 fell to the wayside eventually.

While you can call "nothing paged in and not scheduled to run" the same as "swapped", it's not really an all-or-nothing proposition like the original meaning of "swapped" was.

The difference got lost in the decades since then since demand-paging was so much more useful and scaled better than ordinary swapping that it replaced it once the necessary CPU and MMU features became commonplace, but the slowdown and thrashing associated with either made for a similar look-and-feel.

| improve this answer | |
  • 8
    Oh wow, that's where fork semantics came from? copy-on-write "sharing" every page always seemed like a somewhat strange semantic to invent in the first place, but makes more sense in the context of swapping whole process images, not pages. – Peter Cordes Aug 12 at 23:26
  • 5
    Well yeah, the combination fork&exec was basically zero extra cost since the memory contained a copy of the forked process image already anyway before running into exec. – user428063 Aug 13 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.