260 likes | 403 Views
CSC 2405: Computer Systems II. Spring 2012 Dr. Tom Way. 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
E N D
CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way
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
Key to Success • Start early to allow time for debugging.
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 –
Fetch Cycle Decode Cycle Execute Cycle Decode Instruction Fetch Next Instruction Execute Instruction START Execute Execute HALT START Instruction Instruction Central Processing Unit (CPU) • Runs the loop Fetch-Decode-Execute • Fetch the next instruction from memory • Decode the instruction to figure out what to do • Execute the instruction and store the result
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)
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
I/O Operation Example Keyboard c = getchar(); Keyboard Controller CPU
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?)
Hardware Interrupts • 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)
Interrupt Vector • 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 0 1 Interrupt vector 2 . . . OS code for disk interrupt OS code for divide by zero trap
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.
Fetch Cycle Decode Cycle Execute Cycle Decode Instruction Fetch Next Instruction Execute Instruction START Execute Execute HALT START Instruction Instruction When does the CPU Check for Interrupts? ???
Fetch Cycle Decode Cycle Execute Cycle Interrupt Check Interrupts disabled Fetch Instruction Check for Check for START Interrupt: START Interrupt: Process Interrupt Process Interrupt Interrupts Enabled Decode HALT Instruction START Execute Execute Instruction CPU Cycle with Interrupts • 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
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
DMA Example count = read(fd,buffer,nbytes); Memory Disk drive Disk Controller CPU
Operating System Where Does the OS Fit? Music Player Web Browser Users and User Programs System Calls Interrupts Device Control
Users and User Programs Operating System Hardware Software Interrupts • Software may trigger interrupts by system calls or illegal operations (such as invalid memory access, divide by zero) System Calls
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: • Push third parameter on to the stack. • Push second parameter on to the stack. • Push first parameter on to the stack. • Call the library routine, which involves pushing the return address on to the stack and jumping to the routine.
System Call Example (3) • 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. • Trap to the kernel (assembly language). This enters the operating system properly and shifts the computer to privileged mode. • The envelope uses the system call number to access a table of pointers to find the handler for this system call. • The read system call handler processes the request (see below).
System Call Example (4) • Some magic instruction returns to user mode and jumps to the location right after the trap. • The library routine returns (count is also returned). • The stack is popped (ending the function call read).
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 activities DMA controller activities CPU activities
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