Slides for week 11

advertisement
Outline





Principles of I/O hardware
Principles of I/O software
I/O software layers
Disks
Clocks
1
Types of Disks

Magnetic disks





Hard disks and floppy disks
Reads/writes are equally fast
Ideal secondary memory
Highly reliable storage
Optical disks


CD-ROM, CD-R: 600MB
DVD: 4.7-17GB
2
Structure of Magnetic Disks
Read/write head (1 per surface)
Cylinder
Surface 1
Direction of arm motion
Surface 0
Sector
1. Cylinders  tracks  sectors
2. Seek time and rotational delay
Track
Cost of Read / Write A Disk Block

Seek time




Rotational delay



The time to move the arm to the proper cylinder
Dominate the other two times for most disks
E.g., 0.8 msec for adjacent cylinders
The time for the proper sector to rotate under the
head
E.g, 0.03 msec for adjacent sectors
Actual data transfer time

E.g., 17 sec for one sector
4
Specifications of Disks

Geometry



Physical geometry: more sectors in outer
cylinders
To OS: same number of sectors for all cylinders:
x cylinders, y heads, and z sectors.
Logical block addressing

Disk sectors are numbered consecutively from 0
to some maximum value
5
Disk Formatting
Preamble
Data
ECC
Gap
A disk sector
•
Preamble: recognize the start of the sector. It
also contains the cylinder and sector numbers.
•
Data: most disks use 512-byte sectors
•
ECC: can be used to recover from read errors
•
Gap between sectors
6
Cylinder Skew


The position of sector 0 on each track is offset
from the previous track. This offset is called
cylinder skew.
Allow the disk to read multiple tracks in one
continuous operation without losing data
Direction of
disk rotation
0
3
3
0
1
2
2
3
2
1
No skew
Direction of
disk rotation
3
0
1
2
1
0
1-sector skew
7
Sector Interleaving
• Consider a controller with one sector buffer. A
request of reading two consecutive sectors.
When the controller is busy with transferring one
sector of data to memory, the next sector will fly
by the head.
• Solution: sector interleaving
6
7
0
3
1
5
4 3
2
No interleaving
7
0
4
6
2 5
1
Single interleaving
2
5
0
3
7
4 1
6
Double interleaving
Disk Arm Scheduling Algorithm

Time required to read/write a disk block





Seek time
Rotational delay
Actual data transfer time
For most disks, the seek time dominates
the other two times.
How to reduce the mean seek time?

Assume we know the cylinder numbers of
all the pending requests.
9
Optimize Seek Time

Fist-come, first-served:



Accept requests one at a time and carry them out
in that order
Little can be done
Shortest seek first



Handle the closest request next
The arm will tend to oscillate between two
cylinders
Starvation: with a heavily loaded disk, requests for
cylinders at either extreme get poor service
10
Elevator Algorithm

Keep moving the arm in the same
direction until there are no more
outstanding requests in that direction,
then switch the direction.

Upper bound: twice of the number of
cylinders
11
An Example
Initial position
Request order
1
5
07
2
Cylinder Number
1
6
8 9
13
3
64
16 18 19
FCFS: cylinder 811316196189, total 59 motions
SSF: cylinder 896113161819, total 27 motions
EA: cylinder 891316181961, total 29 motions
Assume the direction is initially UP.
Stable Storage Model



No disk works all the time with no errors
Achievable: either correctly writes the
data or does nothing, leaving the
existing data intact.
Possible errors (model assumptions):


Incorrect write: detected by ECC
Correctly written can go bad. However,
ignore the chance that two independent
drives have the same sector going bad
13
Model

Hardware: a pair of identical disks



Corresponding blocks works together to
form one error-free block
At any time, for any pair of blocks, at least
one is in good condition.
Target:


If no error, the corresponding blocks on
both drives are the same
If error occurs, at least one block can
provide the correct data (either new or old)
14
Model: Stable Writes/Reads

Stable writes

Write the block in drive 1read and verify



Keep writing until it successes
Write the block in drive 2read and verify
Stable reads

Read the block from drive 1. If incorrect (detected
by examining ECC), the corresponding block in
drive 2 must contain the correct data. Why?


Stable writes leave two good copies
Both copies go bad: impossible (assumption 2)
15
Model: Crash Recovery

Compare corresponding blocks



Disk
2
1
Old
Crash
Old
Both good: nothing is done
One is bad: overwrite the bad copy
Both good but different: block 1 overwrites
block2. Why?
Disk
2
1
Bad
Old
Crash
Disk
2
1
New
Old
Crash
Disk
2
1
New Bad
Crash
Disk
2
1
New
New
Crash
16
Outline





Principles of I/O hardware
Principles of I/O software
I/O software layers
Disks
Clocks
17
Clocks

Essential for multi-programming system



