How a computer program stored on disc is loaded into memory and starts to execute run

When you turn on the power to a computer, the first program that runs is usually a set of instructions kept in the computer's firmware called the boot ROM. For a typical PC, this can be the basic input output system (BIOS), or on newer machines, the unified extensible firmware interface (UEFI). This code examines the system hardware to make sure everything is functioning properly and in the case of UEFI, that the boot software is legitimate and secure. Once the test has successfully completed, the firmware continues the boot process.

The bootstrap loader, or boot loader, is a small program that has a single function: It loads the operating system into memory and allows it to begin operation. In the most basic form, the bootstrap loader sets up the small driver programs that interface with and control the various hardware subsystems of the computer. It sets up the divisions of memory that hold the operating system, user information and applications. It establishes the data structures that hold the myriad signals, flags and semaphores that are used to communicate within and between the subsystems and applications of the computer. Then it turns control of the computer over to the operating system.

The operating system's tasks, in the most general sense, fall into several categories:

  • Processor management
  • Memory management
  • Device management
  • Storage management
  • Application interface
  • User interface
  • System security management

These tasks define the core of nearly all operating systems. Next, let's look at the tools the operating system uses to perform each of these functions.

The CPU is the heart of the computer. A program is a sequence of instructions stored in main memory. When a program is run, the CPU fetches the instructions and executes or follows the instructions.

The fetch/decode/execute cycle

  • fetch - get the next instruction from main memory.
  • decode - determine what instruction to perform.
  • execute - perform the instruction.

Every program ends up as a sequence of basic instructions that consist of arithmetic and logic operations and control flow operations.

Arithmetic and logic operations include add, subtract, multiply, divide and comparison of values (equality, less than, greater than).

Control flow operations are used to determine what instruction to execute next. For example, based on the instruction, the program may skip or branch to another part of the instructions list.

You will learn the details of how CPUs process instructions in CS 271, Computer architecture and assembly language.

Main memory or RAM is used to store the program while it is executing and to store the data that the program is working with.

RAM details

  • The CPU is able to quickly access any location in RAM.
  • RAM is called volatile storage. Unlike persistent storage, when a computer is turned off or when a program finishes executing, the values stored in RAM are erased.
  • RAM is divided into storage units called bytes. A byte is a sequence of eight bits.
  • A bit is the smallest element of the RAM and it stores a binary digit, either a 0 or a 1. Every program and every data value in your computer is stored as sequences of Os and ls.

Secondary storage provides long lasting and persistent storage. Unlike RAM, data stored within secondary storage does not disappear when a computers is turned off or restarted. The most common form of secondary storage for large computers is a disk drive but computers can use other forms of secondary storage such as solid state drives which use memory chips that maintain data values without power.

Like main memory, secondary storage also stores information as sequences of 0s and 1s as bits and bytes.

We typically think of keyboards and mice but input devices can include cameras, microphones, and many other types of various sensors when you start thinking of computers embedded in cars, electronics, and almost any electrical device.

The information a computer sends to the outside world is called output. If a person is involved, output is typically sent to an output device such as the computer screen or a printer. Not all programs will output data to an output device. Instead, the output may be sent out over a computer network or stored in a database.

Questions for today

  • What does "run" mean?
  • How does code run on the CPU?

CPU - Machine Code

  • The CPU implements "machine code" instructions
  • Each machine code instruction is extremely simple  -e.g. add 2 numbers

     -e.g. compare 2 numbers

  • Javascript code we've used: print(1, 2)
  • Javascript is not machine code
  • Javascript does not run on the CPU directly
  • Javascript works in translation
    -print(1, 2)translates to a lot of machine instructions to actually run on the CPU
  • Only machine code runs on the CPU

"Software" is the general category of code which runs on the hardware. If the hardware is a player piano, then the software is the music. The common case is a "program" like Firefox -- software you run on your computer to solve a particular problem. A computer can run multiple programs at the same time and is responsible for keeping their memory separate.

A CPU understands a low level "machine code" language (also known as "native code"). The language of the machine code is hardwired into the design of the CPU hardware; it is not something that can be changed at will. Each family of compatible CPUs (e.g. the very popular Intel x86 family) has its own, idiosyncratic machine code which is not compatible with the machine code of other CPU families.

What is a Program/App?


How a computer program stored on disc is loaded into memory and starts to execute run
  • What is a Program or App?
    - e.g. Firefox
  • Firefox is made of millions of machine code instructions - Run top to bottom (just like Javascript!) - The Firefox window appears (the first 1000 instructions) - Its menus appear (the next 1000)

    - The cursor blinks waiting for you to type

  • The instructions such that, when run, "Firefox" actions happen
  • Firefox.exe is a file in the file system, 80 MB in size
    (".exe" is a Windows convention for the name of a program file)
  • The Firefox.exe file is mostly the bytes of machine code instructions
  • Each instruction is, say, 4 bytes in size
  • Firefox.exe at 80 MB is about 20 million machine instructions

The machine code defines a set of individual instructions. Each machine code instruction is extremely primitive, such as adding two numbers or testing if a number is equal to zero. When stored, each instruction takes up just a few bytes. When we said earlier that a CPU can execute 2 billion operations per second, we meant that the CPU can execute 2 billion lines of machine code per second.

A program, such as Firefox, is made up of a sequence of millions of these very simple machine code instructions. It's a little hard to believe that something as rich and complicated as Firefox can be built up out of instructions that just add or compare two numbers, but that is how it works. A sand sculpture can be rich and complicated when viewed from a distance, even though the individual grains of sand are extremely simple.

