Upcoming SlideShare
×

# My cool new Slideshow!

223 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
223
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### My cool new Slideshow!

1. 1. Fundamentals and a Brief History of Computer Systems
2. 2. Q: What is a computer? A: A machine that manages information
3. 3. Representing Information <ul><li>Computers store and manipulates numbers </li></ul><ul><ul><li>Information needs to be encoded or represented </li></ul></ul><ul><ul><li>The numbers need to be interpreted </li></ul></ul><ul><li>The memory of any computer just looks like: </li></ul>010011010010011101001001001011010100100100100100100101111110000001
4. 4. Adding meaning to bits <ul><li>As programmers we add meaning to the bits by defining an interpretation </li></ul><ul><li>Handled by the programming language </li></ul><ul><ul><li>Types and variables </li></ul></ul><ul><li>Examples </li></ul>42 -> 00000000000000000000000000101010 3.14159 -> 01000000010010010000111111010000 “ unix” -> 01111000011010010110111001110101
5. 5. Von Neumann Model <ul><li>Programs are also represented using numbers </li></ul><ul><ul><li>Instructions and data are numbers stored in memory </li></ul></ul><ul><li>Typical sequence: </li></ul><ul><ul><li>Read a number (an instruction) </li></ul></ul><ul><ul><li>Decode it (using a predefined mapping) </li></ul></ul><ul><ul><li>Produce new number (data or instruction) </li></ul></ul>
6. 6. Memory <ul><li>Memory and devices are accessed using addresses </li></ul><ul><ul><li>Uniquely identifies a memory location </li></ul></ul><ul><li>An address is an index into the memory </li></ul><ul><ul><li>Also a number </li></ul></ul>10100100101101001001001001000100111111010010011101010100100100 Address 453365
7. 7. Computer Organization CPU Memory Devices
8. 8. Central Processing Unit (CPU) <ul><li>Driven by a synchronous clock </li></ul><ul><li>Clock Frequency </li></ul><ul><ul><li>3 GHz (nanoseconds) </li></ul></ul><ul><li>Billions of transistors </li></ul><ul><ul><li>220 mm 2 area </li></ul></ul><ul><ul><li>70 degrees C (158 F!) </li></ul></ul>
9. 9. Instruction Set Architecture (ISA) <ul><li>Instruction encoding </li></ul><ul><ul><li>Hardwired </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>x86 (Intel, AMD) </li></ul></ul><ul><ul><li>IA-64 (Intel, HP, SGI) </li></ul></ul><ul><ul><li>PowerPC (Freescale, IBM) </li></ul></ul><ul><ul><li>SPARC (Sun, Fujitsu) </li></ul></ul><ul><ul><li>MIPS (SGI) </li></ul></ul>
10. 10. Example 1 + 2 = 3
11. 11. Programming <ul><li>Store “1” and “2” into memory </li></ul><ul><ul><li>Remember the addresses of these locations </li></ul></ul><ul><li>Select an ADD instruction </li></ul><ul><ul><li>Encode the addresses of the numbers </li></ul></ul><ul><ul><li>Encode the address of the result </li></ul></ul><ul><li>Store the instruction in memory </li></ul>
12. 12. Running the program <ul><li>Load the address of the program into the program counter </li></ul><ul><li>Start the computer </li></ul>
13. 13. Execution <ul><li>Read the instruction from the program counter </li></ul><ul><li>Decode instruction </li></ul><ul><li>Fetch operands </li></ul><ul><li>Activate circuits that produce the sum of two numbers </li></ul><ul><li>Store the result in memory </li></ul><ul><li>Fetch next instruction </li></ul>
14. 14. Programming in the 60s
15. 15. Assembly Language <ul><li>First computer language </li></ul><ul><li>Assembles instructions from ASCII text </li></ul><ul><ul><li>ADD 23,address(454433) </li></ul></ul><ul><li>Made programs human-readable (sort of!) </li></ul><ul><li>An “assembler” creates the machine-language instructions from the code </li></ul><ul><ul><li>A program that outputs another program </li></ul></ul>
16. 16. Source Code <ul><li>Source code in ASCII </li></ul><ul><li>Transformed into machine instructions by a special program </li></ul><ul><ul><li>Called a compiler or interpreter </li></ul></ul><ul><li>Advantage </li></ul><ul><ul><li>Only the translator program needs to be rewritten for every new computer </li></ul></ul>
17. 17. Programming languages <ul><li>High level languages helps you handling addresses and instructions </li></ul><ul><ul><li>Declaring variables </li></ul></ul><ul><li>Control flow and operations </li></ul><ul><ul><li>+,-,*,/,&,||,… </li></ul></ul><ul><ul><li>loops, conditions, functions, objects ... </li></ul></ul><ul><li>Two major types </li></ul><ul><ul><li>Compiled </li></ul></ul><ul><ul><li>Interpreted </li></ul></ul>
18. 18. Programming Languages <ul><li>1954 - FORTRAN </li></ul><ul><li>1958 - LISP </li></ul><ul><li>1958 - ALGOL </li></ul><ul><li>1959 - COBOL </li></ul><ul><li>1962 - Simula </li></ul><ul><li>1964 - BASIC </li></ul><ul><li>1970 - Pascal </li></ul><ul><li>1972 - C </li></ul><ul><li>1972 - Smalltalk </li></ul><ul><li>1972 - Prolog </li></ul><ul><li>1973 - ML </li></ul><ul><li>1978 - SQL </li></ul><ul><li>1983 - Ada </li></ul><ul><li>1983 - C++ </li></ul><ul><li>1985 - Eiffel </li></ul><ul><li>1987 - Perl </li></ul><ul><li>1990 - Haskell </li></ul><ul><li>1990 - Python </li></ul><ul><li>1991 - Java </li></ul><ul><li>1993 - Ruby </li></ul><ul><li>1995 - PHP </li></ul><ul><li>2000 - C# </li></ul>
19. 19. Variables <ul><li>A variable is a programming language abstraction to help you manage addresses and values </li></ul><ul><li>Assigns a name or symbol to an address </li></ul><ul><li>Hence, every variable has a name, address (storage) and a value </li></ul><ul><ul><li>Variables in algebra do not have notion of storage </li></ul></ul><ul><ul><li>Storage is provided automatically by the programming language </li></ul></ul>
20. 20. Types <ul><li>A variable is declared using a type </li></ul><ul><ul><li>Determines representation </li></ul></ul><ul><ul><li>Sets the context </li></ul></ul><ul><li>Makes sure that operations on variables are closed in the algebraic sense </li></ul><ul><ul><li>Results are of the same type as the operands </li></ul></ul><ul><ul><li>No loss of information/precision </li></ul></ul>
21. 21. Interpreted Languages <ul><li>The code is executed by an interpreter program </li></ul><ul><ul><li>Transforms source code into machine code on the fly </li></ul></ul><ul><li>The program can be stopped at any given statement </li></ul><ul><ul><li>Interactivity </li></ul></ul><ul><li>Example languages </li></ul><ul><ul><li>Matlab </li></ul></ul><ul><ul><li>Python </li></ul></ul><ul><ul><li>Perl </li></ul></ul>
22. 22. Compiled Languages <ul><li>A compiler program translates the entire source code into some ISA </li></ul><ul><ul><li>Result is often called a binary program or executable </li></ul></ul><ul><li>Allows for optimization </li></ul><ul><li>Limited feedback on runtime errors compared to interpreted languages </li></ul><ul><li>Examples </li></ul><ul><ul><li>C/C++ </li></ul></ul><ul><ul><li>Fortran </li></ul></ul>
23. 23. Source code <ul><li>Text files describing the program, i.e., the code to be compiled or interpreted </li></ul><ul><li>The code is defined by a language grammar </li></ul><ul><ul><li>Set of rules and keywords that define the language </li></ul></ul><ul><ul><li>Grammar is checked by the compiler or interpreter </li></ul></ul><ul><ul><li>Code is not automatically correct just because it compiles! </li></ul></ul><ul><li>Code can be automatically generated or pasted together from several different sources </li></ul><ul><ul><li>Preprocessing </li></ul></ul>
24. 24. Compilation <ul><li>The compiler typically generates </li></ul><ul><ul><li>Source code </li></ul></ul><ul><ul><li>Assembly Language code (.s) </li></ul></ul><ul><ul><li>Object Code (.o or .obj) </li></ul></ul><ul><ul><li>Executables (a.out or .exe) </li></ul></ul><ul><li>Object files are an intermediate program form where the names (symbols) of variables and functions are present </li></ul><ul><li>Several objects can be linked to form a complete program </li></ul><ul><ul><li>Can be compiled from different languages </li></ul></ul>
25. 25. Compilation, Example <ul><li>Compile program.c into object file (no linking): </li></ul><ul><ul><li>\$ gcc -c program.c </li></ul></ul><ul><li>Compile and link program.c to executable file “program” </li></ul><ul><ul><li>\$ gcc -o program program.c </li></ul></ul>
26. 26. Computers as resources <ul><li>Computers were (and still are) an expensive and powerful resource </li></ul><ul><ul><li>In the early days, people where scheduled physically to the computer </li></ul></ul><ul><ul><li>Now, programs are scheduled automatically </li></ul></ul><ul><li>Sharing the resource </li></ul><ul><ul><li>Processors </li></ul></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>Devices </li></ul></ul>
27. 27. Operating System (OS) <ul><li>Manages the computer </li></ul><ul><ul><li>Allocation and assignment of resources </li></ul></ul><ul><ul><li>Scheduling (priorities) </li></ul></ul><ul><ul><li>Monitoring (interfaces) </li></ul></ul><ul><li>Multiple programs, multiple users </li></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Protection </li></ul></ul><ul><ul><li>Scalability </li></ul></ul>
28. 28. Q: How do we actually share the resources? A: Virtualization
29. 29. Virtualization <ul><li>A device, CPU or even the memory has an interface that can be virtualized </li></ul><ul><li>Creates a virtual computer managed by the OS </li></ul><ul><ul><li>Virtual memory </li></ul></ul><ul><ul><li>Multi-tasking </li></ul></ul>
30. 30. Multitasking <ul><li>A process is a program in execution </li></ul><ul><ul><li>Contains the execution context </li></ul></ul><ul><ul><li>Acts like a bubble where the program lives </li></ul></ul><ul><li>The instruction streams are sliced into smaller quanta and scheduled onto the CPUs </li></ul><ul><ul><li>Time-sharing </li></ul></ul><ul><ul><li>Preemptive multitasking </li></ul></ul>
31. 31. Context switches <ul><li>On a single-CPU system there is only one program in execution </li></ul><ul><ul><li>Only one process scheduled for execution </li></ul></ul><ul><li>At a given rate or a runtime exception a new process is scheduled </li></ul><ul><ul><li>Context switch </li></ul></ul><ul><li>Creates an illusion of parallel execution if context switch rate is sufficiently high </li></ul>
32. 32. Logical Control Flows Time Process A Process B Process C Each process has its own logical control flow
33. 33. Concurrent Processes <ul><li>Two processes run concurrently ( are concurrent) if their flows overlap in time. </li></ul><ul><ul><li>“Not exactly parallel” </li></ul></ul><ul><li>Otherwise, they are sequential. </li></ul>
34. 34. Example of concurrency <ul><li>Examples: </li></ul><ul><ul><li>Concurrent: A & B, A & C </li></ul></ul><ul><ul><li>Sequential: B & C </li></ul></ul>Time Process A Process B Process C
35. 35. User View of Concurrent Processes <ul><li>Control flows for concurrent processes are physically disjoint in time. </li></ul><ul><li>However, we can think of concurrent processes are running in parallel with each other. </li></ul>Time Process A Process B Process C
36. 36. Virtual Memory <ul><li>Memory is virtualized by dividing the available memory into pages </li></ul><ul><li>Each processes has its own set of pages </li></ul><ul><li>Total amount of pages is much larger than the available physical memory </li></ul><ul><ul><li>The contents is backed-up by either physical memory or disk </li></ul></ul><ul><li>Pages are swapped in and out to physical memory at context switches </li></ul>
37. 37. Virtual Address Space <ul><li>Due to virtualization two programs can access memory at the same address </li></ul><ul><li>The OS provides an address translation mechanism to map the virtual addresses to physical </li></ul><ul><ul><li>Totally transparent for the programmer </li></ul></ul><ul><li>Memory is categorized as resident if it is backed-up by physical memory </li></ul>
38. 38. On Demand Paging Memory 0: 1: P-1: Page Table Disk Virtual Addresses Physical Addresses CPU 0: 1: N-1:
39. 39. The Kernel <ul><li>The virtualization is handled by the OS kernel </li></ul><ul><li>Processors execute in either user or kernel mode </li></ul><ul><ul><li>I/O can only be initiated from kernel mode </li></ul></ul><ul><ul><li>Separates users from each other </li></ul></ul><ul><li>Part of the address space is reserved for kernel memory </li></ul>
40. 40. System Calls <ul><li>User can indirectly access the kernel using system calls </li></ul><ul><li>The system calls provides a service to the user though this interface </li></ul><ul><li>In many cases this interface is enhanced by user-level libraries that use system calls </li></ul>
41. 41. Sharing Memory <ul><li>As the memory is virtualized processes can share pages </li></ul><ul><li>Especially useful for read-only data like programs </li></ul><ul><li>Instead of loading multiple copies of the “firefox” program, one instance of the code is shared </li></ul><ul><ul><li>Faster loading </li></ul></ul><ul><ul><li>Less memory </li></ul></ul>
42. 42. Running a program <ul><li>After booting the OS starts some kind of user interface </li></ul><ul><ul><li>GUIs (Windows) </li></ul></ul><ul><ul><li>Command Line Interpreters (Prompts) </li></ul></ul><ul><li>A shell is created to host new programs </li></ul><ul><ul><li>Program cocoon </li></ul></ul><ul><li>The program loader copies the new program into the shell process </li></ul>
43. 43. Virtual Address Space in Linux kernel virtual memory (code, data, heap, stack) memory-mapped region for shared libraries run-time heap (managed by malloc) user stack (created at runtime) unused memory invisible to user code 0xc0000000 0x08048000 0x40000000 read/write segment (.data, .bss) read-only segment (.init, .text, .rodata) loaded from the executable file 0xffffffff Virtual Address in hexadecimal, base 16
44. 44. Some Important System Calls <ul><li>fork() </li></ul><ul><ul><li>Creates a child process which is a copy of the parent </li></ul></ul><ul><li>execve() </li></ul><ul><ul><li>Loads a new program and overwrites the current one </li></ul></ul><ul><li>mmap() </li></ul><ul><ul><li>Maps a device or a new page of memory into the virtual address space </li></ul></ul>
45. 45. Some Useful Commands <ul><li>ps </li></ul><ul><ul><li>Lists all processes </li></ul></ul><ul><li>top </li></ul><ul><ul><li>Prints resource usage </li></ul></ul><ul><li>strace </li></ul><ul><ul><li>Traces system calls. Great for reverse engineering! </li></ul></ul><ul><li>/proc </li></ul><ul><ul><li>File system for kernel data structures </li></ul></ul>
46. 46. Linux Demo <ul><li>Accounts being created for you on ICME cluster at calving.stanford.edu </li></ul><ul><li>Username is SUNet ID, you will be given an initial password (change right away!) </li></ul><ul><li>To connect, go to vpn.stanford.edu and download VPN client, then connect Stanford VPN with SUNet ID/password </li></ul><ul><li>Will demonstrate now </li></ul>
47. 47. Grading Criteria <ul><li>Correctness and robustness (by far most important!) </li></ul><ul><ul><li>Follow the specification! </li></ul></ul><ul><ul><li>Recover gracefully from errors </li></ul></ul><ul><li>Clarity and readability </li></ul><ul><li>Modularity and maintainability </li></ul><ul><ul><li>Is code easy to re-use or enhance? </li></ul></ul><ul><li>Portability </li></ul><ul><ul><li>Avoid using nonstandard functionality </li></ul></ul><ul><li>Performance </li></ul>
48. 48. Discussion Section <ul><li>Room reserved: </li></ul><ul><ul><li>Where: Bldg 320, Rm 220 (Main Quad, Geocorner) </li></ul></ul><ul><ul><li>When: Thursdays, 4:15pm to 6:05pm </li></ul></ul><ul><li>Actual section time: 4:15-5:30pm </li></ul><ul><li>First section held: TODAY! (Jan 10) </li></ul>