资源预览内容
第1页 / 共42页
第2页 / 共42页
第3页 / 共42页
第4页 / 共42页
第5页 / 共42页
第6页 / 共42页
第7页 / 共42页
第8页 / 共42页
第9页 / 共42页
第10页 / 共42页
亲,该文档总共42页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
CSAPP L11 Chp11.1Wu Spring 09 USTCComputer Systems: A Programmers Perspective 计算机系统详解 Lecture 11 SYSTEM I/OJune 1, 2009Wu junmin (jmwuustc.edu.cn)CSAPP L11 Chp11.2Wu Spring 09 USTCUnix I/ORobust reading and writingReading file metadataSharing filesI/O redirectionStandard I/OTopicsCSAPP L11 Chp11.3Wu Spring 09 USTCUnix I/O Key Characteristics Classic Unix/Linux I/O: I/O operates on linear streams of Bytes Can reposition insertion point and extend file at end I/O tends to be synchronous Read or write operation block until data has been transferred Fine grained I/O One key-stroke at a time Each I/O event is handled by the kernel and an appropriate process Mainframe I/O: I/O operates on structured records Functions to locate, insert, remove, update records I/O tends to be asynchronous Overlap I/O and computation within a process Coarse grained I/O Process writes “channel programs” to be executed by the I/O hardware Many I/O operations are performed autonomously with one interrupt at completionCSAPP L11 Chp11.4Wu Spring 09 USTCA Typical Hardware Systemmain memoryI/O bridgebus interfaceALUregister fileCPU chipsystem busmemory busdisk controllergraphics adapterUSB controllermousekeyboardmonitordiskI/O busExpansion slots for other devices such as network adapters.CSAPP L11 Chp11.5Wu Spring 09 USTCReading a Disk Sector: Step 1 main memoryALUregister fileCPU chipdisk controllergraphics adapterUSB controllermousekeyboardmonitordiskI/O busbus interfaceCPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller.CSAPP L11 Chp11.6Wu Spring 09 USTCReading a Disk Sector: Step 2main memoryALUregister fileCPU chipdisk controllergraphics adapterUSB controllermousekeyboardmonitordiskI/O busbus interfaceDisk controller reads the sector and performs a direct memory access (DMA) transfer into main memory.CSAPP L11 Chp11.7Wu Spring 09 USTCReading a Disk Sector: Step 3main memoryALUregister fileCPU chipdisk controllergraphics adapterUSB controllermousekeyboardmonitordiskI/O busbus interfaceWhen the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU)CSAPP L11 Chp11.8Wu Spring 09 USTCUnix Files A Unix file is a sequence of m bytes: B0, B1, , Bk , , Bm-1 All I/O devices are represented as files: /dev/sda2 (/usr disk partition) /dev/tty2 (terminal) Even the kernel is represented as a file: /dev/kmem (kernel memory image) /proc (kernel data structures)CSAPP L11 Chp11.9Wu Spring 09 USTCUnix File Types Regular file Binary or text file. Unix does not know the difference! Directory file A file that contains the names and locations of other files. Character special and block special files Terminals (character special) and disks ( block special) FIFO (named pipe) A file type used for interprocess communication Socket A file type used for network communication between processesCSAPP L11 Chp11.10Wu Spring 09 USTCUnix I/O The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. Key Unix idea: All input and output is handled in a consistent and uniform way. Basic Unix I/O operations (system calls): Opening and closing files -open()and close() Changing the current file position (seek) -lseek (not discussed) Reading and writing a file -read() and write()CSAPP L11 Chp11.11Wu Spring 09 USTCOpening Files Opening a file informs the kernel that you are getting ready to access that file. Returns a small identifying integer file descriptor fd = -1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal: 0: standard input 1: standard output 2: standard errorint fd; /* file descriptor */if (fd = open(“/etc/hosts“, O_RDONLY) 0) if (nread = read(fd, bufp, nleft) = 0 */ CSAPP L11 Chp11.20Wu Spring 09 USTCBuffered I/O: Motivation I/O Applications Read/Write One Character at a Time getc, putc, ungetc gets -Read line of text, stopping at newline Implementing as Calls to Unix I/O Expensive Read /* descriptor for this internal buf */int rio_cnt; /* unread bytes in internal buf */char *rio_bufptr; /* next unread byte in internal buf */char rio_bufRIO_BUFSIZE; /* internal buffer */ rio_t;Bufferrio_bufrio_bufptrrio_cntCSAPP L11 Chp11.22Wu Spring 09 USTCBuffered RIO Input Functions Efficiently read text lines and binary data from a file partially cached in an internal memory buffer rio_readlineb reads a text line of up to maxlen bytes from file fd and stores the line in usrbuf.-Especially useful for reading text lines from network sockets. rio_readnb reads up to n bytes from file fd. Calls to rio_readlineb and rio_readnb can be interleaved arbitrarily on the same descriptor.-Warning: Dont interleave with calls to rio_readn#include “csapp.h“void rio_readinitb(r
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号