Maintain the time of day
Prevent processes from monopolizing the CPU
Hardware components:



Crystal oscillator: generate high frequency periodic
signal, typically several hundred MHz
Counter: value is decreased by 1 when a signal is
generated. It causes a CPU interrupt when it gets
to zero.
Holding register: programmable. Used to control
the frequency of interrupt.
18
Operation Modes

One-shot mode





Copy the value of the holding register into the
counter
Decrease the counter at each pulse (signal)
Cause an interrupt if it gets zero
Stop
Square-wave mode

After causing the interrupt, the holding register is
automatically copied into the counter, and the
whole process is repeated again.
19
Summary

Hardware Principle




Device controller: between devices and OS
Memory mapped I/O Vs. I/O port number
DMA Vs. Interrupt
Software Principle



Programmed I/O: waste CPU time
Interrupts: overheads
DMA: DMA controller may not drive the
device at full speed
20
Summary (Cont.)

Four layers of I/O software




Interrupt handlers: context switch, wake up
driver when I/O completed
Device drivers: set up device registers,
issue commands, check status and errors
Device-independent software: naming,
protection, buffering, allocating
User-space software: make I/O call, format
I/O, spooling
21
Summary (Cont.)

Disks




Structure: cylindertracksector
Optimize seek time: elevator algorithm
Stable storage
Clocks

One-shot and square-wave modes
22
CMPT 300 Operating System I
Chapter 6
File Systems
Outline



Files
Directories
File system implementation
24
Store Information in Memory?

How to store a very large amount of
information?


How to survive the termination of processes?


All account info in a bank >> main memory size
Store your program when the PC is turned off
How to share information among processes


Share airline reservation info among multiple
processes
Share information around the world?
25
Files: Long-term Storages

Files: info on disk/other external media


File system: the part of OS dealing with
files


Persistent, huge capacity, shared by
multiple processes
Structure, name, access, protection, …
User interface of file systems



How do files appear to users?
Naming rules and protection
File operations
26
File Naming

Strings as file names

Letters, digits





Special characters are also permitted in some system
File name length, usually less than 255
Case sensitive?
Processes access files using file names
File name extensions: file_stem.extension

Indicates the type of a file


Register extensions with programs


E.g., code.c, code.o, code.c.Z
Specify a program to open files with a specific extension
Convention & Enforcement
27
File Structure

An unstructured sequence of bytes



A sequence of fixed-length records




Most widely used, e.g., UNIX and Windows
User programs impose meaning of files
Records have internal structure
Read/write in records
Not used in any current general-purpose system
A tree of records


Search records by keys
Used on some large mainframe computers
28
1 byte
Examples of File Structures
1 record
Byte sequence
Name: Jack
SIN
Address
Account #
Name: Carole
SIN
Address
Account #
Name: Nicola
SIN
Address
Account #
Name: Andrew
SIN
Address
Account #
Ant
Cat
Cow
Dog
Hen
Fox
Goat
Ibis
Pig
Lion
Owl
Lamb
Tree
Record sequence
29
File Types

Regular files – store user’s information

ASCII files (text file): lines of text



Binary files: binary streams



Internal structure know to programs
Object file, executable code. Word file?
Directories – maintained by system


Can be displayed and printed as is.
E.g., source code file
Maintaining the structure of the file system
UNIX special files: modeling I/O devices


Character special files: serial I/O devices
Block special files: disks/block devices
30
File Access

Sequential access



Random access files




Read all the bytes in order from the beginning
Rewind if read again
Read the bytes/records by specifying positions
Applications: database, etc.
All the files are random access nowadays
How to specify the starting point for reading

Use seek operation to set the current position

Roll forward/backward for n bytes
31
File Attributes
File protection
and access
Flags
control/enable
some specific
property
Used in file with
records having a key
Time stamps
Size
Attribute
Meaning
Protection
Who can access the file and in what way
Password
Password needed to access the file
Creator
ID of the person who created the file
Owner
Current owner
Read-only flag
Read/write or read only
Hidden flag
Normal file or the file does not display in
listings
System flag
Normal file or system file
Archive flag
The file has been backed up or not
Random access flag
Sequential access only or random access
ASCII/binary flag
ASCII file or binary file
Temporary flag
Normal file or file will be deleted on process
exit
Lock flags
Unlocked or locked
Record length
Number of bytes in a record
Key position
Offset of the key within each record
Key length
Number of bytes in the key field
Creation time
Data and time the file was created
Time of last access
Date and time the file was last accessed
Time of last
change
Date and time the file was last changed
Current size
Number of bytes in the file
Maximum size
32
Number of bytes the file may grow to
File Operations


Store and retrieve information
System calls relating to files

Open: a process must open a file first before using
it



Read: read data starting from the current position



