Title: 20-755: The Internet Lecture 2: Computer Systems I
120-755 The InternetLecture 2 Computer Systems I
- David OHallaron
- School of Computer Science and
- Department of Electrical and Computer Engineering
- Carnegie Mellon University
- Institute for eCommerce, Summer 1999
2Todays lecture
- Administrative issues (10 minutes)
- Data (50 min)
- Break (10 min)
- Programs (40 min)
3Systems
- A system is a collection of interworking parts.
- Examples
- the human body
- the economy
- a car
- a stereo
- a computer
- Systems are often extremely complicated.
- How do we understand complex systems?
- Abstraction is the key.
4Abstraction
- Example a lighting system
light bulb
switch
A
A
B
B
5Abstraction
- Heres one way to understand how our lighting
system works - Closing the switch induces a voltage drop
between A and B, which causes current to flow
through the light bulb, which heats up the
filament, which causes the filament to emit
light. Opening the switch eliminates the voltage
differential, which stops the current, which
causes the filament to cool and stop emitting
light.
6Abstraction
- Heres another way to understand our lighting
system - Close the switch and light turns on. Open the
switch and the light turns off - This is an example of an abstraction, where we
describe the behavior of a system in terms of its
inputs (the position of the switch) and its
outputs (whether it is emitting light or not).
7Abstraction
- Abstraction is one of the most powerful weapons
in the arsenal of computer science. - Useful because it hides complexity.
- High-level languages like C, Java, and Perl
provide abstractions for low-level machine
instructions. - Operating systems provide abstractions for
resources such as the CPU, memory, and I/O
devices. - TCP/IP provides an abstraction for collections of
interconnected heterogeneous networks. - However, abstractions are most useful if we
understand something about how things work.
8Typical computer system
Keyboard
Mouse
Printer
Modem
Processor
Interrupt controller
Serial port controller
Parallel port controller
Keyboard controller
Local/IO Bus
Network adapter
Video adapter
Memory
IDE disk controller
SCSI controller
SCSI bus
disk
Network
Display
disk
cdrom
9Bits
- All computer data (input, output, memory, and
even programs) are collections of 1s and 0s
called bits (binary digits). - 0 and 1 are abstractions for voltage levels.
- easy to store with bistable elements and can be
reliably transmitted on noisy and innacurate
wires.
0
1
0
3.3V
2.8V
0.5V
0.0V
10Powers of 10
- Def 10p 10 x 10 x ... x 10 (there are p 10s)
- ten to the power p or the pth power of 10
- Examples of powers of 10
- 100 1
- 101 10
- 102 10 x 10 100
- 103 10 x 10 x 10 1,000
11Decimal (base 10) numbers
- 10 digits to choose from 0, 1, ... , 8, 9.
- Each digit corresponds to a different power of
10. - Examples
- 345 (3 x 102) (4 x 101) (5 x 100)
- 300 40 5
- 132 (1 x 102) (3 x 101) (2 x 100)
- 100 30 2
12Powers of 2
- Def 2p 2 x 2 x ... x 2 (there are p 2s)
- two to the power p or the pth power of two
- Examples of powers of 2
- 20 1
- 21 2
- 22 2 x 2 4
- 23 2 x 2 x 2 8
- 24 2 x 2 x 2 x 2 16
13Binary (base 2) numbers
- 2 digits to choose from 0, 1.
- Each digit corresponds to a different power of 2.
- Examples (converting from binary to decimal)
- 1012 (1 x 22) (0 x 21) (1 x 20)
- 4 0 1
- 5
- 0112 (0 x 22) (1 x 21) (1 x 20)
- 0 2 1
- 3
14Converting from decimal to binary
- converting decimal 5 to binary
- 5 / 2 2 rem 1 first (righmost) binary
digit is 1 - 2 / 2 1 rem 0 second binary digit is 0
- 1 / 2 0 rem 1 third binary digit is 1
- So 5 1012
- converting decimal 3 to binary
- 3 / 2 1 rem 1 first (righmost) binary
digit is 1 - 1 / 2 0 rem 1 second binary digit is 1
- So 3 112 0112
15Counting in binary
- With n bits, you can represent 2n numbers 0,
1, ... , 2n - 1. - Example 1 bit (21 2 numbers)
- 02 (0)
- 12 (1)
- Example 2 bits (22 4 numbers)
- 002 (0)
- 012 (1)
- 102 (2)
- 112 (3)
16Counting in binary (cont.)
- Example 3 bits (23 8 numbers)
- 0002 (0)
- 0012 (1)
- 0102 (2)
- 0112 (3)
- 1002 (4)
- 1012 (5)
- 1102 (6)
- 1112 (7)
17Practice problems
- Powers of two
- (a) 20
- (b) 21
- (c) 25
Binary to decimal (a) 00102 (b) 01112
(c) 10012 (d) 11112
Decimal to binary (a) 8 (b) 9 (c) 12
(d) 14
18Practice problems Counting in binary
- (a) how many numbers can you represent using 4
bits? - (b) What is the largest number?
- (c) write them out in binary, starting from 00002
19Twos complement representation of signed integers
sign bit 0 positive 1 negative
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
1010 1011 1100 1101 1110 1111
(0) (1) (2) (3) (4) (5) (6) (7) (-8) (-7) (-6) (-5
) (-4) (-3) (-2) (-1)
positive integers
negative integers
20Interpreting twos complement numbers
- To negate a twos complement number
- invert the bits (I.e., change each 0 to 1 and
each 1 to 0). - add 1 to the result.
- Examples
- To negate 0 00002 11112 1 00002
- To negate -2 11102 00012 1 00102
- You can use this property to determine the twos
complement representation of a negative number. - Example -5 inv(0101) 1 1010 1 1011,
where inv(x) inverts the bits of x. - Example -12 ?
21Hex (base-16) representation of binary numbers
- Hex is a compact way to represent binary numbers.
Each hex digit represents a byte (8 bits) of
data - 011010102 6a16
- 10111110111011112 beef16
- In Perl, use the 0x prefix to denote a hex
number - x 0x6a
- y 0xbeef
22Characters
- ASCII characters are represented in 8-bit chunks
called bytes. - Two types of characters
- printable characters characters that can be
typed on a keyboard (e.g., d, h) - unprintable control characters (e.g., BEL,BS)
23ASCII character set
hex
char
esc
00 NUL '\0' 01 SOH 02 STX
03 ETX 04 EOT
05 ENQ 06 ACK
07 BEL '\a' 08 BS '\b' 09
HT '\t' 0A LF '\n' 0B VT
'\v' 0C FF '\f' 0D CR '\r'
0E SO 0F SI
10 DLE 11 DC1 12
DC2 13 DC3 14 DC4
15 NAK 16 SYN 17 ETB
18 CAN 19 EM
1A SUB 1B ESC 1C
FS 1D GS 1E RS
1F US 20 SPACE
21 ! 22 "
23 24 25
26 27 28 (
29 ) 2A
2B 2C , 2D
- 2E . 2F /
30 0 31 1 32
2 33 3
34 4 35 5 36
6 37 7 38 8
39 9 3A
3B 3C lt
3D 3E gt 3F
? 40 _at_ 41 A
42 B 43 C
44 D 45 E
46 F 47 G 48 H
49 I 4A J 4B K 4C L
4D M
4E N 4F O 50
P 51 Q 52 R
53 S 54 T
55 U 56 V
57 W 58 X 59
Y 5A Z 5B
5C \ '\\' 5D
5E 5F _
60 ' 61 a 62
b 63 c 64 d 65 e
66 f 67 g
68 h 69 i 6A
j 6B k 6C l
6D m 6E n
6F o 70 p
71 q 72 r 73
s 74 t 75 u
76 v 77 w
78 x 79 y
7A z 7B 7C
7D 7E
7F DEL
24Computer memory
Keyboard
Mouse
Printer
Modem
Processor
Interrupt controller
Serial port controller
Parallel port controller
Keyboard controller
Local/IO Bus
Network adapter
Video adapter
Memory
IDE disk controller
SCSI controller
SCSI bus
disk
Network
Display
disk
cdrom
25Metrics for space and time
- Space
- Kilobyte (KB) 210
- approx. 102 , a thousand
- Megabyte (MB) 220
- approx. 106, a million
- Gigabyte (GB) 230
- approx. 109, a billion
- Terabyte (TB) 240
- approx. 1012, a trillion
- Time
- millisecond (ms) 10-3 s
- .001 s
- a thousandth of a sec
- microsecond (us) 10-6 s
- .000001 s
- a millionth of a second
- nanosecond (ns) 10-9 s
- .000000001 s
- a billionth of a second
- 1 ns is the time it takes light to travel about
12 in!
26Computer memory
Bytes
Addr
0000
0001
0002
- Organized as a sequential array of bytes.
- Each byte has an integer address (location).
- Addresses start at 0.
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
27Words
Words
Bytes
Addr
- Machine has word size
- nominal size of numbers, including addresses.
- Numbers, instructions, and addresses typically
fractions or multiples of the word size. - Words are addressed by first byte.
- PC-class machines are 32 bits
- Limits addresses to 4 GB.
- Becoming too small!
- Newer server-class machines are 64 bits (e.g. DEC
Alpha) - Limits addresses to 4 TB.
- In 20 years, well be complaining about this too!
0000
0001
0002
Addr 0000
0003
0004
0005
0006
0007
0008
Addr 0008
0009
0010
0011
0012
Addr 0012
0013
0014
0015
28Strings
Bytes
Addr
- A string is represented in memory as a sequence
of bytes terminated by 0x00 (\0). - Known as a null-terminated string
- Example (Perl)
- x Hi Dave!\n
0000
0001
0002
0003
H
0004
I
0005
SPACE
0006
D
0007
a
0008
v
0009
e
0010
\0
0011
0012
0013
0014
0015
29Data Representation Summary
- Key concepts
- Its all just bits!
- everything in a computer is represented as a
collection of bits that are interpreted in
different ways. - memory is organized as a sequence of bytes
- each byte in memory has its own address.
- each machine has nominal word size
- numbers are fractions or multiples of words
- the address of a word is the address of its first
byte - floating point representation is used for
non-integral numbers - e.g., 3.14159
- too complex for us to study in this course.
30Break time!(10 mins)
31Todays lecture
- Administrative issues (10 minutes)
- Data (50 min)
- Break (10 min)
- Programs (40 min)
32Programs
Keyboard
Mouse
Printer
Modem
Processor
Interrupt controller
Serial port controller
Parallel port controller
Keyboard controller
Local/IO Bus
Network adapter
Video adapter
Memory
IDE disk controller
SCSI controller
SCSI bus
disk
Network
Display
disk
cdrom
33Processor components
Processor
Addresses
P C
ALU
Register File
Data
Instructions
- PC (Program Counter)
- Contains address of the next instruction
- ALU (Arithmetic/Logic Unit)
- addition, subtraction, etc.
- Register File
- Small fast internal memory for heavily used
program data, typically 16, 32, or 64 locations
(called registers), each of which holds 1 word. - Memory
- Contains both object code and data
Register name
Register file
R0
R1
...
R2
R30
R31
34Object code
- Processor simply executes one machine language
instruction after another until you unplug it. - A program (or code) is a related set of these
instructions. - Key idea Programs are stored in memory as simply
another kind of data - variously called object code, machine code,
object program. - Example
- on the DEC Alpha, the word 0x42110401 is the bit
pattern for an instruction that adds the contents
of two registers and stores the result in a third
register.
35Assembly Language
- machine language instructions are represented in
ASCII text form as assembly language instructions.
- Assembly
- Add the integers in registers 16 and 17 and store
the result in register 1. - Object Code
- 32-bit pattern
- Stored at address 0x1200012d0
addq r16,r17,r1
0x1200012d0 0x42110401b
36Basic processor operation
- Fetch
- load an instruction from memory, using the
address contained in the PC. - Execute
- load word from memory to register file
- store word from register file to memory
- perform an arithmetic operation (e.g., addition)
on the contents of two registers and store the
results in a register. - Update PC
- if Branch instruction
- set PC to some new address
- if not Branch instruction
- increment PC to point to next sequential
instruction - ex PC lt-- PC 4
37Basic instructions load
- Move a word from memory to a register.
- example ld r1, addr
- move word at address addr (Memaddr) to register
r1. - increment PC with address of next instruction.
Processor
addr
P C
ALU
Register File
Memaddr
MemPC ld r1, addr
PC lt-- PC 4
38Basic instructions store
- Move a word from a register to memory
- example st r1, addr
- move contents of register r1 (Regr1) to memory
address addr. - increment PC with address of next instruction
Processor
ltaddrgt
P C
ALU
Register File
Regr1
MemPC st r1, addr
PC lt-- PC 4
39Basic instructions arithmetic operations
- Add the contents of two registers and store the
result in a third register - example add r16,r17,r1
- add contents of r16 and r17 and store the results
in r1. - increment PC with address of next instruction
Processor
P C
ALU
Register File
MemPC add r16, r17, r1
PC lt-- PC 4
40Basic instructions branch
- branch to a new location in the program
- example branch addr
- set the PC to addr
Processor
P C
ALU
Register File
MemPC branch addr
PC lt-- addr
41Altering the control flow
- Changing the default value of the PC (I.e.,
pointing to the next instruction in memory) is
called altering the control flow. - There are two mechanisms for altering the control
flow - executing branch instructions
- exceptions
- crucial mechanism for modern multitasking
operating systems.
42Exceptions and interrupts
Keyboard
Mouse
Printer
Modem
Processor
Interrupt controller
Serial port controller
Parallel port controller
Keyboard controller
Local/IO Bus
Network adapter
Video adapter
Memory
IDE disk controller
SCSI controller
SCSI bus
disk
Network
Display
disk
cdrom
43Exceptions
- An exception is a transfer of control to the OS
in response to some event (i.e. change in
processor state)
User Process
Operating System
exception processing by exception handler (also
called a device driver or an interrupt handler)
event
exception
exception return (optional)
44Internal (CPU) exceptions
- Internal exceptions occur as a result of events
generated by executing instructions. - Execution of a SYSCALL instruction.
- allows a program to ask for OS services (e.g.,
timer updates) - Execution of a BREAK instruction
- used by debuggers
- Errors during instruction execution
- arithmetic overflow, address error, parity error,
undefined instruction - Events that require OS intervention
- virtual memory page fault
45External (I/O) exceptions(or I/O interrupts)
- External exceptions occur as a result of events
generated by devices external to the processor
(managed by interrupt controller). - I/O interrupts
- hitting C at the keyboard
- arrival of a packet from a network
- arrival of data from a disk
- Hard reset interrupt
- hitting the reset button
- Soft reset interrupt
- hitting ctl-alt-delete on a PC
46High-level languages
- High-level languages like C/C, Java, and Perl
provide an abstraction for the low-level details
of machine-language programs.
Corresponding machine/assembly code
C Code
foo 0x42100400 addq a0,a0,v0 0x40110400
addq v0,a1,v0 0x4c120400 mulq
v0,a2,v0 0x6bfa8001 ret zero,(ra),1
long foo(long a, long b, long c) long sum
(aab)c return(sum)
47Procedures
- Procedures (or functions) are named collections
of commonly executed instruction sequences. - Crucial abstraction mechanism in every
programming language at every level. - take some input, produce some output
48Perl procedure example
definition of function say prints first
parameter followed by second sub say print
_0, _1!\n main body of Perl
program first invocation of
s say(hello,world) prints hello,
world! second invocation of say x hi
there y Dave say(x, y) prints hi
there, Dave!
From Learning Perl, page 95.
49Compiled vs interpreted programs
- Compiled programs
- translated in a series of steps by a compiler,
assembler, and linker from an ASCII source
program written in a high level language to a
binary executable... - and then loaded into memory and executed by a
loader. - Examples C/C compilers, Java Just-in-Time
(JIT) compilers - Interpreted programs
- executing interpreter reads the ASCII source
program and executes its statements. - Examples Java virtual machine (JVM), Perl
interpreter. - Compiled object code is to processor as
interpreted source program is to interpreter.
50Compiled programs
source program
ASCII text files
C program (p1.c p2.c)
Compiler
Asm program (p1.s p2.s)
ASCII text files
Assembler
binary files
Object program (p1.o p2.o)
libraries (.a)
Linker
binary file
Executable object program (p)
executable program
Loader
Executing process (p)
memory
51Interpreted programs
Perl script (foo.pl)
Executing Perl interpreter
52Pros and cons of compiled and interpreted programs
- Efficiency
- compiled programs are more efficient
- instructions are executed directly by hardware
- interpreted programs can be orders of magnitude
slower than compiled programs. - one of the current problems with Java.
- Ease of Use
- Interpreted programs are easier to write
- compiled code edit, compile, link, execute cycle
- interpreted code edit, execute cycle
53Processes
- A process is an instance of a running program
- runs concurrently with other processes
(multitasking) - managed by a shared piece of OS code called the
kernel - kernel is not a separate process, but rather runs
as part of some user process - each process has its own data space and process
id (pid) - data for each process protected from other
processes
Process A
Process B
user code
kernel code
Just a stream of instructions!
Time
user code
kernel code
user code
54Unix process hierarchy
0
init 1
shell
Daemon e.g. httpd
child
child
child
grandchild
grandchild
55Unix Startup Step 1
1. Pushing reset button loads the PC with the
address of a small bootstrap program. 2.
Bootstrap program loads the boot block (disk
block 0). 3. Boot block program loads kernel
(e.g., /vmunix) 4. Boot block program passes
control to kernel. 5. Kernel handcrafts the data
structures for process 0.
0
process 0 handcrafted kernel process
process 1 user mode process fork() and
exec(/sbin/init)
init 1
56Unix Startup Step 2
0
init forks new processes as per the /etc/inittab
file
init 1
/etc/inittab
forks a getty (get tty or get terminal) for the
console
getty
Daemons e.g. snmp
57Unix Startup Step 3
0
init 1
getty execs a login program
login
58Unix Startup Step 4
0
init 1
login gets users login and password if OK, it
execs a shell if not OK, it execs another getty
tcsh
59Running programs from the Unix shell
0
The shell displays a prompt ( in this example)
and waits for the user to type a command such as
ls, cd, or the name of a program to execute.
init 1
shell (tcsh)
keyboard
screen
60Running programs from the Unix shell
0
When the user types in the name of program to run
(e.g. foo), the shell creates a new child
process and runs the program within that process,
transferring control of the keyboard and display
to the child.
init 1
foo
shell (tcsh)
keyboard
foo
screen
child(foo)
foo
61Running programs from the Unix shell
0
init 1
While the child is running, it reads input from
the keyboard and writes output to the screen.
shell (tcsh)
screen
child (foo)
keyboard
foo ltoutput from foogt
62Running programs from the Unix shell
0
The shell waits for the child process (foo) to
finish and then prints another prompt, Indicating
that it is ready to read another command from the
keyboard.
init 1
shell (tcsh)
keyboard
screen
foo ltoutput from foogt
63Programs summary
- Key concepts
- programs exists at different levels of
abstraction - machine code, assembly code, high-level source
- programs are just data in memory
- program instructions are just bits!
- programs can be executed by a processor or by an
interpreter. - processes are instances of running programs.
- modern OSs allow multiple processes to run
independently at the same time.