device management
DESCRIPTION
operating systems. Device Management. The von Neumann Architecture System Architecture Device Management Polling Interrupts DMA. operating systems. Von Neumann Architecture. Born 28 December 1903, Budapest, Hungary; Died 8 February 1957, Washington DC; - PowerPoint PPT PresentationTRANSCRIPT
1
Device ManagementDevice ManagementDevice ManagementDevice Management
• The von Neumann Architecture• System Architecture• Device Management
•Polling•Interrupts•DMA
operatingsystems
2
Von Neumann Von Neumann ArchitectureArchitecture
Von Neumann Von Neumann ArchitectureArchitecture
Born 28 December 1903, Budapest, Hungary; Died 8 February 1957, Washington DC; Brilliant mathematician, synthesizer, and promoter of the stored program concept which became the prototype of most of its successors - the von Neumann Architecture
operatingsystems
3
Von Neumann Von Neumann ArchitectureArchitecture
Von Neumann Von Neumann ArchitectureArchitecture
bus
cpumemorydevicedevice
Machine has a fixed set of electronic parts
Actions are determined by a program stored in the computer memory.
Instructions and Data
operatingsystems
Prior to this time programming was done with switches & plugboards
4
bus
cpu
address linesdata lines
ALUControlUnit
General Purpose&
Status Registers
operatingsystems The CPU and the BusThe CPU and the Bus
5
cpu
ALUControl
Unit
General Purpose&
Status Registers
fetch unit
decode unit
execution unit
instruction reg
program ctraddress
instruction
operatingsystems
6
bus
Memory unit
memory MAR
MDR
Command
Memory Address register
Memory Data register
operatingsystems The Memory UnitThe Memory Unit
7
bus
Memory unit
memoryMAR
MDR
CMDREG
Writing to MemoryWriting to Memory
Address to write toData to write
Write command
operatingsystems
Memory Address register
Memory Data register
8
DevicesDevicesDevicesDevices
In a Unix system, every device is treated just like a file.In fact, all devices have a file name in the file system
operatingsystems
Files that represent devices are in the /dev directory
$ ls –C /dev
You can list the terminal device you are attached to
$ tty
You can copy a file to the terminal
$ cp myFile.txt /dev/tty2
9
Block Devices
Stores data in fixed size blocks.Blocks can be read/written independently
Character DevicesDelivers or accepts a stream of characters.
DevicesDevicesDevicesDevicesoperatingsystems
10
Do an ls command on a terminal device
$ ls –al /dev/ttyp1
operatingsystems
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
this is usually wherethe file size goes.What’s up?
11
Do an ls command on a terminal device
$ ls –al /dev/tty1
operatingsystems
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
The i-node in this case does notcontain the address of a disk block,but the address of a device driver.
In this case, 4 is the “address” of thedevice driver, and 1 is an argumentpassed to the device driver.
12
operatingsystems
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
only the terminal owner can read from the terminal.
any other terminal user can write to the terminal
13
At the API, all devices support a common set of commands:
openclosereadwrite ...
operatingsystems
14
For example, to read from a tape drive, you mightwrite code that looks like the following:
int fd;fd = open(“dev/tape”,O_RDONLY);lseek(fd, long (4096, SEEK_SET);n = read(fd, buffer_length);close(fd);
15
Note that all devices do not support all of thefile system commands. For example, you cannotdo a seek on a terminal.
16
I/O LayersI/O LayersI/O LayersI/O Layers
User ProgramUserSpace
Operating System
DeviceDriver
KernelSpace
DeviceController
DeviceDriver
DeviceController
DeviceDriver
DeviceController
API
operatingsystems
Standard I/F
openclosereadwriteioctl
17
Layers of the I/O system and the main
functions of each layer
Layers of the I/O system and the main
functions of each layer
User Process
Device Independent Layer
Device Dependent Layer
Interrupt Handler
LOGICAL I/O: open, read, write, …
Naming, protection, blocking, allocation
Write to device registers, read statusDri
ver
Wake up driver when I/O completes
Device Controller
Perfom the physical I/O operationdata status command
operatingsystems
18
Device ControllersDevice ControllersDevice ControllersDevice ControllersI/O devices have two major components:
A mechanical component An electronic component
The electronic component is the device controller. It may be able to handle multiple devices
Controller's tasksconvert serial bit stream to block of bytesperform error correction as necessarymake data available to main memorycontinuously monitor the state of the deviceControl the device
I/O devices have two major components:A mechanical component An electronic component
The electronic component is the device controller. It may be able to handle multiple devices
Controller's tasksconvert serial bit stream to block of bytesperform error correction as necessarymake data available to main memorycontinuously monitor the state of the deviceControl the device
operatingsystems
19
Control and statusregisters
Data buffer
Device Controller
I/O Bus
Put an address on the address line of the I/O bus the address selects the device
put the command to be executed on the control line
operatingsystems
20
A Device Controller A Device Controller Example:Example:
Reading Data From DiskReading Data From Disk
A Device Controller A Device Controller Example:Example:
Reading Data From DiskReading Data From Disk
CPU Controller
buffer
Memory
operatingsystems
1. The cpu issues a read command to the disk controller
read
21
Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk
CPU Controller Memory
1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s internal buffer
operatingsystems
||||||||||||
buffer
22
Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk
CPUController
buffer
Memory
1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read
INT
operatingsystems
23
Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk
CPU Controller
buffer
Memory
1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read4. The cpu reads one byte of data from the controller buffer
reg
operatingsystems
a
24
Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk
CPU Controller
buffer
Memory
1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read4. The cpu reads one byte of data from the controller buffer5. The cpu writes the byte to memory. This process continues until all data is transferred
reg
operatingsystems
a
25
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
MemoryDMA Controller
A DMA controller is like a mini-cpu. It can readAnd write data to memory without involving
The CPU.
operatingsystems
26
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count,and a command to execute. It also sends a read command to the disk controller. The cpu can now go on and do other work.
DMA Controller
read
operatingsystems
pgm
27
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.
2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.
DMA Controller
operatingsystems
||||||||||
28
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.
2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.
3. The DMA controller sends a command to the disk controller requesting abyte of data.
DMA Controller
operatingsystems
29
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.
2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.
3. The DMA controller sends a command to the disk controller requesting abyte of data.
4. The disk controller sends a byte of data to the dma controller
DMA Controller
operatingsystems
a
30
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.
2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.
3. The DMA controller sends a command to the disk controller requesting abyte of data.
4. The disk controller sends a byte of data to the dma controller5. The dma controller writes the byte to memory.
DMA Controller
operatingsystems
a
31
Using Direct Memory Using Direct Memory AccessAccess
Using Direct Memory Using Direct Memory AccessAccess
CPU Controller
buffer
Memory
6. The disk controller sends an acknowledgement signal to the dma controller. This continues until all of the data has been transferred. All of this happens without
the cpu being involved. When all of the data has been transferred, then the cpu is
interrupted.
DMA Controller
ack
operatingsystems
int
32
Memory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/O
Primarymemory
device 0
device 1
device 2
memory addresses
device addresses
Traditional approachuses special I/Oinstructions to getat device registers
copy_in R3, 0x012, 4
Copy the contents of register 4 in the device at address 012into cpu register 3
operatingsystems
33
Memory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/O
Primarymemory
device 0
device 1
device 2
memory addresses
With memory mapped I/O no special instructions are needed. Device registers are mapped by the hardware into memory addresses.
Load R3, 0xFFF0124
Load register 3 from memoryAddress 0xFFF0124
operatingsystems
34
Single Bus ArchitectureSingle Bus ArchitectureSingle Bus ArchitectureSingle Bus Architecture
bus
cpumemorydevicedevice
All addresses and data go on the same bus
operatingsystems
35
Multiple Bus ArchitectureMultiple Bus ArchitectureMultiple Bus ArchitectureMultiple Bus Architecture
bus
cpumemorydevicedevice
All device addresses and data go on another bus
The cpu uses one bus to access memory
operatingsystems
36
Polling vs. InterruptsPolling vs. InterruptsPolling vs. InterruptsPolling vs. Interruptsoperatingsystems
37
PollingPollingPollingPolling
Because there is a tremendous difference in speed between an I/O device and the computer, the computer must wait until the device is ready to process the next byte of data.
InstructionRegister
AccumulatorStatus
Register
Control lines and address lines
Data linesStatus lines
operatingsystems
38
ExampleExampleExampleExample
InstructionRegister
AccumulatorStatus
Register
Control lines and address lines
Data linesStatus lines
character to be printed
print instructiondevice address
characterprint
printed!
operatingsystems
39
PollingPollingPollingPolling
Issue WriteCommand
Load characterinto accumulator
Is theprinterdone?
noLoad characterinto accumulator
yes
What is wrong with this approach ?
If the printer can print 100 charactersa second, then it takes 10ms to printeach character. Thus this polling loopruns for 10 ms between each character.Clear the
status register
operatingsystems
40
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
1. Application issues a read( ) call. The OS blocks the application.
operatingsystems
An example of a device driver using polling
41
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
2. The device driver queries the status register in the controller to determine whether or not the device is busy. It polls until it is not.
BLOCKED
operatingsystems
42
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling
2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
3. The driver stores a command in the command register of the device controller.
BLOCKED
operatingsystems
43
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
4. The controller reads from the device. Since this involves mechanical parts, the read can take some time.
BLOCKED
operatingsystems
44
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
BLOCKED
operatingsystems
5. Meanwhile, the device driver queries the status register in the controller to see if the operation is complete. It polls until it is.The CPU cannot be used for anything else.
45
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
7. The device driver reads the data register(s) in the device controller to get the data that has been read from the device.
BLOCKED
operatingsystems
46
Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
8. The device driver copies the data into user space. The OS unblocks the application program.
BLOCKED
operatingsystems
47
Interrupts Interrupts Interrupts Interrupts
Handling interrupts on machines that do look-ahead or on super-scalar machines is quite complicated. This discussion is
simplified.
bus
cpu memoryInterruptcontroller
device
Device finishes it operation.It writes its status to theInterrupt controller
cpu
The Interrupt controllerissues the interrupt to theCPU. The interrupt includesa number which is used toindex into an interrupt table.
The CPU stops whatever it is doing,and executes the interrupt handlerpointed to by the interrupt.
operatingsystems
48
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
1. Application issues a read( ) call. The OS blocks the application.
operatingsystems
A Device Driver Using Interrupts
49
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
2. The device driver queries status registers in the device controller to see if the device is busy. If it is, it waits.
BLOCKED
operatingsystems
50
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
3. When the device is ready, the device driver stores a command in the command register of the device controller. This starts the device.
BLOCKED
operatingsystems
51
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
3. The driver then issues a wait().
BLOCKED
operatingsystems
wait means “I will voluntarily give up the cpuand wait for an interrupt to occur.
52
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
4. The OS schedules another process.
5. Meanwhile, the device controller reads from the device. Some other process is using the CPU
BLOCKED AnotherProcess
BLOCKED
operatingsystems
53
operatingsystems
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
6. The read is complete. The device controller interrupts the CPU.
7. The interrupt handler determines which device caused the interrupt. It does its clean up and then sends a signal to the device driver to wake it up.
InterruptHandler
BLOCKED
BLOCKED
signal
54
Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts
User Program
Operating System
DeviceDriver
KernelSpace
DeviceController
Device Interface (API)
UserSpace
8. The device driver copies the data from the data registers in the device controller into the user’s address space. It then does a return to the application.
operatingsystems
55
ExampleExampleExampleExample
processText to print
userspace
kernelspace
prin
t user process makesa print request
operatingsystems
56
Code executed when the system call is made
copy (userBuffer, kernelBuffer, count);enable interrupts; while (printerStatusReg != READY);printerDataReg = kernelBuffer[0];run scheduler;
Prints the first character
// Block the user and run some other process
57
ExampleExampleExampleExample
process
Text to print
userspace
kernelspace
blocked
The user process is blocked.
the print data is transferred
to the kernel.
process Another user process is scheduled to run.
operatingsystems
58
ExampleExampleExampleExample
process
Text to print
userspace
kernelspace
blocked
the first character is sent to the printer
process while the printer works, this process can run.
operatingsystems
59
ExampleExampleExampleExample
process
Text to print
userspace
kernelspace
blocked
The printer sends an interrupt.It is ready for another character.
process This process is interrupted.
operatingsystems
60
ExampleExampleExampleExample
process
Text to print
userspace
kernelspace
blocked
the next character is sent to the printer
process while the printer works, this process can run again.
operatingsystems
61
ExampleExampleExampleExample
process
userspace
kernelspace
When all of the data has beenprinted, the process is unblocked.
It will now be scheduled to run.
process
operatingsystems
62
Interrupt HandlersInterrupt HandlersInterrupt HandlersInterrupt Handlersoperatingsystems
Interrupt handlers are best hidden deep in the O/S. In the best case, drivers are kernel processes – they use privileged instructions.
have the driver starting an I/O operation block until an interrupt notifies it of completion
Interrupt procedure does its work - when finished it unblocks driver that started it
63
Handling an InterruptHandling an InterruptHandling an InterruptHandling an Interruptoperatingsystems
Save any registers not saved by the interrupt hardware
Set up the context for the interrupt to run in Copy saved registers to the process table Set up stack for interrupt service procedure
Ack interrupt controller, re-enable interrupts
Run the interrupt handler
Determine which process to run next
Set up context for the next process to run
Load new process' registers
Start running the new process
64
Print Driver Interrupt Service Routine
if (count == 0) done so unblock user;else{ i = i + 1; count = count -1; printerDataReg = kernelBuffer[i];}Acknowledge interrupt;Return from interrupt;
65
operatingsystems
Device Manager DesignDevice Manager DesignDevice Manager DesignDevice Manager Design
User Program
Operating System
KernelSpace
Device Interface (API)
UserSpace
read( device_x, … )
Trap Tableread( device_x )
read( deviceID, …){ switch(deviceID) { case dev0: dev0_read( … ); break;
case dev1: dev1_read( …); break; …
This designrequires that thekernel be recompiledevery time a newdevice is added.
66
Reconfigurable DriversReconfigurable DriversReconfigurable DriversReconfigurable DriversDevice 01
Device 02
Device 03
Device 04
…
open( ) read( ) write( ) ioctl( )
When a new device is added, fill in entries in the table using a device “Registration” procedure.
operatingsystems