Open mode: read, write, append, modify, etc
Close it after you use it
Write data
Seek: specify where to take the data
Other calls


Delete, append, get attributes, set attributes, rename
How to copy a file using these system calls?
33
Outline



Files
Directories
File system implementation
34
Organization: Single-level / Two-level

Single-level: one directory for all the files



Not good for huge amount of files
Not good for multi-user system
Two-level: userdirectory


How to access a file?
A large number of files from one user,
inconvenient
root
root
User A:
File mymails
User B:
File mymails
User A
User B
File mymails
File mymails
35
Hierarchical Directory Systems

A general hierarchy: a tree of directories
User directory
root
directory
file
directory
directory
file
file
file
directory
directory
directory
file
file
file
36
Path Names


Mechanism to locate files
Absolute path name



Path starting from the root directory
E.g., /usr/fran/mailbox. ‘/’ is path separator
Relative path name

Relative to the current working directory



E.g., if working directory is /usr/fran, then
/usr/fran/mailbox = mailbox
Each process has its own working directory
Current directory “.” and parent directory “..”

E.g., ../cindy/mailbox, ./mailbox
37
Directory Operations (UNIX)

Create: a directory is created



Link (hard link)



Empty except ‘.’ and “..” entries
Delete, rename a directory
Allow a file to appear in more than one directory
One copy of a file, multiple directory entries
Unlink


A directory entry is removed
Link count > 0


Yes: remove the file (free the i-node and data blocks)
No: keep the file
38
Outline



Files
Directories
File system implementation
39
File System Layout

Disk is divided up into several partitions


Each partition has one file system
MBR – master boot record


boot the computer & contain the partition table
Partition table



Starting & ending addresses of each partition
One partition is marked as active
Within each partition


Boot block – first block, a program loads the OS
Superblock – key parameters about the file sys.
MBR
Partition 1
Boot block
Super block
Partition 2
Free space mgmt
Partition 3
I-nodes
Root dir
Partition 4
Files and directories
40
Implementing Files

Key issue: how to keep track of which
disk sectors go with which file?


E.g., block size= 512B, file size=2014B, so
where are these 2014/514=4 blocks on
disk?
Many methods




Contiguous allocation
Linked list allocation
I-nodes
Each one has its own pros and cons
41
Contiguous Allocation


Store each file as a contiguous run of disk
sectors
Advantages

Simple to implement



Each file has two numbers, starting address & length
Read performance is excellent
Drawback


Holes.
Relocate if file grows.
Free blocks
42
Linked List Allocation

Keep all the blocks as a linked list




Only the address of the first block is stored
Every sector can be used, no holes
Random access is extremely slow
The amount of data storage in a sector is no
longer a power of two

Read a block of data requires accessing two
sectors
0
File
block
0
Physical sector 4
File
block
1
File
block
2
File
block
3
7
2
10
43
File Allocation Table (FAT)

Move the pointer from each block to a table in
memory





Random access is much easier


4
First block
FAT is in main memory
FAT must be held in main memory



FAT– shared by all the files
0
Indexed by physical sector number
1
Each entry contains the number of next block2
3
E.g., 47210
20 GB disk, 1 KB/block, 4 bytes/entry
80MB FAT
Size of FAT is linear to size of disk
10
7
5
6
7
2
8
9
10
-1
11
12
13
14
15
44
Index Nodes (I-nodes)


An i-node lists the attributes and disk
addresses of the file’s blocks
Only when a file is open, its i-nodes
should be loaded into memory


Much smaller than FAT
Irrelevant to size of disk
File attributes
Address of disk block 0
Address of disk block 1
Address of disk block 2
Address of disk block 3
Address of block of pointers
Disk block
containing
additional disk
addresses
45
Implementing Directories

Directory system: map the ASCII file name
onto the info needed to locate the data


Directory entry
Where are the attributes stored?


In the directory entry (MS-DOS/Windows)
In the i-nodes (UNIX)
Games
Attributes
Games
i-node
Mail
Attributes
Mail
File attributes
News
Attributes
News
Work
Attributes
Work
DOS/Windows
Address of disk block 0
Address of disk block 1
…
UNIX
46
Locate A File: /usr/ast/mbox
root
1
.
1
..
4
bin
7
dev
14 lib
9
etc
6
usr
8
tmp
Looking up
usr yields inode 6
I-node 6 Block
is for
132 is
/usr
/usr dir.
Attr.
132
…..
I-node
26 is for
/usr/ast
26
.
6
..
64
grants
92
books
60
mbox
81
simix
17
src
6
.
Attr.
1
..
406
…..
19 dick
30 erik
/usr is
in
block
132
51 jim
26 ast
45 bal
/usr/ast is
Block 406
is /usr/ast
dir.
/usr/ast
is in
block
406
/usr/ast/mbox
is i-node 60
i-node 26
47
Download