Input and Output (IO)

advertisement
Input and Output (IO)






In addition to processing the job (using CPU and
memory), IO is the main job of a computer system
Input and output are typically carried out by
peripheral devices, managed by the IO subsystem
of the kernel
Every device has a corresponding device driver
that manages the interface (IF) between device
and kernel
All device drivers are typically implemented with a
single interface that the kernel understands
(syscalls?)
Operations: open, close, initialize, read, write…
Typically the device manufacturer provides the IF
Chap 0
Hardware
monitor
CPU
cache
cache
graphics
controller
system bus
IDE Disk
Controller
disk 1
...
disk n
Chap 0
Communicating with a Device


The device driver (DD) has to communicate with
the device to give it commands and receive
feedback
So-called IO-instructions read/write data to an IOport


Another way of doing it is using memory-mapped
IO


usually a register communicates directly with the device
(i.e., whenever a write is done on an IO port, the device
subsequently receives the data)
same idea, but the communication is done through
specific (predefined) memory locations
Some systems use both (for example, a monitor has IOports for control and memory-mapped IO for large data
transfers)
Chap 0
Communication (cont)

Two ways of CPU-device communication:

Polling: the device is asked (polled) whether it has data
1- host checks the status register for that device
2- host tells the device to “go ahead”
3- device writes data/command to the IO-port


allows CPU to control how/when it interacts with device,
but large overhead if it has to poll repeatedly
Interrupts: the device takes the initiative
1- the device interrupts CPU to pass on data/commands
2- CPU does a context switch to process the interrupt
3- CPU returns from interrupt and resumes process

CPU doesn’t waste time polling, but has no control of
when device will interrupt
Chap 0
Real-Time Systems Device
Communication

In real-time systems (RTSs), due to deadlines,
devices are usually not allowed to interrupt when
they want




a mal-functioning device could bring down the system
In RTSs, many tasks are periodic (reading
sensors, sending commands to actuators)
If polling is used, need to guarantee that IO will be
done in a timely fashion
If interrupts are used, need to guarantee that they
will not violate the deadline guarantees given to
processes
Chap 0
Handling Interrupts

Interrupt controller hardware provides ability to :
defer interrupts
 call the proper interrupt service routine (ISR)
 distinguish and prioritize between high- and low-priority
interrupts


In addition, processors can (and do!) mask
interrupts
disallow some interrupts to occur to process other stuff
 however, there is the non-maskable interrupt
(emergency)

Chap 0
More on Interrupts


Interrupt vectors are very common: keep the
address of the ISR in a fixed location, the
hardware will read the number of the interrupt and
jump to the appropriate location
The interrupt handler will not have its own stack.
However, it will execute on top of the kernel stack,
not in the user stack

7
This is because there is little control over the size of the
user stack and the ISR may run out of memory
Interacting with Devices


Sometimes a mixture of interrupts and IO using
direct memory access (DMA) is beneficial.
A disk that has to read large data block to position X:
receives the request, reads the data
 asks the DMA controller to put it in the memory location X
 and then, finally, interrupts the CPU



The DMA controller and the disk exchange
information (a protocol) to be able to do this transfer
In some architectures this is called cycle stealing,
since the bus is used to transfer data into the
memory and thus the CPU cannot use the memory in
those cycles
Chap 0
Kernel IO Subsystem





The users want to use the devices.
How to achieve it?
The user uses a library that invokes
system calls in the OS.
The system calls get translated into
device driver requests
The DDs request service from the IO
controllers, who talk to the device
When the service is done, the
controller sends an interrupt, to
signal CPU
user
OS
DD
ISR
controller
device
Chap 0
Disk as a case study

A disk drive has several physical components
 spindle
 surface (one side in the pack)
 read/write
 cylinder
arm and head
(or track)
 sector
Chap 0
Accessing the Disk





A disk is accessed through the library, file system,
DevDriver and disk controller
The calls to the controller are called disk drive
commands, such as drive select, head select,
direction, read/write, data out, etc
The disk controller does the synchronization
between disk and OS, signaling/timing, some error
control
Several users may request data to/from the disk at
once
The file system is the first entity to recognize it
and synchronize access to the disk
Chap 0
Accessing the Disk (cont)



