Student Computer Simulation Barbara Ericson Georgia Tech Sept 2005 Georgia Institute of Technology Operating System - Organizer • Keep track of executing programs – Give them time with the CPU – A program gets a slice of time with the CPU • Keep track of memory – Decide when to move some data to disk (virtual memory) • Keep track of disk space – Decide where to store stuff • Interface with the user – Accept input via keyboard and mouse • Keep track of devices – USB drives, cameras, etc Georgia Institute of Technology Try it Out • Type ctrl-alt-delete (press all keys at the same time) – To bring up the task manager – Click on each of the tabs • To see the applications running • To see the processes that are running • To see how much memory is used Georgia Institute of Technology CPU – Calculator Plus More • Add, subtract, multiply, divide numbers • Compare numbers – Characters are encoded as numbers • Jump to a new instruction based on the result of a comparison • Can only work with one program at a time! – Keeps a program counter which is the address of the currently executing instruction Georgia Institute of Technology Memory – Whiteboard • Holds the currently executing programs and their data • Clears when the power is turned off • Much faster than disk • Has addresses for data based on bytes • Can get data from an address • Can put data into an address Georgia Institute of Technology Disk – Storage Facility • Stores data and programs • Doesn’t clear when the power is turned off • Much slower than memory • Much cheaper than memory Georgia Institute of Technology Program - Recipe • Set of instructions to the computer • Carried out by the CPU • Accomplishes some task Georgia Institute of Technology Computer Startup • The power is turned on – The Basic Input/Output System (BIOS) • Loads from a memory chip (ROM) and executes • Initializes the hardware (keyboard, disk drive, mouse, etc) – Then loads the operating system into memory and executes it • The Operating System waits for user input • The user starts a program – By double clicking on an icon or file – Or by click on Start->Program->Some Program Georgia Institute of Technology Program Execution • The operating system reads the program into memory – adds it to a list of programs that want to execute • It gives it a time slice of the CPU – adds it to a list of programs that are executing • It saves the current state of the CPU when it gives another program a time slice of the CPU – Context switch • It can restore the state of the CPU Georgia Institute of Technology Student Computer Simulation – Operating System – need 1 • • • • • Keep a list of programs to be run Keep a list of programs that are running Tell CPU to run a program (set program counter to memory address) Tell CPU to save the current state to a memory address Tell CPU to restore the current state from a memory address – Central Processing Unit – need 1 • • • • Keep track of the contents of Registers A, B, and C Keep track of the program counter Keep track of the currently executing instruction Be able to save current state to memory – Memory – need 1 • Have a list of values for addresses (on board) • Change the values at the addresses – Disk – need 1 • Hold the source code for programs and the byte code (compiled) versions Georgia Institute of Technology Simulation Set-Up • Give the person playing the disk the source code and 2 copies of the assembler code • Create an area on the whiteboard for memory to use • Create an area on the whiteboard for the CPU to use – Label registers A, B, C – Label the program counter • Create an area on the whiteboard for the operating system to use – Label programs that want to execute list – Label programs that are executing list with address in memory Georgia Institute of Technology Program 1 // how many pizza slices? int numPeople = 30; int numSlicesPerPerson = 2; int totalSlices = numPeople * numSlicesPerPerson; int numPizzas = totalSlices / 10; Georgia Institute of Technology Compiling • Computers can’t execute source code – They need to execute bytecodes • Instructions for the machine the code is running on – 1 might be add A and B and store the result in C • We compile Java source code Name.java into Name.class – bytecodes for a virtual machine • Not any particular machine • Then we execute Java class files – Runs a Java Virtual Machine to interpret the bytecodes – Or compile to native bytecodes using a just-in-time compiler Georgia Institute of Technology Assembler • Low level programming language – Names for instructions instead of numbers • ADD instead of 1 • LOADA instead of 20 • Use an assembler to convert to the bytecodes for a machine Georgia Institute of Technology Example Assembler LOADA mem - Load register A from memory address LOADB mem - Load register B from memory address CONB con - Load a constant value into register B SAVEB mem - Save register B to memory address SAVEC mem - Save register C to memory address ADD - Add A and B and store the result in C SUB - Subtract A and B and store the result in C MUL - Multiply A and B and store the result in C DIV - Divide A and B and store the result in C COM - Compare A and B and store the result in test JUMP addr - Jump to an address JEQ addr - Jump, if equal, to address JNEQ addr - Jump, if not equal, to address JG addr - Jump, if greater than, to address JGE addr - Jump, if greater than or equal, to address JL addr - Jump, if less than, to address JLE addr - Jump, if less than or equal, to address STOP - Stop execution Georgia Institute of Technology Program 1 Assembler 0 CONB 30 // put 30 in register B 1 SAVEB 128 // int numPeople = 30; 2 CONB 2 // put 2 in register B 3 SAVEB 132 // int numSlicesPerPerson = 2; 4 LOADA 128 // get value in address 128 5 MUL // multiply a and b and store result in c 6 SAVEC 136 // int totalSlices = numPeople * numSlicesPerPerson; 7 LOADA 136 8 CONB 10 9 DIV // divide a and b and store result in c 10 SAVEC 140 int numPizzas = totalSlices / 10; 11 STOP Georgia Institute of Technology Program 2 • • • • // calculate if we have enough material int width = 15; int length = 50; int total = width * height; Georgia Institute of Technology Program 2 Assembler 0 CONB 15 1 SAVEB 160 // (int width = 15) 2 CONB 50 3 SAVEB 164 // (int height = 50) 4 LOADA 160 // load width 5 LOADB 164 // load height 6 MUL 7 SAVEC 168 // (int total = width * height) 8 STOP Georgia Institute of Technology Simulation – Get Ready • Operating System: – Say, “Program 1 wants to execute so I add it to my list of programs that want to execute” • Add it to the list of programs to execute and put 0 for the address – Say, “Disk, please give a copy of the assembler version of program 1 to the memory at location 0” • The memory can tape the program at address 0 – Say, “CPU, please execute the program at address 0” • The CPU updates the program counter to 0 • The Operating System takes program 1 off the list of programs to execute and puts it on the list of executing programs with an address of 0 Georgia Institute of Technology Simulation – Start Program 1 • CPU: – Say, “Memory please tell me the first instruction at address 0” • Memory: – Read the first instruction of program 1 (assembler version) • CPU: – Write down the first instruction as the currently executing instruction and then do what it says • If the instruction is SAVEB ask Memory to please store the value at the memory address – Memory should write down the address and value • If the instruction is a LOAD ask Memory for the value at the address • After each instruction is finished update the program counter • Ask the Memory for the next instruction Georgia Institute of Technology Simulation – Context Switch • Operating System: – After the CPU executes 6 instructions from Program 1 interrupt • Say, “Program 2 wants to execute” • Ask the Disk for a copy of program 2 to be loaded into address 200 – Have the disk give a copy of the assembler version to memory – Memory can tape it by the address 200 • Add program 2 to the list of programs to execute – Ask the CPU to save state to address 400 • CPU asks memory to save A, B, C, and program counter to memory starting at 400 – Ask the CPU to set the program counter to 200 • Remove program 2 from the list of programs to execute • Add program 2 to the list of programs that are executing Georgia Institute of Technology Simulation – Start Program 2 • CPU: – Say, “Memory please tell me the first instruction at address 200” • Memory: – Read the first instruction of program 2 (assembler version) • CPU: – Write down the first instruction as the currently executing instruction and then do what it says • If the instruction is SAVEB ask Memory to please store the value at the memory address – Memory should write down the address and value • If the instruction is a LOAD ask Memory for the value at the address – Memory will tell you the value • After each instruction is finished update the program counter • Ask the Memory for the next instruction Georgia Institute of Technology Simulation – Context Switch • Operating System: – After the CPU executes 6 instructions from Program 2 interrupt – Ask the CPU to save state to address 600 • CPU asks memory to save A, B, C, and program counter to memory address 600 • CPU – Ask memory for values at address 400 • Reset A, B, C and program counter – Ask for next instruction based on program counter • Memory tells next instruction in Program 1 – Execute this instruction and update the program counter – When you reach STOP tell Operating System • Operating System – Remove program 1 from list of executing programs Georgia Institute of Technology Simulation – Context Switch • Operating System: – Ask CPU to restore state from address 600 • CPU – Ask memory for values at address 600 • Reset A, B, C and program counter – Ask for next instruction based on program counter • Memory tells next instruction in Program 2 – Execute this instruction and update the program counter – When you reach STOP tell Operating System • Operating System – Remove program 2 from list of executing programs Georgia Institute of Technology Summary • Programs must be in memory to execute – Data and programs are copied from disk • The CPU only has a few places to save values – So we save values to memory while we are executing • We can give each program a bit of time with the CPU – And then save the current state to memory – Later we can restore it from memory – This makes it look like it is executing several programs at once Georgia Institute of Technology