15213 Recitation Section CImportant DatesRobust I/O: RIORio: Unbuffered Input/OutputRIO: Buffered InputRio: Buffered Inputrio_readlinebInterleaving RIO Read FunctionsRio Error CheckingProblems from Chapter 11Problem 11.1Answer to 11.1File SharingProblem 11.2Answer to 11.2Problem 11.3Answer to 11.3Problem 11.4Answer to 11.4Problem 11.5Answer to 11.515213 Recitation Section C• Robust I/O package• Chapter 11 practice problemsShimin ChenNov. 18, 2002Outline215213 Recitation C Shimin ChenImportant Dates•Lab 6 Malloc: due on Thursday, Nov 21•Lab 7 Proxy: due on Thursday, Dec 5•Final Exam: Tuesday, Dec 17315213 Recitation C Shimin ChenRobust I/O: RIO•csapp.c and csapp.h•Why?–Handles interrupted system calls–Handles short counts–Good for network programming•Two parts:–Unbuffered I/O–Buffered I/O415213 Recitation C Shimin ChenRio: Unbuffered Input/Output•Use Unix I/O•No internal buffering•Useful for reading/writing binary data to/from networksssize_t rio_readn(int fd, void* usrbuf, size_t n)–reads n bytes from fd and put into usrbuf –only returns short count on EOFssize_t rio_writen(int fd, void* usrbuf, size_t n)–writes n bytes from usrbuf to fd–never returns short count515213 Recitation C Shimin ChenRIO: Buffered Input•Internal buffers#define RIO_BUFSIZE 8192typedef struct { int rio_fd; int rio_cnt; char *rio_bufptr; char rio_buf[RIO_BUFSIZE];} rio_t;void 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);615213 Recitation C Shimin ChenRio: Buffered Inputvoid rio_readinitb(rio_t* rp, int fd);–called only once per open descriptor–associate fd with a read buffer rio_t structure pointed to by rpssize_t rio_readlineb(rio_t* rp, void* usrbuf, size_t maxlen); –for reading text lines–read a line (until ‘\n’) or maxlen-1 chars from file rp to usrbuf–terminate the text line with null (zero) character–returns number of chars read ssize_t rio_readnb(rio_t* rp, void* usrbuf, size_t n);–reads n bytes from rp into usrbuf–Result string is NOT null-terminated!–Returns number of bytes read715213 Recitation C Shimin Chenrio_readlinebssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) { int n, rc; char c, *bufp = usrbuf; for (n = 1; n < maxlen; n++) { if ((rc = rio_read(rp, &c, 1)) == 1) { *bufp++ = c; if (c == '\n') break; } else if (rc == 0) { if (n == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ } else return -1; /* error */ } *bufp = 0; return n;}815213 Recitation C Shimin ChenInterleaving RIO Read Functions•Do not interleave calls on the same fd between the buffered and unbuffered functions•Within each set it is ok•Why?rio_readinitbrio_readlinebrio_readnbrio_readnrio_writenunbufferedbuffered915213 Recitation C Shimin ChenRio Error Checking•RIO functions handle–Short counts–interrupted system calls•All functions have upper case equivalents –Rio_readn, Rio_writen, Rio_readlineb, Rio_readnb,etc.–call unix_error if the function encounters an error•But EPIPE errors!–for Lab 7, EPIPE should not terminate the process1015213 Recitation C Shimin ChenProblems from Chapter 11•11.1 ~ 11.51115213 Recitation C Shimin ChenProblem 11.1#include "csapp.h"int main(){ int fd1, fd2; fd1 = Open("foo.txt", O_RDONLY, 0); Close(fd1); fd2 = Open("baz.txt", O_RDONLY, 0); printf("fd2 = %d\n", fd2); exit(0);}What is the output of the following program?1215213 Recitation C Shimin ChenAnswer to 11.1•stdin (descriptor 0)stdout (descriptor 1)stderr (descriptor 2)•open always returns lowest unopened descriptor•First open returns 3. close frees it.•So second open also returns 3.•Program prints: "fd2 = 3"1315213 Recitation C Shimin ChenFile Sharing•Descriptor table–Each process has its own–Child inherits from parents•File Table–set of all open files–Shared by all processes–Reference count of number of file descriptors pointing to each entry–File position•V-node table–Contains information in the stat structure–Shared by all processes1415213 Recitation C Shimin ChenProblem 11.2Suppose that foobar.txt consists of the 6 ASCII characters "foobar". Then what is the output of the following program?#include "csapp.h"int main(){ int fd1, fd2; char c; fd1 = Open("foobar.txt", O_RDONLY, 0); fd2 = Open("foobar.txt", O_RDONLY, 0); Read(fd1, &c, 1); Read(fd2, &c, 1); printf("c = %c\n", c); exit(0);}1515213 Recitation C Shimin ChenAnswer to 11.2The descriptors fd1 and fd2 each have their own open file table entry, so each descriptor has its own file position for foobar.txt. Thus, the read from fd2 reads the first byte of foobar.txt, and the output isc = fand notc = oas you might have thought initially.1615213 Recitation C Shimin ChenProblem 11.3As before, suppose foobar.txt consists of 6 ASCII characters "foobar". Then what is the output of the following program?#include "csapp.h"int main(){ int fd; char c; fd = Open("foobar.txt", O_RDONLY, 0); if(Fork() == 0) {Read(fd, &c, 1); exit(0);} Wait(NULL); Read(fd, &c, 1); printf("c = %c\n", c); exit(0);}1715213 Recitation C Shimin ChenAnswer to 11.3Child inherit’s the parent’s descriptor table. So child and parent share an open file table entry (refcount = 2). Hence they share a file position.c = o1815213 Recitation C Shimin ChenProblem 11.4•How would you use dup2 to redirect standard input to descriptor 5?•int dup2(int oldfd, int newfd);–copies descriptor table entry oldfd to descriptor table entry newfd1915213 Recitation C Shimin ChenAnswer to 11.4dup2(5,0);ordup2(5,STDIN_FILENO);2015213 Recitation C Shimin ChenProblem 11.5Assuming that foobar.txt consists of 6 ASCII characters “foobar”. Then what is the output of the following program?#include "csapp.h"int main(){ int fd1, fd2; char c; fd1 = Open("foobar.txt", O_RDONLY, 0); fd2 = Open("foobar.txt", O_RDONLY, 0); Read(fd2, &c, 1); Dup2(fd2, fd1); Read(fd1, &c, 1); printf("c = %c\n", c); exit(0);}2115213 Recitation C Shimin ChenAnswer to 11.5We are redirecting fd1 to fd2. (fd1 now points to the same open file table entry as fd2). So the second Read uses the file position offset of fd2.c =
View Full Document