63
Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department of Electrical and Computer Engineering Carnegie Mellon University Institute for eCommerce, Summer 1999

Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Embed Size (px)

Citation preview

Page 1: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 1

20-755: The InternetLecture 2: Computer Systems I

David O’Hallaron

School of Computer Science and

Department of Electrical and Computer Engineering

Carnegie Mellon University

Institute for eCommerce, Summer 1999

Page 2: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 2

Today’s lecture

• Administrative issues (10 minutes)

• Data (50 min)

• Break (10 min)

• Programs (40 min)

Page 3: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 3

Systems

• 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.

Page 4: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 4

Abstraction

• Example: a lighting system

B

switch

light bulb

A

B

A

Page 5: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 5

Abstraction

• Here’s 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.”

Page 6: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 6

Abstraction

• Here’s 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).

Page 7: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 7

Abstraction

• 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.

Page 8: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 8

Typical computer system

Local/IO BusLocal/IO Bus

MemoryMemory Networkadapter

Networkadapter

IDE diskcontroller

IDE diskcontroller

Videoadapter

Videoadapter

DisplayDisplay NetworkNetwork

ProcessorProcessor Interruptcontroller

Interruptcontroller

SCSIcontroller

SCSIcontroller

SCSI busSCSI bus

Serial port controller

Serial port controller

Parallel portcontroller

Parallel portcontroller

Keyboardcontroller

Keyboardcontroller

KeyboardKeyboard MouseMouse PrinterPrinterModemModem

disk

disk cdrom

Page 9: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 9

Bits

• All computer data (input, output, memory, and even programs) are collections of 1’s and 0’s 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.0V

0.5V

2.8V

3.3V

0 1 0

Page 10: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 10

Powers of 10

• Def: 10p = 10 x 10 x ... x 10 (there are p 10’s)– “ten to the power p” or “the pth power of 10”

• Examples of powers of 10100 = 1

101 = 10

102 = 10 x 10 = 100

103 = 10 x 10 x 10 = 1,000

Page 11: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 11

Decimal (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

Page 12: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 12

Powers of 2

• Def: 2p = 2 x 2 x ... x 2 (there are p 2’s)– “two to the power p” or “the pth power of two”

• Examples of powers of 220 = 1

21 = 2

22 = 2 x 2 = 4

23 = 2 x 2 x 2 = 8

24 = 2 x 2 x 2 x 2 = 16

Page 13: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 13

Binary (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

Page 14: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 14

Converting from decimal to binary

• converting decimal 5 to binary5 / 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 binary3 / 2 = 1 rem 1 first (righmost) binary digit is 1

1 / 2 = 0 rem 1 second binary digit is 1

So 3 = 112 = 0112

Page 15: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 15

Counting 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)

Page 16: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 16

Counting 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)

Page 17: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 17

Practice 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 =

Page 18: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 18

Practice 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:

Page 19: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 19

Two’s complement representation of signed integers

0000000100100011010001010110011110001001101010111100110111101111

(0)(1)(2)(3)(4)(5)(6)(7)(-8)(-7)(-6)(-5)(-4)(-3)(-2)(-1)

sign bit: 0: positive 1: negative

positiveintegers

negativeintegers

Page 20: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 20

Interpreting two’s complement numbers

• To negate a two’s 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 two’s complement representation of a negative number.

– Example: -5 = inv(0101) + 1 = 1010 + 1 = 1011, where inv(x) inverts the bits of x.

– Example: -12 = ?

Page 21: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 21

Hex (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

Binary

0000000100100011010001010110011110001001101010111100110111101111

Hex

0123456789abcdef

Decimal

0123456789

101112131415

In Perl, use the “0x” prefix to denote a hex number:

x = 0x6a;

y = 0xbeef;

Page 22: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 22

Characters

• 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)

Page 23: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 23

ASCII character set

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 DC314 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 132 233 3

hex char esc

34 4 35 5 36 6 37 7 38 8 39 9 3A : 3B ; 3C < 3D = 3E > 3F ? 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G48 H 49 I 4A J4B K4C 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 c64 d65 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

