text
stringlengths
1
7.76k
source
stringlengths
17
81
284 FILE SYSTEMS CHAP. 4 would take hours or even days with large disks. As a result, the disk ultimately consists of files and holes, as illustrated in the figure. Initially, this fragmentation is not a problem, since each new file can be written at the end of disk, following the previous one. However, eventually the ...
clipped_os_Page_284_Chunk6501
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 285 File A Physical block Physical block 4 0 7 2 10 12 File block 0 File block 1 File block 2 File block 3 File block 4 File B 0 6 3 11 14 File block 0 File block 1 File block 2 File block 3 Figure 4-11. Storing a file as a linked list of disk blocks. Unlike contiguous allocation, ev...
clipped_os_Page_285_Chunk6502
286 FILE SYSTEMS CHAP. 4 Physical block File A starts here File B starts here Unused block 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 10 11 7 3 2 12 14 -1 -1 Figure 4-12. Linked-list allocation using a file-allocation table in main memory. Using this organization, the entire block is available for data. Furthermore, ran- do...
clipped_os_Page_286_Chunk6503
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 287 The big advantage of this scheme over linked files using an in-memory table is that the i-node need be in memory only when the corresponding file is open. If each i- node occupies n bytes and a maximum of k files may be open at once, the total memory occupied by the array holding...
clipped_os_Page_287_Chunk6504
288 FILE SYSTEMS CHAP. 4 4.3.3 Implementing Directories Before a file can be read, it must be opened. When a file is opened, the operat- ing system uses the path name supplied by the user to locate the directory entry on the disk. The directory entry provides the information needed to find the disk blocks. Depending on...
clipped_os_Page_288_Chunk6505
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 289 The simplest approach is to set a limit on file-name length, typically 255 char- acters, and then use one of the designs of Fig. 4-14 with 255 characters reserved for each file name. This approach is simple, but wastes a great deal of directory space, since few files have such lo...
clipped_os_Page_289_Chunk6506
290 FILE SYSTEMS CHAP. 4 only now compacting the directory is feasible because it is entirely in memory. An- other problem is that a single directory entry may span multiple pages, so a page fault may occur while reading a file name. Another way to handle variable-length names is to make the directory entries themselve...
clipped_os_Page_290_Chunk6507
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 291 link. The file system itself is now a Directed Acyclic Graph, or DAG, rather than a tree. Having the file system be a DAG complicates maintenance, but such is life. Root directory B B B C C C C A B C B ? C C C A Shared file Figure 4-16. File system containing a shared file. Shari...
clipped_os_Page_291_Chunk6508
292 FILE SYSTEMS CHAP. 4 C's directory B's directory B's directory C's directory Owner = C Count = 1 Owner = C Count = 2 Owner = C Count = 1 (a) (b) (c) Figure 4-17. (a) Situation prior to linking. (b) After the link is created. (c) After the original owner removes the file. an invalid i-node. If the i-node is later re...
clipped_os_Page_292_Chunk6509
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 293 in a directory and its subdirectories onto a tape may make multiple copies of a linked file. Furthermore, if the tape is then read into another machine, unless the dump program is clever, the linked file will be copied twice onto the disk, instead of being linked. 4.3.5 Log-Struc...
clipped_os_Page_293_Chunk6510
294 FILE SYSTEMS CHAP. 4 thus contain i-nodes, directory blocks, and data blocks, all mixed together. At the start of each segment is a segment summary, telling what can be found in the seg- ment. If the average segment can be made to be about 1 MB, almost the full band- width of the disk can be utilized. In this desig...
clipped_os_Page_294_Chunk6511
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 295 4.3.6 Journaling File Systems While log-structured file systems are an interesting idea, they are not widely used, in part due to their being highly incompatible with existing file systems. Nevertheless, one of the ideas inherent in them, robustness in the face of failure, can be...
clipped_os_Page_295_Chunk6512
296 FILE SYSTEMS CHAP. 4 To make journaling work, the logged operations must be idempotent, which means they can be repeated as often as necessary without harm. Operations such as ‘‘Update the bitmap to mark i-node k or block n as free’’ can be repeated until the cows come home with no danger. Similarly, searching a di...
clipped_os_Page_296_Chunk6513
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 297 1986), most UNIX systems have used the concept of a VFS (virtual file system) to try to integrate multiple file systems into an orderly structure. The key idea is to abstract out that part of the file system that is common to all file systems and put that code in a separate layer...
clipped_os_Page_297_Chunk6514
298 FILE SYSTEMS CHAP. 4 normally supported. These include the superblock (which describes a file system), the v-node (which describes a file), and the directory (which describes a file sys- tem directory). Each of these has associated operations (methods) that the concrete file systems must support. In addition, the V...
clipped_os_Page_298_Chunk6515
SEC. 4.3 FILE-SYSTEM IMPLEMENTATION 299 are shown in Fig. 4-19. Starting with the caller’s process number and the file de- scriptor, successively the v-node, read function pointer, and access function within the concrete file system are located. ... Process table 0 File descriptors ... V-nodes open read write Function ...
clipped_os_Page_299_Chunk6516
300 FILE SYSTEMS CHAP. 4 4.4.1 Disk-Space Management Files are normally stored on disk, so management of disk space is a major con- cern to file-system designers. Two general strategies are possible for storing an n byte file: n consecutive bytes of disk space are allocated, or the file is split up into a number of (no...
clipped_os_Page_300_Chunk6517
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 301 Length VU 1984 VU 2005 Web Length VU 1984 VU 2005 Web 1 1.79 1.38 6.67 16 KB 92.53 78.92 86.79 2 1.88 1.53 7.67 32 KB 97.21 85.87 91.65 4 2.01 1.65 8.33 64 KB 99.18 90.84 94.80 8 2.31 1.80 11.30 128 KB 99.84 93.73 96.93 16 3.32 2.15 11.46 256 KB 99.96 96.12 98.48 32 ...
clipped_os_Page_301_Chunk6518
302 FILE SYSTEMS CHAP. 4 1 KB 4 KB 16 KB 64 KB 256 KB 1MB 100% 10 20 30 40 50 60 0 80% 60% 40% 20% 0% Data rate (MB/sec) Disk space utilization Figure 4-21. The dashed curve (left-hand scale) gives the data rate of a disk. The solid curve (right-hand scale) gives the disk-space efficiency. All files are 4 KB. Hence the...
clipped_os_Page_302_Chunk6519
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 303 Keeping Track of Free Blocks Once a block size has been chosen, the next issue is how to keep track of free blocks. Two methods are widely used, as shown in Fig. 4-22. The first one con- sists of using a linked list of disk blocks, with each block holding as many fre...
clipped_os_Page_303_Chunk6520
304 FILE SYSTEMS CHAP. 4 consecutive free blocks. In the best case, a basically empty disk could be repres- ented by two numbers: the address of the first free block followed by the count of free blocks. On the other hand, if the disk becomes severely fragmented, keeping track of runs is less efficient than keeping tra...
clipped_os_Page_304_Chunk6521
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 305 (a) Disk Main memory (b) (c) Figure 4-23. (a) An almost-full block of pointers to free disk blocks in memory and three blocks of pointers on disk. (b) Result of freeing a three-block file. (c) An alternative strategy for handling the three free blocks. The shaded ent...
clipped_os_Page_305_Chunk6522
306 FILE SYSTEMS CHAP. 4 Open file table Quota table Soft block limit Hard block limit Current # of blocks # Block warnings left Soft file limit Hard file limit Current # of files # File warnings left Attributes disk addresses User = 8 Quota pointer Quota record for user 8 Figure 4-24. Quotas are kept track of on a per...
clipped_os_Page_306_Chunk6523
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 307 Most people do not think making backups of their files is worth the time and effort—until one fine day their disk abruptly dies, at which time most of them undergo a deathbed conversion. Companies, however, (usually) well understand the value of their data and genera...
clipped_os_Page_307_Chunk6524
308 FILE SYSTEMS CHAP. 4 Third, since immense amounts of data are typically dumped, it may be desir- able to compress the data before writing them to tape. However, with many com- pression algorithms, a single bad spot on the backup tape can foil the decompres- sion algorithm and make an entire file or even an entire t...
clipped_os_Page_308_Chunk6525
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 309 However, sometimes blocks go bad after formatting, in which case the operat- ing system will eventually detect them. Usually, it solves the problem by creating a ‘‘file’’ consisting of all the bad blocks—just to make sure they nev er appear in the free-block pool and...
clipped_os_Page_309_Chunk6526
310 FILE SYSTEMS CHAP. 4 1 18 19 5 6 27 7 10 20 22 30 29 23 14 11 2 3 4 8 9 12 13 15 31 28 32 24 25 26 16 17 21 File that has changed File that has not changed Root directory Directory that has not changed Figure 4-25. A file system to be dumped. The squares are directories and the cir- cles are files. The shaded items...
clipped_os_Page_310_Chunk6527
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 311 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 (d) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 (c) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3...
clipped_os_Page_311_Chunk6528
312 FILE SYSTEMS CHAP. 4 4.4.3 File-System Consistency Another area where reliability is an issue is file-system consistency. Many file systems read blocks, modify them, and write them out later. If the system crashes before all the modified blocks have been written out, the file system can be left in an inconsistent s...
clipped_os_Page_312_Chunk6529
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 313 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 2 3 4 5 6 7 8 9 101112131415 Block number Blocks in use 0 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 Free blocks (a) 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 2 3 4 5 6 7 8 9 101112131415 Blocks in use 0 0 1 0 2 0 0 0 0 1 1 0 0 0 1 1 Free blocks (c) ...
clipped_os_Page_313_Chunk6530
314 FILE SYSTEMS CHAP. 4 file system marks it as unused and releases all of its blocks. This action will result in one of the directories now pointing to an unused i-node, whose blocks may soon be assigned to other files. Again, the solution is just to force the link count in the i- node to the actual number of directo...
clipped_os_Page_314_Chunk6531
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 315 Caching The most common technique used to reduce disk accesses is the block cache or buffer cache. (Cache is pronounced ‘‘cash’’ and is derived from the French cacher, meaning to hide.) In this context, a cache is a collection of blocks that log- ically belong on the...
clipped_os_Page_315_Chunk6532
316 FILE SYSTEMS CHAP. 4 the crashes and file-system consistency discussed in the previous section. If a criti- cal block, such as an i-node block, is read into the cache and modified, but not rewritten to the disk, a crash will leave the file system in an inconsistent state. If the i-node block is put at the end of th...
clipped_os_Page_316_Chunk6533
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 317 in which all modified blocks are written back to the disk immediately are called write-through caches. They require more disk I/O than nonwrite-through caches. The difference between these two approaches can be seen when a program writes a 1-KB block full, one charac...
clipped_os_Page_317_Chunk6534
318 FILE SYSTEMS CHAP. 4 benefit of the doubt and put in sequential-access mode. However, whenever a seek is done, the bit is cleared. If sequential reads start happening again, the bit is set once again. In this way, the file system can make a reasonable guess about wheth- er it should read ahead or not. If it gets it...
clipped_os_Page_318_Chunk6535
SEC. 4.4 FILE-SYSTEM MANAGEMENT AND OPTIMIZATION 319 I-nodes are located near the start of the disk Disk is divided into cylinder groups, each with its own i-nodes (a) (b) Cylinder group Figure 4-29. (a) I-nodes placed at the start of the disk. (b) Disk divided into cyl- inder groups, each with its own blocks and i-nod...
clipped_os_Page_319_Chunk6536
320 FILE SYSTEMS CHAP. 4 more trouble than it is worth. In some systems, these are fixed-size contiguous areas anyway, so they do not have to be defragmented. The one time when their lack of mobility is a problem is when they happen to be near the end of the parti- tion and the user wants to reduce the partition size. ...
clipped_os_Page_320_Chunk6537
SEC. 4.5 EXAMPLE FILE SYSTEMS 321 file size. File names shorter than 8 + 3 characters are left justified and padded with spaces on the right, in each field separately. The Attributes field is new and con- tains bits to indicate that a file is read-only, needs to be archived, is hidden, or is a system file. Read-only fi...
clipped_os_Page_321_Chunk6538
322 FILE SYSTEMS CHAP. 4 of a misnomer, since only the low-order 28 bits of the disk addresses are used. It should have been called FAT -28, but powers of two sound so much neater. Another variant of the FAT file system is exFAT , which Microsoft introduced for large removable devices. Apple licensed exFAT , so that th...
clipped_os_Page_322_Chunk6539
SEC. 4.5 EXAMPLE FILE SYSTEMS 323 Block siz e FAT-12 FAT-16 FAT-32 0.5 KB 2 MB 1 KB 4 MB 2 KB 8 MB 128 MB 4 KB 16 MB 256 MB 1 TB 8 KB 512 MB 2 TB 16 KB 1024 MB 2 TB 32 KB 2048 MB 2 TB Figure 4-31. Maximum partition size for different block sizes. The empty boxes represent forbidden combinations. In addition to supporti...
clipped_os_Page_323_Chunk6540
324 FILE SYSTEMS CHAP. 4 characters and can contain any ASCII characters except / (because that is the sepa- rator between components in a path) and NUL (because that is used to pad out names shorter than 14 characters). NUL has the numerical value of 0. A UNIX directory entry contains one entry for each file in that d...
clipped_os_Page_324_Chunk6541
SEC. 4.5 EXAMPLE FILE SYSTEMS 325 I-node Attributes Disk addresses Single indirect block Double indirect block Triple indirect block Addresses of data blocks Figure 4-33. A UNIX i-node. an i-node from its number is straightforward, since each one has a fixed location on the disk. From this i-node, the system locates th...
clipped_os_Page_325_Chunk6542
326 FILE SYSTEMS CHAP. 4 Root directory I-node 6 is for /usr Block 132 is /usr directory I-node 26 is for /usr/ast Block 406 is /usr/ast directory Looking up usr yields i-node 6 I-node 6 says that /usr is in block 132 /usr/ast is i-node 26 /usr/ast/mbox is i-node 60 I-node 26 says that /usr/ast is in block 406 1 1 4 7 ...
clipped_os_Page_326_Chunk6543
SEC. 4.5 EXAMPLE FILE SYSTEMS 327 (although seeks across the spiral are possible). The bits along the spiral are divid- ed into logical blocks (also called logical sectors) of 2352 bytes. Some of these are for preambles, error correction, and other overhead. The payload portion of each logical block is 2048 bytes. When...
clipped_os_Page_327_Chunk6544
328 FILE SYSTEMS CHAP. 4 then people from companies whose products were big endian would have felt like second-class citizens and would not have accepted the standard. The emotional content of a CD-ROM can thus be quantified and measured exactly in kilo- bytes/hour of wasted space. The format of an ISO 9660 directory e...
clipped_os_Page_328_Chunk6545
SEC. 4.5 EXAMPLE FILE SYSTEMS 329 extension, a semicolon, and a binary version number (1 or 2 bytes). The base name and extension may use uppercase letters, the digits 0–9, and the underscore character. All other characters are forbidden to make sure that every computer can handle every file name. The base name can be ...
clipped_os_Page_329_Chunk6546
330 FILE SYSTEMS CHAP. 4 The extensions use the System use field in order to make Rock Ridge CD- ROMs readable on any computer. All the other fields retain their normal ISO 9660 meaning. Any system not aware of the Rock Ridge extensions just ignores them and sees a normal CD-ROM. The extensions are divided up into the ...
clipped_os_Page_330_Chunk6547
SEC. 4.5 EXAMPLE FILE SYSTEMS 331 Therefore Microsoft invented some extensions that were called Joliet. They were designed to allow Windows file systems to be copied to CD-ROM and then restor- ed, in precisely the same way that Rock Ridge was designed for UNIX. Virtually all programs that run under Windows and use CD-R...
clipped_os_Page_331_Chunk6548
332 FILE SYSTEMS CHAP. 4 2012; and Vrable et al., 2012). Another area that has been getting attention recently is provenance—keeping track of the history of the data, including where they came from, who owns them, and how they hav e been transformed (Ghoshal and Plale, 2013; and Sultana and Bertino, 2013). Keeping data...
clipped_os_Page_332_Chunk6549
CHAP. 4 PROBLEMS 333 3. In early UNIX systems, executable files (a.out files) began with a very specific magic number, not one chosen at random. These files began with a header, followed by the text and data segments. Why do you think a very specific number was chosen for ex- ecutable files, whereas other file types ha...
clipped_os_Page_333_Chunk6550
334 FILE SYSTEMS CHAP. 4 16. Consider the i-node shown in Fig. 4-13. If it contains 10 direct addresses and these were 8 bytes each and all disk blocks were 1024 KB, what would the largest possible file be? 17. For a giv en class, the student records are stored in a file. The records are randomly ac- cessed and updated...
clipped_os_Page_334_Chunk6551
CHAP. 4 PROBLEMS 335 27. Oliver Owl’s night job at the university computing center is to change the tapes used for overnight data backups. While waiting for each tape to complete, he works on writ- ing his thesis that proves Shakespeare’s plays were written by extraterrestrial visitors. His text processor runs on the s...
clipped_os_Page_335_Chunk6552
336 FILE SYSTEMS CHAP. 4 38. Given a disk-block size of 4 KB and block-pointer address value of 4 bytes, what is the largest file size (in bytes) that can be accessed using 10 direct addresses and one indi- rect block? 39. Files in MS-DOS have to compete for space in the FAT -16 table in memory. If one file uses k entr...
clipped_os_Page_336_Chunk6553
5 INPUT/OUTPUT In addition to providing abstractions such as processes, address spaces, and files, an operating system also controls all the computer’s I/O (Input/Output) de- vices. It must issue commands to the devices, catch interrupts, and handle errors. It should also provide an interface between the devices and th...
clipped_os_Page_337_Chunk6554
338 INPUT/OUTPUT CHAP. 5 presented to the software—the commands the hardware accepts, the functions it carries out, and the errors that can be reported back. In this book we are concerned with programming I/O devices, not designing, building, or maimtaining them, so our interest is in how the hardware is programmed, no...
clipped_os_Page_338_Chunk6555
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 339 I/O devices cover a huge range in speeds, which puts considerable pressure on the software to perform well over many orders of magnitude in data rates. Figure 5-1 shows the data rates of some common devices. Most of these devices tend to get faster as time goes on. Device Data ra...
clipped_os_Page_339_Chunk6556
340 INPUT/OUTPUT CHAP. 5 The interface between the controller and the device is often a very low-level one. A disk, for example, might be formatted with 2,000,000 sectors of 512 bytes per track. What actually comes off the drive, howev er, is a serial bit stream, start- ing with a preamble, then the 4096 bits in a sect...
clipped_os_Page_340_Chunk6557
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 341 each control register is assigned an I/O port number, an 8- or 16-bit integer. The set of all the I/O ports form the I/O port space, which is protected so that ordinary user programs cannot access it (only the operating system can). Using a special I/O instruction such as IN REG,...
clipped_os_Page_341_Chunk6558
342 INPUT/OUTPUT CHAP. 5 The x86 uses this architecture, with addresses 640K to 1M −1 being reserved for device data buffers in IBM PC compatibles, in addition to I/O ports 0 to 64K −1. How do these schemes actually work in practice? In all cases, when the CPU wants to read a word, either from memory or from an I/O por...
clipped_os_Page_342_Chunk6559
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 343 the loop given above, a fourth instruction has to be added, slightly slowing down the responsiveness of detecting an idle device. In computer design, practically everything involves trade-offs, and that is the case here, too. Memory-mapped I/O also has its disadvantages. First, m...
clipped_os_Page_343_Chunk6560
344 INPUT/OUTPUT CHAP. 5 buses. One possibility is to first send all memory references to the memory. If the memory fails to respond, then the CPU tries the other buses. This design can be made to work but requires additional hardware complexity. A second possible design is to put a snooping device on the memory bus to...
clipped_os_Page_344_Chunk6561
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 345 CPU DMA controller Disk controller Main memory Buffer 1. CPU programs the DMA controller Interrupt when done 2. DMA requests transfer to memory 3. Data transferred Bus 4. Ack Address Count Control Drive Figure 5-4. Operation of a DMA transfer. Then the controller causes an interr...
clipped_os_Page_345_Chunk6562
346 INPUT/OUTPUT CHAP. 5 use a different device controller. After each word is transferred (steps 2 through 4) in Fig. 5-4, the DMA controller decides which device to service next. It may be set up to use a round-robin algorithm, or it may have a priority scheme design to favor some devices over others. Multiple reques...
clipped_os_Page_346_Chunk6563
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 347 the controller tried to write data directly to memory, it would have to go over the system bus for each word transferred. If the bus were busy due to some other de- vice using it (e.g., in burst mode), the controller would have to wait. If the next disk word arrived before the pr...
clipped_os_Page_347_Chunk6564
348 INPUT/OUTPUT CHAP. 5 the device is just ignored for the moment. In this case it continues to assert an in- terrupt signal on the bus until it is serviced by the CPU. To handle the interrupt, the controller puts a number on the address lines speci- fying which device wants attention and asserts a signal to interrupt...
clipped_os_Page_348_Chunk6565
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 349 Precise and Imprecise Interrupts Another problem is caused by the fact that most modern CPUs are heavily pipelined and often superscalar (internally parallel). In older systems, after each instruction was finished executing, the microprogram or hardware checked to see if there wa...
clipped_os_Page_349_Chunk6566
350 INPUT/OUTPUT CHAP. 5 However, it must be clear which case applies. Often, if the interrupt is an I/O inter- rupt, the instruction will not yet have started. However, if the interrupt is really a trap or page fault, then the PC generally points to the instruction that caused the fault so it can be restarted later. T...
clipped_os_Page_350_Chunk6567
SEC. 5.1 PRINCIPLES OF I/O HARDWARE 351 point are allowed to have any noticeable effect on the machine state. Here the price is paid not in time, but in chip area and in complexity of the design. If precise in- terrupts were not required for backward compatibility purposes, this chip area would be available for larger ...
clipped_os_Page_351_Chunk6568
352 INPUT/OUTPUT CHAP. 5 are not able to deal with the problem should the upper layers be told about it. In many cases, error recovery can be done transparently at a low lev el without the upper levels even knowing about the error. Still another important issue is that of synchronous (blocking) vs. asyn- chronous (inte...
clipped_os_Page_352_Chunk6569
SEC. 5.2 PRINCIPLES OF I/O SOFTWARE 353 String to be printed User space Kernel space ABCD EFGH Printed page (a) ABCD EFGH ABCD EFGH Printed page (b) A Next (c) AB Next Figure 5-7. Steps in printing a string. The user process then acquires the printer for writing by making a system call to open it. If the printer is cur...
clipped_os_Page_353_Chunk6570
354 INPUT/OUTPUT CHAP. 5 tight loop, outputting the characters one at a time. The essential aspect of program- med I/O, clearly illustrated in this figure, is that after outputting a character, the CPU continuously polls the device to see if it is ready to accept another one. This behavior is often called polling or bu...
clipped_os_Page_354_Chunk6571
SEC. 5.2 PRINCIPLES OF I/O SOFTWARE 355 copy from user(buffer, p, count); if (count == 0) { enable interr upts( ); unblock user( ); while (*pr inter status reg != READY) ; } else { *pr inter data register = p[0]; *pr inter data register = p[i]; scheduler( ); count = count −1; i = i + 1; } acknowledge interr upt( ); ret...
clipped_os_Page_355_Chunk6572
356 INPUT/OUTPUT CHAP. 5 5.3 I/O SOFTWARE LAYERS I/O software is typically organized in four layers, as shown in Fig. 5-11. Each layer has a well-defined function to perform and a well-defined interface to the ad- jacent layers. The functionality and interfaces differ from system to system, so the discussion that follo...
clipped_os_Page_356_Chunk6573
SEC. 5.3 I/O SOFTWARE LAYERS 357 system dependent, so some of the steps listed below may not be needed on a partic- ular machine, and steps not listed may be required. Also, the steps that do occur may be in a different order on some machines. 1. Save any registers (including the PSW) that have not already been saved b...
clipped_os_Page_357_Chunk6574
358 INPUT/OUTPUT CHAP. 5 have to know all about sectors, tracks, cylinders, heads, arm motion, motor drives, head settling times, and all the other mechanics of making the disk work properly. Obviously, these drivers will be very different. Consequently, each I/O device attached to a computer needs some device-spe- cif...
clipped_os_Page_358_Chunk6575
SEC. 5.3 I/O SOFTWARE LAYERS 359 does and how it interacts with the rest of the operating system. Device drivers are normally positioned below the rest of the operating system, as is illustrated in Fig. 5-12. User space Kernel space User process User program Rest of the operating system Printer driver Camcorder driver ...
clipped_os_Page_359_Chunk6576
360 INPUT/OUTPUT CHAP. 5 with UNIX systems because they were run by computer centers and I/O devices rarely changed. If a new device was added, the system administrator simply re- compiled the kernel with the new driver to build a new binary. With the advent of personal computers, with their myriad I/O devices, this mo...
clipped_os_Page_360_Chunk6577
SEC. 5.3 I/O SOFTWARE LAYERS 361 have some data to pass to the device-independent software (e.g., a block just read). Finally, it returns some status information for error reporting back to its caller. If any other requests are queued, one of them can now be selected and started. If nothing is queued, the driver blocks...
clipped_os_Page_361_Chunk6578
362 INPUT/OUTPUT CHAP. 5 The basic function of the device-independent software is to perform the I/O functions that are common to all devices and to provide a uniform interface to the user-level software. We will now look at the above issues in more detail. Uniform Interfacing for Device Drivers A major issue in an ope...
clipped_os_Page_362_Chunk6579
SEC. 5.3 I/O SOFTWARE LAYERS 363 on and off, formatting, and other disky things. Often the driver holds a table with pointers into itself for these functions. When the driver is loaded, the operating system records the address of this table of function pointers, so when it needs to call one of the functions, it can mak...
clipped_os_Page_363_Chunk6580
364 INPUT/OUTPUT CHAP. 5 User process User space Kernel space 2 2 1 1 3 Modem Modem Modem Modem (a) (b) (c) (d) Figure 5-15. (a) Unbuffered input. (b) Buffering in user space. (c) Buffering in the kernel followed by copying to user space. (d) Double buffering in the kernel. Yet another approach is to create a buffer in...
clipped_os_Page_364_Chunk6581
SEC. 5.3 I/O SOFTWARE LAYERS 365 but this leads to an even worse problem: how does the user process know that the output has been completed and it can reuse the buffer? The system could generate a signal or software interrupt, but that style of programming is difficult and prone to race conditions. A much better soluti...
clipped_os_Page_365_Chunk6582
366 INPUT/OUTPUT CHAP. 5 Error Reporting Errors are far more common in the context of I/O than in other contexts. When they occur, the operating system must handle them as best it can. Many errors are device specific and must be handled by the appropriate driver, but the framework for error handling is device independe...
clipped_os_Page_366_Chunk6583
SEC. 5.3 I/O SOFTWARE LAYERS 367 Device-Independent Block Size Different disks may have different sector sizes. It is up to the device-indepen- dent software to hide this fact and provide a uniform block size to higher layers, for example, by treating several sectors as a single logical block. In this way, the higher l...
clipped_os_Page_367_Chunk6584
368 INPUT/OUTPUT CHAP. 5 Instead what is done is to create a special process, called a daemon, and a spe- cial directory, called a spooling directory. To print a file, a process first generates the entire file to be printed and puts it in the spooling directory. It is up to the dae- mon, which is the only process havin...
clipped_os_Page_368_Chunk6585
SEC. 5.3 I/O SOFTWARE LAYERS 369 When the disk is finished, the hardware generates an interrupt. The interrupt handler is run to discover what has happened, that is, which device wants attention right now. It then extracts the status from the device and wakes up the sleeping process to finish off the I/O request and le...
clipped_os_Page_369_Chunk6586
370 INPUT/OUTPUT CHAP. 5 same time. (Reading or writing requires the controller to move bits on a microsec- ond time scale, so one transfer uses up most of its computing power.) The situa- tion is different for hard disks with integrated controllers, and in a system with more than one of these hard drives they can oper...
clipped_os_Page_370_Chunk6587
SEC. 5.4 DISKS 371 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 Figure 5-19. (a) Physical geometry of a disk with two zones. (b) A po...
clipped_os_Page_371_Chunk6588
372 INPUT/OUTPUT CHAP. 5 Yes! As we have seen, parallel processing is increasingly being used to speed up CPU performance. It has occurred to various people over the years that parallel I/O might be a good idea, too. In their 1988 paper, Patterson et al. suggested six specific disk organizations that could be used to i...
clipped_os_Page_372_Chunk6589
SEC. 5.4 DISKS 373 proper disks in the right sequence and then assemble the results in memory cor- rectly. Performance is excellent and the implementation is straightforward. RAID level 0 works worst with operating systems that habitually ask for data one sector at a time. The results will be correct, but there is no p...
clipped_os_Page_373_Chunk6590
374 INPUT/OUTPUT CHAP. 5 Figure 5-20. RAID levels 0 through 6. Backup and parity drives are shown shaded.
clipped_os_Page_374_Chunk6591
SEC. 5.4 DISKS 375 crashes, the controller just pretends that all its bits are 0s. If a word has a parity error, the bit from the dead drive must have been a 1, so it is corrected. Although both RAID levels 2 and 3 offer very high data rates, the number of separate I/O re- quests per second they can handle is no better...
clipped_os_Page_375_Chunk6592
376 INPUT/OUTPUT CHAP. 5 The preamble starts with a certain bit pattern that allows the hardware to rec- ognize the start of the sector. It also contains the cylinder and sector numbers and some other information. The size of the data portion is determined by the low- level formatting program. Most disks use 512-byte s...
clipped_os_Page_376_Chunk6593
SEC. 5.4 DISKS 377 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 2 9 3 0 3 1 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 6 2 7 2 8 2 9 3 0 3 1 0 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2...
clipped_os_Page_377_Chunk6594
378 INPUT/OUTPUT CHAP. 5 memory. While this transfer is taking place, the next sector will fly by the head. When the copy to memory is complete, the controller will have to wait almost an entire rotation time for the second sector to come around again. This problem can be eliminated by numbering the sectors in an inter...
clipped_os_Page_378_Chunk6595
SEC. 5.4 DISKS 379 The final step in preparing a disk for use is to perform a high-level format of each partition (separately). This operation lays down a boot block, the free storage administration (free list or bitmap), root directory, and an empty file system. It also puts a code in the partition table entry telling...
clipped_os_Page_379_Chunk6596
380 INPUT/OUTPUT CHAP. 5 Initial position Pending requests Sequence of seeks Cylinder X X X X X X X 0 5 10 15 20 25 30 35 Time Figure 5-24. Shortest Seek First (SSF) disk scheduling algorithm. Alternatively, it could always handle the closest request next, to minimize seek time. Given the requests of Fig. 5-24, the seq...
clipped_os_Page_380_Chunk6597
SEC. 5.4 DISKS 381 Figure 5-25 shows the elevator algorithm using the same seven requests as Fig. 5-24, assuming the direction bit was initially UP. The order in which the cyl- inders are serviced is 12, 16, 34, 36, 9, and 1, which yields arm motions of 1, 4, 18, 2, 27, and 8, for a total of 60 cylinders. In this case ...
clipped_os_Page_381_Chunk6598
382 INPUT/OUTPUT CHAP. 5 space is available in the controller’s cache memory. The hard disk described in Fig. 5-18 has a 4-MB cache, for example. The use of the cache is determined dynam- ically by the controller. In its simplest mode, the cache is divided into two sections, one for reads and one for writes. If a subse...
clipped_os_Page_382_Chunk6599
SEC. 5.4 DISKS 383 a few bits, it is possible to use the bad sector and just let the ECC correct the errors ev ery time. If the defect is bigger, the error cannot be masked. There are two general approaches to bad blocks: deal with them in the con- troller or deal with them in the operating system. In the former approa...
clipped_os_Page_383_Chunk6600