




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、System-Level I/OIntroduction to Computer Systems19th Lecture, Nov. 26, 2013Instructors: Xiangqun Chen,Junlin LuGuangyu Sun,Xuetao GuanTodayUnix I/OMetadata, sharing, and redirectionStandard I/ORIO (robust I/O) packageClosing remarksUnix FilesA Unix file is a sequence of m bytes:B0 , B1 , . , Bk , .
2、, Bm-1All 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 File TypesRegular fileFile containing user/app data (binary, text, whatever)OS does not know a
3、nything about the formatother than “sequence of bytes”, akin to main memoryDirectory fileA file that contains the names and locations of other filesCharacter special and block special filesTerminals (character special) and disks (block special)FIFO (named pipe)A file type used for inter-process comm
4、unicationSocketA file type used for network communication between processesUnix I/OKey FeaturesElegant mapping of files to devices allows kernel to export simple interface called Unix I/OImportant idea: All input and output is handled in a consistent and uniform wayBasic Unix I/O operations (system
5、calls): Opening and closing filesopen()and close()Reading and writing a fileread() and write()Changing the current file position (seek)indicates next offset into file to read or writelseek()B0B1 Bk-1BkBk+1 Current file position = kOpening FilesOpening a file informs the kernel that you are getting r
6、eady to access that fileReturns a small identifying integer file descriptorfd = -1 indicates that an error occurredEach process created by a Unix shell begins life with three open files associated with a terminal:0: standard input1: standard output2: standard errorint fd; /* file descriptor */if (fd
7、 = open(/etc/hosts, O_RDONLY) 0) perror(open); exit(1);Closing FilesClosing a file informs the kernel that you are finished accessing that fileClosing an already closed file is a recipe for disaster in threaded programs (more on this later)Moral: Always check return codes, even for seemingly benign
8、functions such as close()int fd; /* file descriptor */int retval; /* return value */if (retval = close(fd) 0) perror(close); exit(1);Reading FilesReading a file copies bytes from the current file position to memory, and then updates file positionReturns number of bytes read from file fd into bufRetu
9、rn type ssize_t is signed integernbytes 0 indicates that an error occurredShort 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 up to 512 bytes from file fd */if (nbytes = read(
10、fd, buf, sizeof(buf) 0) perror(read); exit(1);Writing FilesWriting a file copies bytes from memory to the current file position, and then updates current file positionReturns number of bytes written from buf to file fdnbytes 0 indicates that an error occurredAs with reads, short counts are possible
11、and are not errors!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 (nbytes = write(fd, buf, sizeof(buf) 0) perror(write); exit(1);Simple Unix I/O exampleCopying standard in to standard out, on
12、e byte at a timeint main(void) char c; int len; while (len = read(0 /*stdin*/, &c, 1) = 1) if (write(1 /*stdout*/, &c, 1) != 1) exit(20); if (len ./statcheck statcheck.ctype: regular, read: yesunix chmod 000 statcheck.cunix ./statcheck statcheck.ctype: regular, read: nounix ./statcheck .type: direct
13、ory, read: yesunix ./statcheck /dev/kmemtype: other, read: yesstatcheck.cHow the Unix Kernel Represents Open FilesTwo descriptors referencing two distinct open disk files. Descriptor 1 (stdout) points to terminal, and descriptor 4 points to open disk filefd 0fd 1fd 2fd 3fd 4Descriptor tableone table
14、 per processOpen file table shared by all processesv-node tableshared by all processesFile posrefcnt=1.File posrefcnt=1.stderrstdoutstdinFile access.File sizeFile typeFile access.File sizeFile typeFile A (terminal)File B (disk)Info in stat structFile SharingTwo distinct descriptors sharing the same
15、disk file through two distinct open file table entriesE.g., Calling open twice with the same filename argumentfd 0fd 1fd 2fd 3fd 4Descriptor tableone table per processOpen file table shared by all processesv-node tableshared by all processesFile posrefcnt=1.File posrefcnt=1.stderrstdoutstdinFile acc
16、ess.File sizeFile typeFile A (disk)File B (disk)How Processes Share Files: Fork()A child process inherits its parents open filesNote: situation unchanged by exec functions (use fcntl to change)Before fork() call:fd 0fd 1fd 2fd 3fd 4Descriptor tableone table per processOpen file table shared by all p
17、rocessesv-node tableshared by all processesFile posrefcnt=1.File posrefcnt=1.stderrstdoutstdinFile access.File sizeFile typeFile access.File sizeFile typeFile A (terminal)File B (disk)How Processes Share Files: Fork()A child process inherits its parents open filesAfter fork():Childs table same as pa
18、rents, and +1 to each refcntfd 0fd 1fd 2fd 3fd 4Descriptor tableone table per processOpen file table shared by all processesv-node tableshared by all processesFile posrefcnt=2.File posrefcnt=2.File access.File sizeFile typeFile access.File sizeFile typeFile A (terminal)File B (disk)fd 0fd 1fd 2fd 3f
19、d 4ParentChildI/O RedirectionQuestion: How does a shell implement I/O redirection?unix ls foo.txtAnswer: By calling the dup2(oldfd, newfd) functionCopies (per-process) descriptor table entry oldfd to entry newfdabfd 0fd 1fd 2fd 3fd 4Descriptor tablebefore dup2(4,1)bbfd 0fd 1fd 2fd 3fd 4Descriptor ta
20、bleafter dup2(4,1)I/O Redirection Example Step #1: open file to which stdout should be redirectedHappens in child executing shell code, before execfd 0fd 1fd 2fd 3fd 4Descriptor tableone table per processOpen file table shared by all processesv-node tableshared by all processesFile posrefcnt=1.stder
21、rstdoutstdinFile access.File sizeFile typeFile AFile posrefcnt=1.File access.File sizeFile typeFile BI/O Redirection Example (cont.)Step #2: call dup2(4,1)cause fd=1 (stdout) to refer to disk file pointed at by fd=4fd 0fd 1fd 2fd 3fd 4Descriptor tableone table per processOpen file table shared by al
22、l processesv-node tableshared by all processesFile posrefcnt=0.File posrefcnt=2.stderrstdoutstdinFile access.File sizeFile typeFile access.File sizeFile typeFile AFile BTodayUnix I/OMetadata, sharing, and redirectionStandard I/ORIO (robust I/O) packageClosing remarksStandard I/O FunctionsThe C stand
23、ard library (libc.so) contains a collection of higher-level standard I/O functionsDocumented in Appendix B of K&RExamples of standard I/O functions:Opening and closing files (fopen and fclose)Reading and writing bytes (fread and fwrite)Reading and writing text lines (fgets and fputs)Formatted readin
24、g and writing (fscanf and fprintf)Standard I/O StreamsStandard I/O models open files as streamsAbstraction for a file descriptor and a buffer in memoryC programs begin life with three open streams (defined in stdio.h)stdin (standard input)stdout (standard output)stderr (standard error)#include exter
25、n FILE *stdin; /* standard input (descriptor 0) */extern FILE *stdout; /* standard output (descriptor 1) */extern FILE *stderr; /* standard error (descriptor 2) */int main() fprintf(stdout, Hello, worldn);Buffered I/O: MotivationApplications often read/write one character at a timegetc, putc, ungetc
26、gets, fgetsRead line of text on character at a time, stopping at newlineImplementing as Unix I/O calls expensiveread and write require Unix kernel calls 10,000 clock cyclesSolution: Buffered readUse Unix read to grab block of bytesUser input functions take one byte at a time from bufferRefill buffer
27、 when emptyunreadalready readBufferBuffering in Standard I/OStandard I/O functions use buffered I/OBuffer flushed to output fd on “n” or fflush() callprintf(h);hellon.printf(e);printf(l);printf(l);printf(o);printf(n);fflush(stdout);bufwrite(1, buf, 6);Standard I/O Buffering in ActionYou can see this
28、 buffering in action for yourself, using the always fascinating Unix strace program:linux strace ./helloexecve(./hello, hello, /* . */).write(1, hellon, 6) = 6.exit_group(0) = ?#include int main() printf(h); printf(e); printf(l); printf(l); printf(o); printf(n); fflush(stdout); exit(0);TodayUnix I/O
29、Metadata, sharing, and redirectionStandard I/ORIO (robust I/O) packageClosing remarksThe RIO PackageRIO is a set of wrappers that provide efficient and robust I/O in apps, such as network programs that are subject to short countsRIO provides two different kinds of functionsUnbuffered input and outpu
30、t of binary datario_readn and rio_writenBuffered input of binary data and text linesrio_readlineb and rio_readnbBuffered RIO routines are thread-safe and can be interleaved arbitrarily on the same descriptorDownload from src/csapp.c and include/csapp.hUnbuffered RIO Input and OutputSame interface as
31、 Unix read and writeEspecially useful for transferring data on network socketsrio_readn returns short count only if it encounters EOFOnly use it when you know how many bytes to readrio_writen never returns a short countCalls to rio_readn and rio_writen can be interleaved arbitrarily on the same desc
32、riptor#include csapp.hssize_t rio_readn(int fd, void *usrbuf, size_t n);ssize_t rio_writen(int fd, 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(
33、int fd, void *usrbuf, size_t n) size_t nleft = n; ssize_t nread; char *bufp = usrbuf; while (nleft 0) if (nread = read(fd, bufp, nleft) = 0 */csapp.cBuffered RIO Input FunctionsEfficiently read text lines and binary data from a file partially cached in an internal memory bufferrio_readlineb reads a
34、text line of up to maxlen bytes from file fd and stores the line in usrbufEspecially useful for reading text lines from network socketsStopping conditions maxlen bytes readEOF encounteredNewline (n) encountered#include csapp.hvoid rio_readinitb(rio_t *rp, int fd);ssize_t rio_readlineb(rio_t *rp, voi
35、d *usrbuf, size_t maxlen); Return: num. bytes read if OK, 0 on EOF, -1 on errorBuffered RIO Input Functions (cont)rio_readnb reads up to n bytes from file fdStopping conditions maxlen bytes readEOF encounteredCalls to rio_readlineb and rio_readnb can be interleaved arbitrarily on the same descriptor
36、Warning: Dont interleave with calls to rio_readn#include csapp.hvoid rio_readinitb(rio_t *rp, int fd);ssize_t rio_readlineb(rio_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 errorunreadBuffered I/O: Implement
37、ationFor reading from fileFile has associated buffer to hold bytes that have been read from file but not yet read by user codeLayered on Unix file:already readBufferrio_bufrio_bufptrrio_cntunreadalready readnot in bufferunseenCurrent File PositionBuffered PortionBuffered I/O: DeclarationAll informat
38、ion contained in structtypedef struct int rio_fd; /* 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;unreadalready readBufferrio_bufrio_bufptrrio_cntRIO E
39、xampleCopying the lines of a text file from standard input to standard output#include csapp.hint main(int argc, char *argv) int n; rio_t rio; char bufMAXLINE; Rio_readinitb(&rio, STDIN_FILENO); while(n = Rio_readlineb(&rio, buf, MAXLINE) != 0) Rio_writen(STDOUT_FILENO, buf, n); exit(0);cpfile.cToday
40、Unix I/OMetadata, sharing, and redirectionStandard I/ORIO (robust I/O) packageClosing commentsUnix I/O vs. Standard I/O vs. RIOStandard I/O and RIO are implemented using low-level Unix I/OWhich ones should you use in your programs?Unix I/O functions (accessed via system calls) Standard I/O functions
41、C application programfopen fdopenfread fwrite fscanf fprintf sscanf sprintf fgets fputs fflush fseekfcloseopen readwrite lseekstat closerio_readnrio_writenrio_readinitbrio_readlinebrio_readnb RIOfunctionsPros and Cons of Unix I/OProsUnix I/O is the most general and lowest overhead form of I/O.All ot
42、her I/O packages are implemented using Unix I/O functions.Unix I/O provides functions for accessing file metadata.Unix I/O functions are async-signal-safe and can be used safely in signal handlers. ConsDealing with short counts is tricky and error prone.Efficient reading of text lines requires some
43、form of buffering, also tricky and error prone.Both of these issues are addressed by the standard I/O and RIO packages.Pros and Cons of Standard I/OPros:Buffering increases efficiency by decreasing the number of read and write system callsShort counts are handled automaticallyCons:Provides no functi
44、on for accessing file metadataStandard I/O functions are not async-signal-safe, and not appropriate for signal handlers. Standard I/O is not appropriate for input and output on network socketsThere are poorly documented restrictions on streams that interact badly with restrictions on sockets (CS:APP2e, Sec 10.9)Choosing I/O FunctionsGeneral rule: use the highest-level I/O functions you canMany C programmers are able to do all of their work using the standard I/O functionsBut, be sure to understand the functions you use!When to use standard I/O
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藥庫人員培訓管理制度
- 藥店藥品進銷管理制度
- 設備保養考核管理制度
- 設備外包服務管理制度
- 設計編制審核管理制度
- 評審人員檔案管理制度
- 診所清點庫存管理制度
- 診斷試劑保存管理制度
- 財務財產安全管理制度
- 財政專項債券管理制度
- 腰椎間盤突出癥手術的圍手術期護理
- 安全生產風險防控課件
- 2025年中國分布式光伏行業市場動態分析、發展方向及投資前景分析
- 2025年湖南株洲南方中學自主招生英語試卷真題(含答案詳解)
- 2025年蘇州保安員證試題及答案
- 勞務綠化合同范本
- 2025年云南黃金礦業集團股份有限公司招聘筆試參考題庫含答案解析
- 比亞迪秦EV新能源汽車電機驅動系統
- 美國麻醉醫師協會ASA困難氣道管理xuli
- 落戶服務協議上海上海落戶承諾書
- 高中信息技術《數據處理與應用》練習題(附答案解析)
評論
0/150
提交評論