Page 24: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 24

Computer memory

Local/IO BusLocal/IO Bus

MemoryMemory Networkadapter

Networkadapter

IDE diskcontroller

IDE diskcontroller

Videoadapter

Videoadapter

DisplayDisplay NetworkNetwork

ProcessorProcessor Interruptcontroller

Interruptcontroller

SCSIcontroller

SCSIcontroller

SCSI busSCSI bus

Serial port controller

Serial port controller

Parallel portcontroller

Parallel portcontroller

Keyboardcontroller

Keyboardcontroller

KeyboardKeyboard MouseMouse PrinterPrinterModemModem

disk

disk cdrom

Page 25: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 25

Metrics 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!

Page 26: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 26

Computer memory

• Organized as a sequential array of bytes.

• Each byte has an integer address (location).

• Addresses start at 0.

000000010002000300040005000600070008000900100011

Bytes Addr

0012001300140015

Page 27: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 27

Words

• 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, we’ll be complaining about

this too!

000000010002000300040005000600070008000900100011

Bytes Addr

0012001300140015

Addr =

0000

Addr =

0008

Words

Addr =

0012

Page 28: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 28

Strings

• 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”;

‘H’

‘I’

SPACE

‘D’

‘a’

‘v’

‘e’

‘\0’

000000010002000300040005000600070008000900100011

Bytes Addr

0012001300140015

Page 29: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 29

Data Representation Summary

• Key concepts: – It’s 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.

Page 30: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 30

Break time!(10 mins)

Page 31: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 31

Today’s lecture

• Administrative issues (10 minutes)

• Data (50 min)

• Break (10 min)

• Programs (40 min)

Page 32: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 32

Programs

Local/IO BusLocal/IO Bus

MemoryMemory Networkadapter

Networkadapter

IDE diskcontroller

IDE diskcontroller

Videoadapter

Videoadapter

DisplayDisplay NetworkNetwork

ProcessorProcessor Interruptcontroller

Interruptcontroller

SCSIcontroller

SCSIcontroller

SCSI busSCSI bus

Serial port controller

Serial port controller

Parallel portcontroller

Parallel portcontroller

Keyboardcontroller

Keyboardcontroller

KeyboardKeyboard MouseMouse PrinterPrinterModemModem

disk

disk cdrom

Page 33: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 33

Processor components

• 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

DataPC

RegisterFile

Processor Memory

Object CodeProgram Data

Addresses

Instructions

Register file

...

R0

R1

R2

R30

R31

Registername

ALU

Page 34: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 34

Object 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.

Page 35: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 35

Assembly 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

Page 36: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 36

Basic 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 <-- PC + 4

Page 37: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 37

Basic instructions: load

• Move a word from memory to a register.– example: ld r1, addr– move word at address addr (Mem[addr]) to register r1.

– increment PC with address of next instruction.

Memory

Object CodeProgram Data

addr

Mem[PC]“ld r1, addr”

Mem[addr]

PC <-- PC + 4

PC

RegisterFile

Processor

ALU

Page 38: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 38

Basic instructions: store

• Move a word from a register to memory– example: st r1, addr– move contents of register r1 (Reg[r1]) to memory address

addr. – increment PC with address of next instruction

Memory

Object CodeProgram Data

<addr>

Mem[PC]“st r1, addr”

Reg[r1]

PC <-- PC + 4

PC

RegisterFile

Processor

ALU

Page 39: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 39

Basic 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

Memory

Object CodeProgram Data

PC <-- PC + 4

Mem[PC]“add r16, r17, r1”

PC

RegisterFile

Processor

ALU

Page 40: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 40

Basic instructions: branch

• branch to a new location in the program– example: branch addr– set the PC to addr

Memory

Object CodeProgram Data

PC <-- addr

Mem[PC]“branch addr”

PC

RegisterFile

Processor

ALU

Page 41: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 41

Altering 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.

Page 42: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 42

Exceptions and interrupts

