System-Level I-OMay 28, 2008.ppt
《System-Level I-OMay 28, 2008.ppt》由会员分享,可在线阅读,更多相关《System-Level I-OMay 28, 2008.ppt(37页珍藏版)》请在麦多课文档分享上搜索。
1、System-Level I/O May 28, 2008,Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O,A Typical Hardware System,main memory,I/O bridge,bus interface,ALU,register file,CPU chip,system bus,memory bus,disk controller,graphics adapter,USB controller,mo
2、use,keyboard,monitor,disk,I/O bus,Expansion slots for other devices such as network adapters.,Unix 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 Ter
3、minals (character special) and disks (block special) FIFO (named pipe) A file type used for interprocess comunication Socket A file type used for network communication between processes,Opening Files,Opening a file informs the kernel that you are getting ready to access that file.Returns a small ide
4、ntifying 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 error,int fd; /* file descriptor */if (fd = open(“/etc/hosts”, O_RDONLY) 0) pe
5、rror(“open”);exit(1); ,C File I/O vs. Unix File I/O,C file I/O: FILE * fopen(const char *name, const char *mode) int fclose(FILE *stream) FILE type represents a stream, passed to fclose(), fread(), fwrite(), Stream: abstraction for a file descriptor and a buffer in memory Portable; implemented on to
6、p of host OS file system Unix file I/O: int open(const char *path, int oflag, int mode) int close(int fd) open() returns lowest available integer file descriptor file descriptor analogous to FILE in C I/O See Stevens chapter for details, examples,Unix File I/O Quiz 1,What is the output of this code?
7、,int main() int fd1, fd2;fd1 = open(“foo.txt“, O_RDONLY, 0);close(fd1);fd2 = open(“bar.txt“, O_RDONLY, 0);close(fd2);printf(“fd2 = %dn“, fd2);exit(0); ,Unix File I/O Quiz 2,What is the output of this code if foobar.txt contains “foobar“?,int main() int fd1, fd2;char c;fd1 = open(“foobar.txt“, O_RDON
8、LY, 0);fd2 = open(“foobar.txt“, O_RDONLY, 0);read(fd1, ,Unix File I/O Quiz 3,What is the output of this code if foobar.txt contains “foobar“?,int main() int fd;char c;fd = open(“foobar.txt“, O_RDONLY, 0);if (fork() = 0) read(fd, ,Unix Files,A Unix file is a sequence of m bytes: B0, B1, , Bk , , Bm-1
9、 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),Unix I/O,The elegant mapping of files to devices allows kernel to export simple interface called Un
10、ix I/O. Key Unix idea: All input and output is handled in a consistent and uniform way. Why do we care? Understanding I/O will help you understand other system concepts Sometimes you have no chance but to use Unix I/O functions Basic Unix I/O operations (system calls): Opening and closing files open
11、()and close() Changing the current file position (seek) lseek (not discussed) Reading and writing a file read() and write(),Closing Files,Closing a file informs the kernel that you are finished accessing that file.Closing an already closed file is a recipe for disaster in threaded programs (more on
12、this later) Moral: Always check return codes, even for seemingly benign functions such as close(),int fd; /* file descriptor */ int retval; /* return value */if (retval = close(fd) 0) perror(“close”);exit(1); ,Reading Files,Reading a file copies bytes from the current file position to memory, and th
13、en updates file position.Returns number of bytes read from file fd into buf nbytes 0 indicates that an error occurred. short counts (nbytes sizeof(buf) ) are possible and are not errors!,char buf512; int fd; /* file descriptor */ int nbytes; /* number of bytes read */* Open file fd . */ /* Then read
14、 up to 512 bytes from file fd */ if (nbytes = read(fd, buf, sizeof(buf) 0) perror(“read”);exit(1); ,Writing Files,Writing a file copies bytes from memory to the current file position, and then updates current file position.Returns number of bytes written from buf to file fd. nbytes 0 indicates that
15、an error occurred. As with reads, short counts are possible and are not errors! Transfers up to 512 bytes from address buf to file fd,char buf512; int fd; /* file descriptor */ int nbytes; /* number of bytes read */* Open the file fd . */ /* Then write up to 512 bytes from buf to file fd */ if (nbyt
16、es = write(fd, buf, sizeof(buf) 0) perror(“write”);exit(1); ,Unix I/O Example,Copying standard input to standard output one byte at a time.,#include “csapp.h“int main(void) char c;while(Read(STDIN_FILENO, ,Dealing with Short Counts,Short counts can occur in these situations: Encountering (end-of-fil
17、e) EOF on reads. Reading text lines from a terminal. Reading and writing network sockets or Unix pipes. Short counts never occur in these situations: Reading from disk files (except for EOF) Writing to disk files. How should you deal with short counts in your code? Use the RIO (Robust I/O) package f
18、rom your textbooks csapp.c file (Appendix B).,The RIO Package,RIO is a set of wrappers that provide efficient and robust I/O in applications such as network programs that are subject to short counts. RIO provides two different kinds of functions Unbuffered input and output of binary data rio_readn a
19、nd rio_writen Buffered input of binary data and text lines rio_readlineb and rio_readnbDownload from csapp.cs.cmu.edu/public/ics/code/src/csapp.c csapp.cs.cmu.edu/public/ics/code/include/csapp.h,Unbuffered RIO Input and Output,Same interface as Unix read and write Especially useful for transferring
20、data on network socketsrio_readn returns short count only it encounters EOF. rio_writen never returns a short count. Calls to rio_readn and rio_writen can be interleaved arbitrarily on the same descriptor.,#include “csapp.h”ssize_t rio_readn(int fd, void *usrbuf, size_t n); ssize_t rio_writen(nt fd,
21、 void *usrbuf, size_t n);Return: num. bytes transferred if OK, 0 on EOF (rio_readn only), -1 on error,Implementation of rio_readn,/* rio_readn - robustly read n bytes (unbuffered)*/ ssize_t rio_readn(int fd, void *usrbuf, size_t n) size_t nleft = n;ssize_t nread;char *bufp = usrbuf;while (nleft 0) i
22、f (nread = read(fd, bufp, nleft) = 0 */ ,Buffered RIO Input Functions,Efficiently read text lines and binary data from a file partially cached in an internal memory bufferrio_readlineb reads a text line of up to maxlen bytes from file fd and stores the line in usrbuf. Especially useful for reading t
23、ext 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(rio_t *rp, int fd);ssize_t rio_readlineb(rio
24、_t *rp, void *usrbuf, size_t maxlen); ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);Return: num. bytes read if OK, 0 on EOF, -1 on error,RIO Example,Copying the lines of a text file from standard input to standard output.,#include “csapp.h“int main(int argc, char *argv) int n;rio_t rio;char
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SYSTEMLEVELIOMAY28 2008 PPT