The FS does the scheduling of the requests, that is, it
determines the order in which the requests are
serviced
The FS and DD also use buffering for synchronization
(support speed/data size mismatch between OSdevice)
The user, FS, and DD have different ideas (abstraction)
of how the file looks like.
user: contiguous space, byte by byte
 file system: blocks of data, with logical addresses
 DD: sectors in disk, in specific hardware addresses (typically
consists of < cyl, track, sect >

Chap 0
Accessing the Disk (cont)





The translations take place each time a new
interface is crossed
The file-relative logical address <filename,offset>
The volume-relative logical address <sector,offset>
The drive-relative physical address <cyl,track,sect>
Some disks have also a zone, and the physical
address becomes < cyl, zone, track, sect >

This is because the length of outer tracks /cylinders
compared with inner tracks/cylinders (density)
Chap 0
Accessing the Disk (cont)


When we need to access a sector, the disk head
needs to be positioned over that sector (or more
accurately, the disk must rotate until the sector is
under the head)
For that, several delays are involved in the disk
operation, in decreasing time
seek delay: position HEAD on correct track/cylinder
 rotational delay: position correct SECTOR under head
 transfer delay: transfer data to/from memory


Therefore, it is important to minimize the highest
delay, namely, the seek delay
Chap 0
Improve Disk Speed


Increase buffering in the device driver
and/or disk controller (this is a type of
“cache”)
Reduce rotational delay:
 place
blocks of the same file in the same
cylinder
 read the blocks in the appropriate order
 allow block interleaving
1
(clearly, the number of sectors
4
in the disk should be odd,
2
unlike the drawing)
3
Chap 0
Interleaving


Interleaving is done in the device driver, since it
will have to tell the controller where to place
sectors in the disk. Clearly, interleaving is tightly
coupled with the device itself
The device decides the interleaving degree (how
many sectors to skip over), which is tightly
coupled with the speeds of the disk
Rotational
 Seek (or arm speed)
 Transfer

Chap 0
Improve Disk Speed (cont)

To decrease seek delays one can:
increase the number of heads
 park the head in the middle track of the disk to
decrease the average seek delay
 place the data in the appropriate locations (tracks)


The organ pipe distribution does the last trick spindle
create a histogram of the disk block usage (count
the number of times that disk blocks are used)
 place most used blocks in the middle track

Chap 0
Improve Disk Speed (cont)


RAID: Redundant Array of Inexpensive Disks
Allows for more parallelism when retrieving data
store each part of a block in a separate disk, and allow
all sub-blocks to be retrieved in parallel
 If disks are homogeneous and synchronized, even
better performance can be achieved
 If disks are heterogeneous or not synchronized,
performance is worse since it depends on the slowest of
the disks, or the one off phase.

 can
use slower, cheaper disks
Chap 0
Possible File Structures


None - sequence of words or bytes
Simple record structure
Lines
 Fixed length
 Variable length


Complex Structures
Formatted document
 Relocatable load file



Can simulate last two with first method by
inserting appropriate control characters.
Who decides:
Operating system
 Program

19
File Attributes







20
Name – only data kept in human-readable form.
Type – needed for supporting different file types.
Location – pointer to file location on device.
Size – current file size.
Protection – controls who can do reading, writing,
executing, access, etc.
Time, date, and user identification – data for
protection, security, and usage monitoring.
Information about files are kept in the directory
structure, which is maintained on the disk.
File Operations








21
create
write
read
file seek: reposition within file
delete
truncate
open(Fi) – search the directory structure on disk
for entry Fi, and move the content of entry to
memory.
close (Fi) – move the content of entry Fi in memory
to directory structure on disk.
Directory Structure
A collection of nodes containing information
about all files.

Directory
Files


22
F1
F2
F3
F4
Fn
Both the directory structure and the files
reside on disk.
Backups of these two structures are kept on
tapes.
Information in a Device Directory









23
Name
Type
Address
Current length
Maximum length
Date last accessed (for archival)
Date last updated
Owner ID
Protection information
Operations Performed on a Directory






24
Search for a file
Create a file
Delete a file
List a directory
Rename a file
Traverse the file system
Logical Directory Organization

Goals:
– locating a file quickly.
 Naming – convenient to users.
 Efficiency
 Two
users can have same name for different
files.
 The same file can have several different names.
– logical grouping of files by
properties, (e.g., all Pascal programs, all
games, …)
 Grouping
25
Single-Level Directory



26
A single directory for all users.
Naming problem
Grouping problem
Two-Level Directory




27

Separate (flat) directory for each user.
Path name
Can have the same file name for different
user
Efficient searching
No grouping capability
Tree-Structured Directories



Efficient searching
Grouping Capability
Current directory (working directory)
cd /spell/mail/prog
 type list

28
Tree-Structured Directories (Cont.)



Absolute or relative path name
Concept of current working directory
Creating/deleting a new file/subdirectory is done
in current directory.
mail
prog

29
copy prt exp count
Example: Deleting “mail”  deleting the entire
subtree rooted by “mail”.
Acyclic-Graph Directories



Have shared subdirectories and files.
Two different names (aliasing)
If dict deletes count  dangling pointer.
Solutions:
Backpointers, so we can delete all pointers.
 Entry-hold-count solution.

30
Protection

File owner/creator should be able to control:
what can be done
 by whom


Types of access
Read
 Write
 Execute
 Append
 Delete
 List

31
Classical Unix: Access Lists & Groups


Mode of access: read, write, execute
Three classes of users

a) owner access
7
b) groups access
6

1 1 0
c) public access
1

0 0 1
owner
chmod


32
R W X
1 1 1
group
761
public
game
SysAdmin creates a group (unique name),
say G, and add users to the group.
Attach a group to a file
chgrp G game
AFS (Andrew) Access Control Lists





More details, more information
For each file, maintain a list of users and
their access capabilities
More costly, due to more checking
More flexibility, more degrees of sharing
(read, list dir, admin, write, insert, delete)
ACLs are easy to use, and render Unix
protection bits useless
 dangers
of different user interfaces (the unix
bits are still listed in “ls”)
33
Related documents
Download