Local/IO BusLocal/IO Bus

MemoryMemory Networkadapter

Networkadapter

IDE diskcontroller

IDE diskcontroller

Videoadapter

Videoadapter

DisplayDisplay NetworkNetwork

ProcessorProcessor Interruptcontroller

Interruptcontroller

SCSIcontroller

SCSIcontroller

SCSI busSCSI bus

Serial port controller

Serial port controller

Parallel portcontroller

Parallel portcontroller

Keyboardcontroller

Keyboardcontroller

KeyboardKeyboard MouseMouse PrinterPrinterModemModem

disk

disk cdrom

Page 43: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 43

Exceptions

• 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

exceptionexception processingby exception handler(also called a device driver or an interrupt handler)exception

return (optional)

event

Page 44: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 44

Internal (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

Page 45: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 45

External (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

Page 46: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 46

High-level languages

• High-level languages like C/C++, Java, and Perl provide an abstraction for the low-level details of machine-language programs.

C Code Corresponding machine/assembly code

foo:0x42100400 addq a0,a0,v00x40110400 addq v0,a1,v00x4c120400 mulq v0,a2,v00x6bfa8001 ret zero,(ra),1

long foo(long a, long b, long c) { long sum = (a+a+b)*c; return(sum);}

Page 47: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 47

Procedures

• 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

Page 48: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 48

Perl procedure example

# definition of function say# prints first parameter followed by secondsub say { print “$_[0], $_[1]!\n”; }

## main body of Perl program#

# first invocation of ssay(“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.

Page 49: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 49

Compiled 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.

Page 50: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 50

ASCII text files

binary files

Compiler

Assembler

Linker

C program (p1.c p2.c)

Asm program (p1.s p2.s)

Object program (p1.o p2.o)

Executable object program (p)

libraries (.a)

Compiled programs

ASCII text files

binary file

Loader

Executing process (p)memory

“source program”

“executable program”

Page 51: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 51

Interpreted programs

Executing Perl interpreter

Perl script (foo.pl)

Page 52: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 52

Pros 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

Page 53: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 53

Processes• 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

user code

kernel code

user code

Time Just a stream of instructions!

Page 54: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 54

Unix process hierarchy

shell

childchildchild

grandchildgrandchild

init [1]

[0]

Daemone.g. httpd

Page 55: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 55

Unix Startup: Step 1

init [1]

[0] process 0: handcrafted kernel process

process 1: user mode processfork() and exec(/sbin/init)

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.

Page 56: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 56

Unix Startup: Step 2

init [1]

[0]

forks a getty (get tty or get terminal)for the console

gettyDaemonse.g. snmp

/etc/inittab init forks new processes as perthe /etc/inittab file

Page 57: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 57

Unix Startup: Step 3

init [1]

[0]

getty execs a login programlogin

Page 58: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 58

Unix Startup: Step 4

init [1]

[0]

login gets user’s login and password if OK, it execs a shellif not OK, it execs another getty

tcsh

Page 59: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 59

Running programs from the Unix shell

init [1]

[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.

shell (tcsh)

screen%

keyboard

“%”

Page 60: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 60

Running programs from the Unix shell

init [1]

[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 ofthe keyboard and display to the child.shell (tcsh)

screen% foo

keyboard“foo”

child(foo)

“foo”

Page 61: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 61

Running programs from the Unix shell

init [1]

[0]

While the child is running, it reads input from the keyboard and writes output to the screen.shell (tcsh)

screen

% foo <output from foo>

keyboard child (foo)

Page 62: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 62

Running programs from the Unix shell

init [1]

[0]

The shell waits for the child process (foo) to finish and then prints another prompt, Indicating that it is ready toread another command from the keyboard.

shell (tcsh)

screen% foo<output from foo>%

keyboard

“%”

Page 63: Lecture 2, 20-755: The Internet, Summer 1999 1 20-755: The Internet Lecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department

Lecture 2, 20-755: The Internet, Summer 1999 63

Programs 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 OS’s allow multiple processes to run independently at the same time.