Top Banner
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

1

CSC 2405: Computer Systems II

Spring 2012Dr. Tom Way

Page 2: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

2

Facilities for Programming

• Unix cluster– Machines: csgate, tanner, degas, cezanne,

picasso,rodin, cassatt, gauguin, matisse– List displayed when logging into csgate

• Linux machines– felix, helix

• Logging in to the machines remotely– SSH available for download from the CSC website

Page 3: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

3

Key to Success• Start early to allow time for debugging.

Page 4: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

4

Policies: Write your own code

Programming in an individual creative process much like composition. You must reach your own understanding of the problem and discover a path to its solution. During this time, discussions with friends are encouraged. However, when the time comes to write code that solves the problem, such discussions are no longer appropriate

– The program must be your own work –

Page 5: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

5Villanova University

Memory Hierarchy

Cache PrincipleThe more frequently data is accessed, the faster the

access should be.

Page 6: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

6Villanova University

Central Processing Unit (CPU)• Runs the loop

Fetch-Decode-Execute

Fetch NextInstructionSTART

Execute

Instruction

ExecuteInstruction

Execute

InstructionHALT

Fetch Cycle Execute Cycle

DecodeInstructionSTART

Decode Cycle

Fetch the next instruction from memory Decode the instruction to figure out what to do Execute the instruction and store the result

Page 7: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

7Villanova University

Fetch-Decode-Execute• Where is the “next instruction” held in the machine?

– a CPU register called the Program Counter (PC) holds the address of the instruction to be fetched next

• Fetch cycle– Copy instruction from memory into Instruction Register (IR)

• Decode cycle– Decode instruction and fetch operands, if necessary

• Execute cycle– Execute the instruction– Increment PC by the instruction length after execution

(assuming that all instructions are the same length)

Page 8: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

8Villanova University

Device Controller• Special-purpose processor• In charge of a particular

device type• Has registers

(data, control, status)• Has local buffer storage

• I/O is from the device to local buffer of controller• CPU moves data from/to memory to/from local

buffer• I/O devices and CPU can execute concurrently

Page 9: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

9Villanova University

I/O Operation Example

CPU

Keyboard Controller

Keyboardc = getchar();

Page 10: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

10Villanova University

Input / Output (I/O)

• To start an I/O operation, the CPU tells the controller:– The chunk size be transferred (eg, one character)– Goes off to do something else

• The device controller:– Checks registers to determine what to do (read, write)– Transfers data to/from device from/to local buffer– Informs the CPU when transfer is complete (HOW?)

Page 11: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

11Villanova University

• Hardware may trigger interrupts at any time by sending a signal to the CPU by way of system bus

• When the CPU is interrupted– Stops what it is doing– Transfers control to a fixed memory location (Interrupt

Vector)

Hardware Interrupts

Page 12: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

12Villanova University

• A table of pointers (addresses) at a fixed memory location

• Contains addresses of interrupt service routines

• Indexed by aunique devicenumber– Given with the

interrupt request

Interrupt Vector

Interruptvector

0

OS code for disk interrupt

OS code for divide by zero trap

12...

Page 13: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

13Villanova University

Interrupt Handling

• The interrupt architecture – Must save the Program Counter (PC) prior to transferring control to

the interrupt service routine (interrupt handler) – Restore PC upon returning from interrupt

• Interrupt handler– Save registers that are to be modified onto the stack– Service request (eg, copy data from local buffer in memory)– Mark the process blocked on I/O as ready to run– Restore registers from the stack

• Interrupted computation resumes as the point it left off.

Page 14: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

14Villanova University

When does the CPU Check for Interrupts?

Fetch NextInstructionSTART

Execute

Instruction

ExecuteInstruction

Execute

InstructionHALT

Fetch Cycle Execute Cycle

DecodeInstructionSTART

Decode Cycle

???

Page 15: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

15Villanova University

• Interrupts must be handled quickly

• Interrupts are a critical part of a computer system– They allow a program to be interrupted, so the computer may

deal with an urgent event• All modern computer systems are interrupt-driven

CPU Cycle with Interrupts

STARTSTART ExecuteExecuteInstruction

Check forInterrupt:

Process Interrupt

Check forInterrupt:

Process Interrupt

Fetch Cycle Decode Cycle Execute Cycle Interrupt Check

Interrupts disabled

InterruptsEnabled

FetchInstruction

DecodeInstruction

STARTHALT

Page 16: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

16Villanova University

Direct Memory Access I/O

• To start an I/O operation, the CPU tells the DMA controller:– The chunk size to be transferred (eg, 4096 bytes of data)– The memory address where the chunk ought to be stored

• The DMA controller– Accesses the device via its controller– Transfers the chunk from/to device to/from system MEMORY– Interrupts CPU when transfer is complete

• Benefits– The CPU is only involved at the start and end of transfer– Interrupts are now less frequent – Hence, CPU can do a lot of work between interrupts

Page 17: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

17Villanova University

count = read(fd,buffer,nbytes);DMA Example

CPU Disk Controller

Disk driveMemory

Page 18: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

18Villanova University

Where Does the OS Fit?

Operating System

System CallsUsers and User Programs

WebBrowser

MusicPlayer

InterruptsDevice Control

Page 19: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

19Villanova University

• Software may trigger interrupts by system calls or illegal operations (such as invalid memory access, divide by zero)

Software Interrupts

Hardware

Operating System

Users and User ProgramsSystem Calls

Page 20: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

20Villanova University

System Call Example (1)

• What happens when a user executes a system call such as read? In Unix, for instance:

count = read(fd,buffer,nbytes) which reads up to nbytes from the file described by fd into buffer. The actual number of bytes read is returned

• Steps:1. Push third parameter on to the stack. 2. Push second parameter on to the stack. 3. Push first parameter on to the stack. 4. Call the library routine, which involves pushing the return address

on to the stack and jumping to the routine.

Page 21: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

21Villanova University

System Call Example (2)

Page 22: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

22Villanova University

System Call Example (3)

5. Machine/OS dependent actions. One is to put the system call number for read in a well defined place, e.g., a specific register. This requires assembly language.

6. Trap to the kernel (assembly language). This enters the operating system properly and shifts the computer to privileged mode.

7. The envelope uses the system call number to access a table of pointers to find the handler for this system call.

8. The read system call handler processes the request (see below).

Page 23: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

23Villanova University

System Call Example (4)

9. Some magic instruction returns to user mode and jumps to the location right after the trap.

10.The library routine returns (count is also returned).

11.The stack is popped (ending the function call read).

Page 24: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

24Villanova University

The Operating SystemResource Principle

An Operating Systems is a set of algorithms that allocates resources to processes.

Beautification Principle

An Operating Systems is a set of algorithms that hide the details of the hardware and provide a more pleasant

environment

Page 25: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

25Villanova University

DMA Exercise

Suppose that a user program requests the following data transfer:

Read 4096 bytes of data from file “test”

into array “mybuffer”

Describe in detail all steps involved:OS activitiesDMA controller activitiesCPU activities

Page 26: 1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.

26Villanova University

Summary

• Main computer system components:– CPU, Memory, I/O Devices

• Fetch-Decode-Execute-InterruptCheck cycle• I/O device controllers

– Special processors– Use local buffers for I/O transfer

• Software interrupts (system calls, traps) vs. hardware interrupts

• Interrupt handling steps • Interrupt-Driven I/O with DMA