480 likes | 701 Views
Outline. Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks. 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.
E N D
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks • Clocks
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
Read/write head (1 per surface) Surface 1 Direction of arm motion Surface 0 Structure of Magnetic Disks Cylinder Sector 1. Cylinders tracks sectors 2. Seek time and rotational delay Track
Cost of Read / Write A Disk Block • Seek time • 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 • Rotational delay • 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
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
Disk Formatting 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
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 Direction of disk rotation 3 0 0 0 2 3 3 3 1 0 1 1 2 2 1 2 No skew 1-sector skew
5 0 7 0 7 0 2 3 6 3 4 1 7 6 5 6 1 2 4 1 2 5 4 3 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 No interleaving Single interleaving 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.
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
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
Initial position An Example Request order 1 19 9 16 Cylinder Number 6 8 13 18 FCFS: cylinder 811316196189, total 59 motions SSF: cylinder 896113161819, total 27 motions EA: cylinder 891316181961, 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
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)
Model: Stable Writes/Reads • Stable writes • Write the block in drive 1read and verify • Keep writing until it successes • Write the block in drive 2read 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)
Model: Crash Recovery • Compare corresponding blocks • Both good: nothing is done • One is bad: overwrite the bad copy • Both good but different: block 1 overwrites block2. Why? Disk Disk Disk Disk Disk 2 2 2 1 2 1 1 2 1 1 New Bad New Old Old Old Bad Old New New Crash Crash Crash Crash Crash
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks • Clocks
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.
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.
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
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
Summary (Cont.) • Disks • Structure: cylindertracksector • Optimize seek time: elevator algorithm • Stable storage • Clocks • One-shot and square-wave modes
CMPT 300 Operating System I Chapter 6 File Systems
Outline • Files • Directories • File system implementation
Store Information in Memory? • How to store a very large amount of information? • All account info in a bank >> main memory size • How to survive the termination of processes? • 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?
Files: Long-term Storages • Files: info on disk/other external media • Persistent, huge capacity, shared by multiple processes • File system: the part of OS dealing with files • Structure, name, access, protection, … • User interface of file systems • How do files appear to users? • Naming rules and protection • File operations
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 • E.g., code.c, code.o, code.c.Z • Register extensions with programs • Specify a program to open files with a specific extension • Convention & Enforcement
File Structure • An unstructured sequence of bytes • Most widely used, e.g., UNIX and Windows • User programs impose meaning of files • A sequence of fixed-length records • 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
Examples of File Structures 1 byte 1 record Tree Byte sequence Record sequence
File Types • Regular files – store user’s information • ASCII files (text file): lines of text • Can be displayed and printed as is. • E.g., source code file • Binary files: binary streams • Internal structure know to programs • Object file, executable code. Word file? • Directories – maintained by system • 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
File Access • Sequential access • Read all the bytes in order from the beginning • Rewind if read again • Random access files • 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
File Attributes File protection and access Flags control/enable some specific property Used in file with records having a key Time stamps Size
File Operations • Store and retrieve information • System calls relating to files • Open: a process must open a file first before using it • Open mode: read, write, append, modify, etc • Close it after you use it • Read: read data starting from the current position • 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?
Outline • Files • Directories • File system implementation
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: userdirectory • How to access a file? • A large number of files from one user, inconvenient root root User A User B User A: File mymails User B: File mymails File mymails File mymails
Hierarchical Directory Systems • A general hierarchy: a tree of directories User directory root directory directory directory file file file directory directory directory file file file file
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
Directory Operations (UNIX) • Create: a directory is created • Empty except ‘.’ and “..” entries • Delete, rename a directory • Link (hard link) • 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
Outline • Files • Directories • File system implementation
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.
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
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
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 Physical sector 4 7 2 10
File Allocation Table (FAT) • Move the pointer from each block to a table in memory • FAT– shared by all the files • Indexed by physical sector number • Each entry contains the number of next block • E.g., 47210 • Random access is much easier • FAT is in main memory • FAT must be held in main memory • 20 GB disk, 1 KB/block, 4 bytes/entry • 80MB FAT • Size of FAT is linear to size of disk First block
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 Disk block containing additional disk addresses
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) i-node DOS/Windows UNIX
Locate A File: /usr/ast/mbox Block 406 is /usr/ast dir. I-node 6 is for /usr I-node 26 is for /usr/ast root Block 132 is /usr dir. /usr/ast is in block 406 /usr is in block 132 /usr/ast/mbox is i-node 60 Looking up usr yields i-node 6 /usr/ast is i-node 26