Intro to Computer Systems Summer 2014 COMP 2130 Introduction to Computer Systems Computing Science...
Transcript of Intro to Computer Systems Summer 2014 COMP 2130 Introduction to Computer Systems Computing Science...
Intro to Computer Systems
Summer 2014
COMP 2130 Introduction to Computer Systems
Computing ScienceThompson Rivers University
TRU-COMP2130 Intro to Computer Systems 2
Course Objectives
The better knowledge of computer systems, the better programing.Computer System C Programming Language
Computer architectureCPU (Central Processing Unit)IA32 assembly language
Introduction to C language
Compiling, linking, loading, executing
Physical main memoryMMU (Memory Management Unit)
Virtual memory space
Memory hierarchyCache
Dynamic memory management
Better coding – locality
Reliable and efficient programming for power programmers(to avoid strange errors, to optimize codes, to avoid security holes, …)
TRU-COMP2130 Intro to Computer Systems 3
Course Contents
Introduction to computer systems: B&O 1, 9.1 – 9.2 Introduction to C programming: K&R 1 – 4 Data representations: B&O 2.1 – 2.4 C: advanced topics: K&R 5.1 – 5.9, 5.11, 6 – 8 Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 Compiling, linking, loading, and executing: B&O 7 (except 7.12) Dynamic memory management – Heap: B&O 9.9.1 – 9.9.2, 9.9.4 –
9.9.5, 9.11 Code optimization: B&O 5.1 – 5.6, 5.13 Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 –
6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5
TRU-COMP2130 Intro to Computer Systems 4
Unit Learning Objectives
List the four phases in the compilation system. List the four major hardware components. List the three major components in CPU. Explain the logical concepts of main memory. List the two steps in an instruction cycle. Explain briefly for what and how PC is used. Explain briefly what locality means. Give an example code that can be executed fast by using cache
memory. Define what a process is. Explain how process and thread are different. Define what multi-processing is. Distinguish virtual memory and physical memory.
TRU-COMP2130 Intro to Computer Systems 5
Explain what heap in the virtual memory space is. Explain what stack in the virtual memory space is. Name the hardware component that translates virtual addresses to
physical addresses. …
TRU-COMP2130 Intro to Computer Systems 6
Unit Contents
What is information? Programs and compilation systems CPU Cache Memory hierarchy Operating systems Communications
TRU-COMP2130 Intro to Computer Systems 7
Introduction
A computer system consists of1. H…
2. S…
that work together to run … Hardware affects the CORRECTNESS and PERFORMANCE of
programs.
TRU-COMP2130 Intro to Computer Systems 8
1. What is Information?
A source program (or source file) – a sequence of bits?#include <stdio.h>
int main()
{
printf(“Hello, world\n”);
}
Bits ??? Bytes ??? ASCII code ???
For each character, a unique byte-sized integer is assigned. Text file ??? Binary file ??? But files are files for operating systems.
TRU-COMP2130 Intro to Computer Systems 9
Where are files (or data) stored in a computer system? How is information different from data? The only thing that distinguishes different data objects is the context
in which we view. A data object can be interpreted in different ways. E.g., the same sequence of bytes might represent
integer, floating-point number (???), character string, machine instruction, …
As programmers, we need to understand machine representations of numbers. Details in B&O 2.
TRU-COMP2130 Intro to Computer Systems 10
2. Programs and Compilation Systems
Can a computer system understand a high-level C program? A high-level C program translated to low-level machine-language
instructions packaged in a form called executable object program and stored as a binary file. Object programs are also referred as object files.
How to translate? Compilers
gcc (or cc)
TRU-COMP2130 Intro to Computer Systems 11
The compilation system
Preprocessing phase #include <stdio.h> #define PI 3.141593
Compilation phase Assembly phase Linking phase
hello is ready to be loaded into main memory and executed. Being loaded into main memory Being executed
Pre-processor(cpp)
hello.i Compiler(cc1)
hello.s Assembler(as)
hello.o Linker(ld)
hellohello.c
Sourceprogram(text)
Modifiedsourceprogram(text)
Assemblyprogram(text)
Relocatableobjectprograms(binary)
Executableobjectprogram(binary)
printf.o
TRU-COMP2130 Intro to Computer Systems 12
Compilation systems try to produce correct and efficient machine codes, but lists the errors which it could not understand
Reasons to understand how the compilation systems work Optimizing program performance
Not easy for them to optimize source codes Understanding link-time errors
Especially in the development of a large software system Avoiding security holes
Run-time errors
TRU-COMP2130 Intro to Computer Systems 13
3. CPU
CPU stands for Central Processing Unit Let’s discuss how CPU is used to run programs. Shell, a special application program called a command-line
interpreter, accepts commands from the user and execute them. What happens to hello program when we run?
Hardware Organization of a System
What happens to hello program when we run? Need to understand the hardware organization that consists of
4 major components: CPU, main memory, buses, i/o devices
Mainmemory
I/O bridge
Bus interface
ALU
RegistersCPU
System bus Memory bus
Disk controller
Graphicsadapter
USBcontroller
MouseKeyboard Display
Disk
I/O bus Expansion slots forother devices suchas network adapters
hello executable stored on disk
PC
Buses Carry fixed-size bytes known as words 4 bytes (32bits) or 8 bytes (64bits)
I/O devices Main memory
DRAM Logically a linear array of addressable bytes, each with its own unique
address starting at zero CPU (…), or simply processor
Register files, or simply registers A register is a word-sized storage device.
PC (Program Counter) A special register pointing at (contains the address of) some machine-language
instruction stored in main memory; increased after the fetch cycle ALU (???)
Compute new data and address values
Only one instruction in main memory, which is pointed by PC, is read (called fetched) by CPU and executed, not multiple instructions. This is called instruction cycle.
PC is increased after the instruction fetch so that the next instruction is pointed by PC and read by CPU.
Hence programs have to be loaded into main memory to be executed. How to start after the program is loaded into memory?
PC must be updated to the address of the first instruction in the program.
PC is updated.
TRU-COMP2130 Intro to Computer Systems 17
Running the hello Program
What happens to hello program when we run?
TRU-COMP2130 Intro to Computer Systems 18
Mainmemory
I/O bridge
Bus interface
ALU
Registers
CPU
System bus Memory bus
Disk controller
Graphicsadapter
USBcontroller
MouseKeyboard Display
Disk
I/O bus Expansion slots forother devices suchas network adapters
PC
“./hello"
Usertypes
“./hello"
Shell is reading “.\hello” using the services providedby OS.
TRU-COMP2130 Intro to Computer Systems 19
Mainmemory
I/O bridge
Bus interface
ALU
Registers
CPU
System bus Memory bus
Disk controller
Graphicsadapter
USBcontroller
MouseKeyboard Display
Disk
I/O bus Expansion slots forother devices suchas network adapters
hello executable stored on disk
PC
hello code
"hello,world\n"
Shell is loading “.\hello”using the services providedby OS.
TRU-COMP2130 Intro to Computer Systems 20
Mainmemory
I/O bridge
Bus interface
ALU
Registers
CPU
System bus Memory bus
Disk controller
Graphicsadapter
USBcontroller
MouseKeyboard Display
Disk
I/O bus Expansion slots forother devices suchas network adapters
hello executable stored on disk
PC
hello code
"hello,world\n"
"hello,world\n"
“.\hello” is now running.Shell then?
TRU-COMP2130 Intro to Computer Systems 21
4. Cache
From the previous example, we can see that a system spends a lot of time moving information from one place to another.
It might take CPU 10 M times longer to read a word from disk than from main memory. This is why hello is loaded into memory before the file is executed by CPU.
How is hello executed? hello consists of machine instructions. Only one instruction pointed by PC is read (called fetched) by CPU and
executed, not multiple instructions. This is called instruction cycle. Fetch time is much longer than execution time.
PC is updated.
TRU-COMP2130 Intro to Computer Systems 22
Fetch time is much longer than execution time. Any good idea to solve this processor-memory gap?
Hint: The processor can read data from registers almost 100 times faster than from memory.
TRU-COMP2130 Intro to Computer Systems 23
Smaller faster and closely located storage device called cache memory, or simply cache
Multiple levels could be possible: E.g., L1, L2, L3 How is cache helpful? Programs will be loaded into cache, not memory?
Initially programs will be loaded into main memory. Locality – the tendency for programs to access frequently data and code
in localized regions; any example code? Important for code optimization
Mainmemory
I/Obridge
Bus interface
ALU
RegistersCPU chip
System busMemory bus
Cache memories
for (i=0; i<100; i++) sum += i;
TRU-COMP2130 Intro to Computer Systems 24
5. Memory Hierarchy
Regs
L1 cache
Main memory(DRAM)
Local secondary storage(local disks)
Larger, slower,
and cheaper (per byte)storagedevices
Remote secondary storage(distributed file systems, Web servers)
Local disks hold files retrieved from disks on remote network servers.
Main memory holds disk blocks retrieved from local disks.
L2 cache (SRAM)
L1 cache holds cache lines retrieved from the L2 cache.
CPU registers hold words retrieved from cache memory.
L2 cache holds cache linesretrieved from L3 cache
L0:
L1:
L2:
L3:
L4:
L5:
Smaller,faster,and
costlier(per byte)storage devices
L3 cache (SRAM) L3 cache holds cache
linesretrieved from memory.
L6:
25
6. Operating Systems
When the shell loaded and ran the hello program, and when the hello program printed its message, neither program actually accessed the keyboard, display, disk, or main memory directly.
Then how? Services including I/O are provided by the operating system.
Application programs
Processor Main memory I/O devices
Operating systemSoftware
Hardware
Processor Main memory I/O devices
Processes
Files
Virtual memory
TRU-COMP2130 Intro to Computer Systems 26
Processes
A process is the operating system’s abstract for a running program. Multiple processes can run on the system? (We assume one CPU core.)
Logically yes. In the previous example, shell and hello programs. But this is not real parallel processing. This is called multi-processing. The instructions of one process are interleaved with the instructions of
another process, using context switching. (all the register values + …)
Process A Process B
User code
Kernel code
User code
Kernel code
User code
Time
Context switch
Context switch
read
Disk interrupt
Return from read
TRU-COMP2130 Intro to Computer Systems 27
Threads
A process can consists of multiple execution units, called threads or light-weight processes, each running in the context of the process and sharing the same code and global variables, but different local variables.
Very important programming model. Any good example application?
TRU-COMP2130 Intro to Computer Systems 28
Virtual Memory
A program includes instructions and data, and they will be loaded into memory. The addresses of instruction and data are used in the program. E.g., x = y + 10;
Multiple processes should share the fixed-size memory. When a program is loaded next time, the location of the program in memory will be probably different.
How to assign memory space to each process? How to let a process (i.e., machine codes) know its location in
memory? If processes have to know their location in memory to access
variables, programming would become a very difficult job. Do you think in which area in memory you would put variables? How to solve this problem?
Virtual (oac logical) address space and physical address space
TRU-COMP2130 29
Virtual memory is an abstraction that provides each process with the illusion that it has exclusive use of main memory.
Each process has the same uniform view of memory, which is known as its virtual address space.
Now compilers can compile programs to machine-level instructions. (All instructions in programs use virtual addresses.)
Kernel virtual memory
Memory mapped region forshared libraries
Run-time heap(created by malloc)
User stack(created at runtime)
0
Memoryinvisible touser code
Read/write data
Read-only code and data
Loaded from the hello executablefile
printf function
0x08048000 (32)0x00400000 (64)
Intro to Computer Systems
TRU-COMP2130 Intro to Computer Systems 30
Program code and data Machine instructions Global variables
Heap Dynamic memory allocation – malloc(), free(), …
Shared libraries Shared by multiple process – e.g., C standard library, the math library, …
Stack To allocate memory for parameters and local variables as functions are
called Kernel virtual memory
The part of the operating system Always resident in memory
TRU-COMP2130 Intro to Computer Systems 31
Multiple processes have program code and data within the same virtual address space.
But they are loaded in different locations in the physical memory.
TRU-COMP2130 Intro to Computer Systems 32
Multiple processes have program code and data within the same virtual address space.
But they are loaded in different locations in the physical memory. Here is a very critical problem. The program code, i.e., machine
instructions, uses virtual addresses in the virtual address space, and the actual code and data are loaded in different physical addresses. When an instruction is fetched and executed by CPU, virtual addresses are used. E.g., pointing to a variable uses the virtual address of the variable, which is different from the physical address of the variable in memory. CPU would access wrong location in the physical memory.
How to solve? MMU (Memory Management Unit) – hardware component to translate
virtual addresses to physical addresses
TRU-COMP2130 Intro to Computer Systems 33
Example
TRU-COMP2130 Intro to Computer Systems 34
7. Communications
Mainmemory
I/O bridge
Bus interface
ALU
Registers
CPU chip
System bus Memory bus
Disk controller
Graphicsadapter
USBcontroller
Mouse Keyboard Monitor
Disk
I/O bus
Expansion slots
Networkadapter
Network
PC
TRU-COMP2130 Intro to Computer Systems 35
Localtelnetclient
Remotetelnetserver
2. Client sends "hello"string to telnet server 3. Server sends "hello"
string to the shell, which runs the hello program,
and passes the outputto the telnet server4. Telnet server sends
"hello, world\n" stringto client
5. Client prints"hello, world\n"
string on display
1. User types"hello" at the
keyboard