Slide Set 11
Download
Report
Transcript Slide Set 11
operating
systems
Device Management
• The von Neumann Architecture
• System Architecture
• Device Management
•Polling
•Interrupts
•DMA
1
operating
systems
Von Neumann
Architecture
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
2
Von Neumann
Architecture
operating
systems
Machine has a fixed set of electronic parts
Actions are determined by a program
stored in the computer memory.
Prior to this time programming was done with switches & plugboards
Instructions and Data
device
device
memory
cpu
bus
3
operating
systems
The CPU and the Bus
cpu
Control
Unit
ALU
General Purpose
&
Status Registers
bus
address lines
data lines
4
operating
systems
cpu
Control
Unit
fetch unit
decode unit
ALU
instruction reg
program ctr
instruction
address
execution unit
General Purpose
&
Status Registers
5
operating
systems
The Memory Unit
Memory unit
memory
MAR
Memory Address register
MDR
Memory Data register
Command
bus
6
operating
systems
Writing to Memory
Memory unit
memory
MAR
MDR
Memory Address register
Memory Data register
CMD
REG
bus
Write command
Data to write
Address to write to
7
operating
systems
Devices
In a Unix system, every device is treated just like a file.
In fact, all devices have a file name in the file system
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
8
operating
systems
Devices
Block Devices
Stores data in fixed size blocks.
Blocks can be read/written independently
Character Devices
Delivers or accepts a stream of characters.
9
operating
systems
Do an ls command on a terminal device
$ ls –al /dev/ttyp1
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
this is usually where
the file size goes.
What’s up?
10
operating
systems
Do an ls command on a terminal device
$ ls –al /dev/tty1
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
The i-node in this case does not
contain the address of a disk block,
but the address of a device driver.
In this case, 4 is the “address” of the
device driver, and 1 is an argument
passed to the device driver.
11
operating
systems
only the terminal owner can read from the terminal.
crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1
A character device
any other terminal user can write to the terminal
12
operating
systems
At the API, all devices support a
common set of commands:
open
close
read
write ...
13
For example, to read from a tape drive, you might
write 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);
14
Note that all devices do not support all of the
file system commands. For example, you cannot
do a seek on a terminal.
15
I/O Layers
operating
systems
User
Space
User Program
API
Kernel
Space
Operating System
Standard I/F
Device
Driver
Device
Driver
Device
Driver
Device
Controller
Device
Controller
Device
Controller
open
close
read
write
ioctl
16
Layers of the I/O
system and the main
functions of each layer
operating
systems
User Process
Driver
Device Independent Layer
LOGICAL I/O: open, read, write, …
Naming, protection, blocking, allocation
Device Dependent Layer Write to device registers, read status
Interrupt Handler
data
status
command
Device Controller
Wake up driver when I/O completes
Perfom the physical I/O operation
17
operating
systems
Device Controllers
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 tasks
convert serial bit stream to block of bytes
perform error correction as necessary
make data available to main memory
continuously monitor the state of the device
Control the device
18
operating
systems
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
I/O Bus
Device
Controller
Data buffer
Control and status
registers
19
operating
systems
A Device Controller Example:
Reading Data From Disk
Controller
CPU
Memory
buffer
read
1.
The cpu issues a read command to the disk controller
20
operating
systems
Reading Data From Disk
Controller
CPU
Memory
||||||||||||
buffer
1.
2.
The cpu issues a read command to the disk controller
The disk controller reads the required data from the device
and stores it in it’s internal buffer
21
operating
systems
Reading Data From Disk
CPU
Controller
Memory
buffer
INT
1.
2.
3.
The cpu issues a read command to the disk controller
The disk controller reads the required data from the device
and stores it in it’s buffer
The controller sends an interrupt to the cpu indicating that
data is ready to be read
22
Reading Data From Disk
operating
systems
Controller
CPU
reg
1.
2.
3.
4.
Memory
buffer
a
The cpu issues a read command to the disk controller
The disk controller reads the required data from the device
and stores it in it’s buffer
The controller sends an interrupt to the cpu indicating that
data is ready to be read
The cpu reads one byte of data from the controller buffer
23
Reading Data From Disk
operating
systems
CPU
a reg
1.
2.
3.
4.
5.
Controller
Memory
buffer
The cpu issues a read command to the disk controller
The disk controller reads the required data from the device
and stores it in it’s buffer
The controller sends an interrupt to the cpu indicating
that data is ready to be read
The cpu reads one byte of data from the controller buffer
The cpu writes the byte to memory. This process continues
until all data is transferred
24
operating
systems
Using Direct Memory Access
CPU
DMA Controller
Controller
Memory
buffer
A DMA controller is like a mini-cpu. It can read
And write data to memory without involving
The CPU.
25
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
Controller
Memory
buffer
pgm
read
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.
26
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
Controller
Memory
||||||||||
buffer
1.
2.
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 disk controller reads the data from the disk and stores it in its data
buffer, just as it did before.
27
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
Controller
Memory
buffer
1.
2.
3.
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 disk controller reads the data from the disk and stores it in its data
buffer, just as it did before.
The DMA controller sends a command to the disk controller requesting a
byte of data.
28
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
Controller
Memory
buffer
a
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 a
byte of data.
4. The disk controller sends a byte of data to the dma controller
29
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
a
1.
Controller
Memory
buffer
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 a
byte of data.
4. The disk controller sends a byte of data to the dma controller
30
5. The dma controller writes the byte to memory.
operating
systems
Using Direct Memory
Access
CPU
DMA Controller
Controller
Memory
buffer
int
ack
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.
31
operating
systems
Memory-Mapped I/O
Primary
memory
memory
addresses
Traditional approach
uses special I/O
instructions to get
at device registers
device 0
copy_in R3, 0x012, 4
device 1
device
addresses
Copy the contents of register
4 in the device at address 012
into cpu register 3
device 2
32
operating
systems
Memory-Mapped I/O
Primary
memory
device 0
device 1
With memory mapped I/O no
special instructions are needed.
Device registers are mapped by
the hardware into memory
addresses.
memory
addresses
Load R3, 0xFFF0124
Load register 3 from memory
Address 0xFFF0124
device 2
33
operating
systems
Single Bus Architecture
device
device
memory
cpu
bus
All addresses and data go on the same bus
34
operating
systems
Multiple Bus Architecture
The cpu uses one bus to access memory
device
device
memory
cpu
bus
All device addresses and data go on another bus
35
operating
systems
Polling vs. Interrupts
36
Polling
operating
systems
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.
Instruction
Register
Accumulator
Data lines
Status
Register
Status lines
Control lines and address lines
37
Example
operating
systems
print instruction
device address
character to
be printed
Instruction
print
Register
Accumulator
character
Data lines
Status
Register
Status lines
Control lines and address lines
38
Polling
operating
systems
Load character
into accumulator
Clear the
status register
If the printer can print 100 characters
a second, then it takes 10ms to print
each character. Thus this polling loop
runs for 10 ms between each character.
What is wrong
with this approach ?
Issue Write
Command
Load character
into accumulator
yes
Is the
printer
done?
no
39
An example of a device driver using polling
operating
systems
User Program
1.
Application issues a read( ) call.
The OS blocks the application.
User
Space
Device Interface (API)
Direct I/O with Polling
Operating System
Device
Driver
Kernel
Space
Device
Controller
40
operating
systems
User
Space
BLOCKED
User Program
Device Interface (API)
Direct I/O with Polling
2. System
Operating
Kernel
Space
Device
Driver
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.
Device
Controller
41
operating
systems
User
Space
BLOCKED
User Program
Device Interface (API)
Kernel
Space
Operating
2. System
Device
Driver
3. The driver stores a command in the command
register of the device controller.
Direct I/O with Polling
Device
Controller
42
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct I/O with Polling
Operating
2. System
Device
Driver
Device
Controller
4. The controller reads from the device.
Since this involves mechanical parts,
the read can take some time.
43
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct I/O with Polling
Operating
2. System
Device
Driver
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.
Device
Controller
44
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct I/O with Polling
Operating
2. System
Device
Driver
7. The device driver reads the data register(s)
in the device controller to get the data that
has been read from the device.
Device
Controller
45
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct I/O with Polling
Operating
2. System
Device
Driver
8. The device driver copies the data into
user space. The OS unblocks the application
program.
Device
Controller
46
Interrupts
operating
systems
Handling interrupts on machines that do look-ahead or on superscalar machines is quite complicated. This discussion is simplified.
device
Interrupt
controller
cpu
cpu
memory
bus
Device finishes it The
operation.
Interrupt controller
It writes its statusissues
to the the interrupt to the
Interrupt controllerCPU. The interrupt includes
The CPU
stops whatever
it is doing,
a number
which is used
to
and executes
thean
interrupt
handler
index into
interrupt
table.
pointed to by the interrupt.
47
A Device Driver Using Interrupts
operating
systems
User
Space
Kernel
Space
User Program
1.
Application issues a read( ) call.
The OS blocks the application.
Device Interface (API)
Direct
I/O
Operating
Systemwith
Interrupts
Device
Driver
Device
Controller
48
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct
I/O
Operating
Systemwith
Interrupts
2. The device driver queries
Device
Driver
status registers in the device controller to
see if the device is busy. If it is, it waits.
Device
Controller
49
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct
I/O
Operating
Systemwith
3. When the device is ready, the
Interrupts
device driver stores a command in the
Device
Driver
command register of the device controller.
This starts the device.
Device
Controller
50
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Device Interface (API)
Direct
I/O
Operating
Systemwith
3.
The driver then issues a wait().
Interrupts
Device
Driver
Device
Controller
wait means “I will voluntarily give up the cpu
and wait for an interrupt to occur.
51
operating
systems
User
Space
Kernel
Space
BLOCKED
User Program
Another
Process
Device Interface (API)
Direct
I/O
Operating
Systemwith
Interrupts
4. The OS schedules another process.
Device
Driver
BLOCKED
Device
Controller
5. Meanwhile, the device controller reads
from the device. Some other process is
using the CPU
52
operating
systems User
BLOCKED
User
Program
Space
Kernel
Space
Device Interface (API)
Direct
I/O
Operating
Systemwith
Interrupts
Device
Driver
BLOCKED
Device
Controller
signal
Interrupt
Handler
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.
53
operating
systems
User
Space
Kernel
Space
User Program
Device Interface (API)
Direct
I/O
Operating
Systemwith
Interrupts
Device
Driver
Device
Controller
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.
54
Example
operating
systems
process
print
user
space
Text to print
user process makes
a print request
kernel
space
55
Code executed when the system call is made
Prints the first
character
copy (userBuffer, kernelBuffer, count);
enable interrupts;
while (printerStatusReg != READY);
printerDataReg = kernelBuffer[0];
run scheduler; // Block the user and run some other process
56
Example
operating
systems
user
space
kernel
space
process
The user process is blocked.
process
Another user process
is scheduled to run.
Text to print
the print data is
transferred
to the kernel.
57
Example
operating
systems
process
user
space
kernel
space
process
while the printer works,
this process can run.
Text to print
the first character is
sent to the printer
58
Example
operating
systems
process
user
space
kernel
space
process
This process is interrupted.
Text to print
The printer sends an interrupt.
It is ready for another character.
59
Example
operating
systems
process
user
space
kernel
space
process
while the printer works,
this process can run again.
Text to print
the next character is
sent to the printer
60
Example
operating
systems
process
user
space
process
kernel
space
When all of the data has been
printed, the process is unblocked.
It will now be scheduled to run.
61
operating
systems
Interrupt Handlers
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
62
operating
systems
Handling an Interrupt
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
63
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;
64
operating
systemsUser
Space
Kernel
Space
User Program
read( device_x )
Trap Table
Device Interface (API)
Device Manager Design
Operating System
read( deviceID, …)
{
switch(deviceID)
{
case dev0:
dev0_read( … );
break;
…
case dev1:
dev1_read( …);
break;
read( device_x, … )
This design
requires that the
kernel be recompiled
every time a new
device is added.
65
operating
systems
open( )
read( )
write( )
ioctl( )
Device 01
Reconfigurable Drivers
Device 02
Device 03
Device 04
…
When a new device is added, fill in entries in the table using a device
“Registration” procedure.
66