How Does a Program Run?

  • CPU runs a "fetch/execute cycle" -fetch one instruction in sequence -execute (run) that instruction, e.g. do the addition

    -fetch the next instruction, and so on

  • Run a program = Start CPU running on its 1st instruction it runs down through all of the machine code, running the program

    the program will have instructions like "return to step 3" to keep it running

  • Super simple machine code instructions run at the rate of 2 billion per-second

The CPU runs instructions using a "fetch-execute" cycle: the CPU gets the first instruction in the sequence, executes it (adding two numbers or whatever), then fetches the next instruction and executes it, and so on. Some of the instructions affect the order that the CPU takes through the instruction sequence. For example, an instruction might direct the CPU to jump back to an earlier point in the instruction sequence (loops are implemented this way), or to skip over the next instruction if a particular condition is true (if-statements are implemented this way).

How a computer program stored on disc is loaded into memory and starts to execute run

How Does a Program Start?

  • The file Firefox.exe contains its instructions (in the file system)
  • To start Firefox.exe running: -Each program gets its own area of RAM -The RAM area holds the program's code and data it manipulates -The instruction bytes are copied from storage to RAM -The CPU is directed to start running at the first instruction

    -Now the program is running!


How a computer program stored on disc is loaded into memory and starts to execute run

In the file system, a file like Firefox.exe just contains the bytes of the machine code instructions that make up the program (".exe" is a windows convention to mark a file as a program). Each machine code instruction takes up about 4 bytes, and whole program is just an enormous sequence of instructions.

When the user double clicks a program file to run it, essentially the block of bytes of the instructions for the program are copied into RAM, and then the CPU is directed to begin running at the first instruction in that area of RAM.

What Starts Firefox Running? The "Operating System"

  • What are the steps to run Firefox?
  • "Operating System"
    -e.g. Windows, Linux, Android, iOS
  • Operating System = Management
  • Set of supervisory programs that manage the computer
  • The operating system runs when the computer first starts up
  • Manage the start/stop of programs
  • Manage RAM
  • Manage persistent storage
  • Computers can run multiple programs at the same time
  • Operating system keeps track of the information for each program and shares resources (like RAM) among the programs

The "operating system" of a computer is like a first, supervisory program that begins running when the computer first starts up ("boots up"). The operating system plays an invisible administrative and bookkeeping role behind the scenes. When a laptop or phone starts up, the operating system typically gets things organized and then launches a "file explorer" program which displays available programs and menus etc. that show the user what is available, allowing the user to navigate and run programs.

The operating system keeps things organized in the background so that multiple programs can run at the same time, which is known as "multitasking". The operating system gives each program its own area of memory, so each program only accesses its own resources .. attempting to limit what an erroneous or malicious program can do. Keeping the programs separate is sometimes known as "sandboxing" .. mediating the access of each program so it operates independently, without interfering with other programs or the system as a whole. Similarly, each program has some access to the screen through a window, but this output area is separated from the output of other programs.

Recall that a .exe file or whatever is essentially just a file of machine code instructions. When you double-click the program, the operating system "launches" the program, doing the housekeeping steps of allocating an area of memory within RAM for the program, loading the first section of the program's machine code into that memory, and finally directing the CPU to start running that code.

The Whole Picture - Scenarios

Now we have the whole picture of a program running on the hardware. Look at common scenarios.

  • Demo: bring up "Activity Monitor" (Mac) "Task Manager" (windows) -See all the programs running -Do something costly in Firefox, see its CPU % spike

    -Kill a program


How a computer program stored on disc is loaded into memory and starts to execute run

1. Normal Running Programs

  • Operating system starts and stops programs
  • Each program has its own separate area in RAM: its instructions + data
  • CPU "round robin:" CPU runs a few instructions from each program
    so they all the programs appear to be running simultaneously
  • Persistent storage is organized as a file system, programs can read and write data here

2. Program Exits Normally

  • The program exits normally ("Quit" menu item)
  • The operating system stops running that program
  • The operating system reclaims the program's area of RAM (to be re-used)

3. Program Stuck/Infinite Loop - Abnormal Exit

  • Suppose program gets stuck running an infinite loop, is "stuck"
  • The operating system stops running that program - involuntary vs. normal-exit
  • The operating system reclaims the program's area of RAM

4. Running Out Of Memory

  • A program requests more RAM from the operating system
    e.g. to hold an image, but there's not enough RAM available
  • The operating system refuses the request, the program gives an error message

5. Memory Access Error

  • A program tries to access the memory of another program
  • Maybe because of a bug (common)
  • Maybe on purpose because it is malware
  • The operating system blocks the access (ideally)
  • Maybe kills the offending program too

6. Reboot

  • Why does this fix anything????
  • Sometimes some operating-system managed bytes in RAM is not quite right
    -a bug in the operating system, or perhaps a hardware error
  • A reboot wipes all the data from RAM
  • Starts up the operating system fresh
  • This might fix the problem
  • This should never be necessary
  • But sometimes it works!
  • The need for reboots is a hallmark of the presence of computers in previously reliable systems!

Why is it called Reboot?

  • Chicken and egg problem.. who runs the operating system?
  • Old phrase: "get over a fence by pulling on your own bootstraps"
  • When first powered on, computer runs a special "bootstrap" program
  • That program typically looks for persistent storage containing an operating system to run
  • Boot up - start
  • Reboot - do a fresh shutdown/startup cycle