Basic workout on Memory v. Disk


Hardware v. Software

What is a Program?

A list of instructions. A precise definition of what is to be done, as opposed to an English language description of what is to be done. A precise description is an algorithm, a recipe that can be followed blindly by a machine.

A list of instructions to be executed one by one by a machine that has no memory of past instructions and no knowledge of future instructions.

There are many processes that we observe in nature (animal vision, language use, consciousness) and invent in culture (calculating prime numbers, sorting a list). Some we have converted to algorithms. Some remain in the domain of an English language description. The Church-Turing thesis claims that "Any well-defined process can be written as an algorithm".

In the debate about the possibility of Artificial Intelligence, some disagree that the Turing model of an algorithm is elastic enough to capture consciousness and other mental processes, and therefore they will (when understood) represent a new type of "algorithm". No such new type of algorithm has ever been presented though.



But how is this Program, this list of instructions, to be ENCODED?

What (if any) is the difference between the MACHINE and the PROGRAM?
Many possibilities:





(3) is what we're used to, but it's not the only model.




Also, we can have minimalist Turing machines, or complex Turing machines. We can have pressure to: [Footnote: Technically, a Turing machine has infinite memory.]

As new Operating Systems are invented and modified over the years, they constantly redefine the boundary between what should be hardware and what should be software.




The standard model for a Computer System




Definitions

"Memory" = RAM = "working memory" = the fast, temporary medium that we actually run programs in. Reset every time we restart the computer. E.G. PC HAS 64 MB MEMORY.

Hard disk, floppy disks (diskettes) = the permanent medium that we store programs and data files in. E.G. PC HAS 10 GB HARD DISK. To some people this is "long-term memory" (or "primary and secondary memory") but this is confusing.

BE WARNED! - If you use "memory" I will assume you mean RAM.






Questions

If RAM was as fast as registers, would we use registers at all?


If disk was as fast as memory, would we use memory at all?



If somebody is marketing "permanent RAM", that retains its contents in a power outage, then in what sense is this not "disk"?


For the whole history of computers, we needed all the speed we could get, which is why these complex, multi-tiered machines have evolved.

Mathematically, this multi-tier system is not necessary. Only a single medium is needed.

From the engineering point of view, it is doubtful if a single-tier system will ever come into existence, for the reasons above. But it is useful to think about the possibilities for different architectures, especially when later in this course we consider using disk as an overflow of memory, and memory as a cache for disk.




Compile-time, Load-time and Run-time

The standard model:

Program is written "in factory" in HLL, with comments, English-like syntax and variable names, macros and other aids to the programmer.

Program is then compiled "in factory" into an efficient, machine-readable version, with all of the above stripped, optimisations made, and everything translated and resolved as much as possible, so as little as possible needs to be done when it is run.

At different times, on different machines, and with different other programs already running, the user will "launch" or "run" a copy of this compiled program. Any further translation that could not be done at compile-time will now be done at this "load-time". Then the algorithm itself actually starts to run.

Any change that has to be done while the algorithm has already started to run, is a change at "run-time".




Compile-time, Load-time and Run-time (continued)

Memory mapping (or binding)

Consider what happens with the memory allocation for a program's global variable.

Programmer defines global variable x. Refers to x throughout his High-Level Language code:

	   do 10 times
		print(x)
		x := x+7
Obviously when the program is running, some memory location will be set aside for x and the actual machine instructions will be:
	   do 10 times
		read contents of memory location 7604 and print them
		read contents of loc 7604, add 7, store result back in loc 7604
How "x" maps to "7604" can happen in many ways:
  1. The programmer maps x to 7604 in the source code.

  2. Compile-time. If x is mapped to 7604 at this point (or before) then the program can only run in a certain memory location. (e.g. DOS - single user, single process)

  3. Load-time. The compiler might maps x to "start of program + 604". Then when the program is launched, the OS examines memory, decides to run the program in a space starting at location 7000, resolves all the addresses to absolute numerical addresses, and starts running.

  4. Run-time - even after the program has started, we may change the mapping (move the program, or even just bits of it, in memory), so that next time round the loop it is:
    	read contents of memory location 510 and print them
    	read contents of loc 510, add 7, store result back in loc 510
    
    Obviously, OS has to manage this!
    User can't. Programmer can't. Even Compiler designer can't.




Compile-time, Load-time and Run-time (continued)

Question - Why is it not practical for the OS to say to a user on a single-user system: "I'm sorry, another program is occupying memory space 7000-8000. Please terminate the other program before running your program"

Question - Why is it not practical for the OS to say to a user on a multi-user system: "I'm sorry, another program is occupying memory space 7000-8000. Please wait until the other program terminates before running your program"

Question - Why can't program writers simply agree on how to divide up the space? Some programs will take memory locations 7000-8000. Other programs will take locations 8000-9000, etc.

Question - The user's program has started. The OS needs to change what memory locations things map to, but binding is done at load-time. Why is it not practical for the OS to say to the user: "Please reload your program so I can re-do the binding"

Question - Why is it not practical to say to a user: "Please recompile your program"