From a program to a process

A program is a set of instructions and data that can be executed, it is stored in a persistent memory such as a disk. When a program is started, the OS reads the program from the disk and generates a memory image in the RAM. A memory image includes the program code and its data, additionally, the memory image includes libraries that program uses. Meanwhile, some associated metadata are created including standard input, output, and error files descriptors. Lastly the OS starts the process executing.

Difference between a program and a process

A process a running instance of a program, it is a program with its code and data stored in the physical memory RAM along with some metadata (e.g. PID, process status), and the opened files it has. The combination of the memory occupied by the program and all of its metadata, opened files, is called a process. A program is a set of instructions and data that can be executed. It is typically stored on a disk as an executable file or script.

Memory management and paged memory

Virtual RAM

In some OS like Linux, a portion of secondary memory (e.g. hard disk) is being used as virtual RAM AKA swap area. The Unix kernel will write the current idle processes into the virtual RAM to save the actual RAM. This allows larger number of active processes to address more memory than what is physically available. When original processes are needed again, they are read back into the RAM.

Paged memory

Paged memory is a memory management scheme, where each process is broken up into a number of equal sized pages. A single page (typically 4 bytes) might contains a single program, more than one program, or just parts of a program. These pages could be anywhere in the RAM and some pages could be in the virtual RAM.

Virtual address space (per process)

In linux, each process is given its own virtual address space or virtual memory (per process), that starts at zero an goes up to the maximum address range that the CPU can support. It is the set of all possible memory addresses that a process can use. Each virtual address space will be divided into pages. However, the actual physical memory location in RAM may not align with these virtual addresses, especially when multiple processes are running concurrently and each has different starting address in the RAM. To manage the mapping between virtual address space and the actual memory, the Memory Management Uni (MMU) and page table comes in to play.

Page table

In a virtual memory system, a page table contains information about which logical location maps to which physical location, it is used by the MMU in the CPU. The page table contains an entry for every page of memory that the process uses. Each entry in the page table maps a virtual address (used by the process) to either a physical address in the RAM or an indication that the page is currently stored in mass storage (e.g. hard disk). The kernel manages this table and moves pages to and from mass storage. (The virtual address space also known as logical memory)

Retrieve pages from mass storage

If a program attempts to use memory that the page table has mapped to mass storage, the access will cause an interrupt that will transfer the control from the running process to the operating system kernel without any indication to the process. When kernel gains control, it will find the pages on disk (from the virtual RAM) and read it into the free part of the RAM. Then it sets up the page table to point to the new physical location and return the control to the process. (During the interruption, the state of the process is saved)

If the kernel needs to read in a page from the disk and discovered no free space in the RAM is available, it has to write an existing block out from the RAM to the swap area to free up space. If this happens too often, it can lead to a serious system performance slowdown, because accessing data from mass storage is significantly slower than accessing it from RAM.

Virtual storage optimisation

To optimise the use of virtual memory and mitigate the performance impact due to frequent page swapping. Various techniques are adopted in modern OS.

  • Shared memory Some processes may share pages that are mapped to the same physical memory location. This sharing reduces the overall memory usage. When multiple processes use the same memory content, there is no need to duplicate it in the physical RAM.
  • Copy-on-Write (COW) When a process forks to create a new process (child process typically), the new process initially shares physical memory pages with the parent process. However, either parent or child process attempts to modify a shared page, a copy of that page is created for the process that made the modification.

Operating_systemINFO1112Memory_managementUnixProcess_management