1 system-level i/o. 2 outline i/o redirection robust i/o standard i/o suggested reading –10.4,...

40
1 System-Level I/O

Upload: winfred-phelps

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

1

System-Level I/O

Page 2: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

2

Outline

• I/O redirection• Robust I/O• Standard I/O• Suggested Reading

– 10.4, 10.7~10.9

Page 3: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

I/O Redirection

• Question: How does a shell implement I/O redirection?unix> ls > foo.txt

• Answer: By calling the dup2(oldfd, newfd) function– Copies (per-process) descriptor table entry oldfd to entry newfd

a

b

fd 0fd 1fd 2fd 3fd 4

Descriptor tablebefore dup2(4,1)

b

b

fd 0fd 1fd 2fd 3fd 4

Descriptor tableafter dup2(4,1)

3

Page 4: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

4

unix > ls > foo.txt • dup2 copies entries in the per-process file

descriptor table.

• dup2(oldfd, newfd) overwrites the entry in the per-process file table for newfd with the entry for oldfd.

#include <unistd.h>

int dup2(int oldfd, int newfd);returns: nonnegative descriptor if OK,

-1 on error

I/O Redirction

Page 5: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

5

open file table(shared by all

process)

V-node table(shared by all

processes)

file pos

refcnt = 1

...

File access

File size

File type

file pos

refcnt = 1

...

file A

file B

file A

file B

fd0fd1fd2fd3fd4fd5fd6fd7

Descriptor table (one table per process)

File access

File size

File type

Redirection

dup2(4,1)

Page 6: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

6

open file table(shared by all

process)

V-node table(shared by all

processes)

file pos

refcnt = 0

...

File access

File size

File type

file pos

refcnt = 2

...

file A

file B

file A

file B

fd0fd1fd2fd3fd4fd5fd6fd7

Descriptor table (one table per process)

File access

File size

File type

Redirection

dup2(4,1)

Page 7: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

7

Redirection

#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)

}

foobar.txt

foobar

Page 8: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Fun with File Descriptors (1)

• What would this program print for file containing “abcde”?

#include "csapp.h"int main(int argc, char *argv[]){ int fd1, fd2, fd3; char c1, c2, c3; char *fname = argv[1]; fd1 = Open(fname, O_RDONLY, 0); fd2 = Open(fname, O_RDONLY, 0); fd3 = Open(fname, O_RDONLY, 0); Dup2(fd2, fd3); Read(fd1, &c1, 1); Read(fd2, &c2, 1); Read(fd3, &c3, 1); printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); return 0;}

ffiles1.c

8

Page 9: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Fun with File Descriptors (2)

• What would this program print for file containing “abcde”?

#include "csapp.h"int main(int argc, char *argv[]){ int fd1; int s = getpid() & 0x1; char c1, c2; char *fname = argv[1]; fd1 = Open(fname, O_RDONLY, 0); Read(fd1, &c1, 1); if (fork()) { /* Parent */ sleep(s); Read(fd1, &c2, 1); printf("Parent: c1 = %c, c2 = %c\n", c1, c2); } else { /* Child */ sleep(1-s); Read(fd1, &c2, 1); printf("Child: c1 = %c, c2 = %c\n", c1, c2); } return 0;} ffiles2.c

9

Page 10: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Fun with File Descriptors (3)

• What would be the contents of the resulting file?

#include "csapp.h"int main(int argc, char *argv[]){ int fd1, fd2, fd3; char *fname = argv[1]; fd1 = Open(fname, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR); Write(fd1, "pqrs", 4); fd3 = Open(fname, O_APPEND|O_WRONLY, 0); Write(fd3, "jklmn", 5); fd2 = dup(fd1); /* Allocates descriptor */ Write(fd2, "wxyz", 4); Write(fd3, "ef", 2); return 0;}

ffiles3.c

10

Page 11: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

The RIO Package

• RIO is a set of wrappers that provide efficient and robust I/O in apps, 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 and rio_writen

– Buffered input of binary data and text lines• rio_readlineb and rio_readnb

11

Page 12: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

12

Unbuffered I/O

• Transfer data directly between memory and a file, with no application-level bufferingno application-level buffering

#include "csapp.h"

ssize_t rio_readn(int fd, void *usrbuf, size_t count);

ssize_t rio_writen(int fd, void *usrbuf, size_t count);return: number of bytes read (0 if EOF) or written,

-1 on error

• rio_readn returns short count only if it encounters EOF• Only use it when you know how many bytes to read• rio_writen never returns a short count• Calls to rio_readn and rio_writen can be interleaved arbitrarily

on the same descriptor

Page 13: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

13

1 ssize_t rio_readnrio_readn(int fd, void *buf, size_t count)2 {3 size_t nleft = count;4 ssize_t nread;5 char *ptr = buf;67 while (nleft > 0) {8 if ((nread = read(fd, ptr, nleft)) < 0) {9 if (errno == EINTR)10 nread = 0; /* and call read() again */11 else12 return -1; /* errno set by read() */13 }14 else if (nread == 0)15 break; /* EOF */16 nleft -= nread;17 ptr += nread;18 }19 return (count - nleft); /* return >= 0 */20 }

Page 14: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

14

1 ssize_t rio_writenrio_writen(int fd, const void *buf, size_t count)2 {3 size_t nleft = count;4 ssize_t nwritten;5 const char *ptr = buf;67 while (nleft > 0) {8 if ((nwritten = write(fd, ptr, nleft)) <= 0) {9 if (errno == EINTR)10 nwritten = 0; /* and call write() again

*/11 else12 return -1; /* errorno set by write() */13 }14 nleft -= nwritten;15 ptr += nwritten;16 }17 return count;18 }

Page 15: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Buffered I/O: Motivation

• Applications often read/write one character at a time– getc, putc, ungetc– gets, fgets

• Read line of text on character at a time, stopping at newline

• Implementing as Unix I/O calls expensive– read and write require Unix kernel calls

• > 10,000 clock cycles

15

Page 16: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Buffered I/O: Motivation

• Solution: Buffered read– Use Unix read to grab block of bytes– User input functions take one byte at a time

from buffer• Refill buffer when empty

unreadalready readBuffer

16

Page 17: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

unread

Buffered I/O: Implementation

• For reading from file• File has associated buffer to hold bytes

that have been read from file but not yet read by user code

already readBuffer

rio_bufrio_bufptr

rio_cnt

typedef 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_buf[RIO_BUFSIZE]; /* internal buffer */} rio_t;

17

Page 18: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Buffered I/O: Implementation

• Layered on Unix file:

unreadalready readnot in buffer unseen

Current File Position

Buffered Portion

18

Page 19: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

19

Buffered RIO Input Function

• Efficiently read text lines and binary data from a file whose contents are cached in an application-level buffer

#include "csapp.h"

void rio_readinitb(rio_t *rp, int fd) ;

ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size t maxlen);returns: number of bytes read (0 if EOF), -1 on error

• 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

• Stopping conditions maxlen bytes read EOF encountered Newline (‘\n’) encountered

Page 20: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Buffered RIO Input Functions (cont)

• rio_readnb reads up to n bytes from file fd• Stopping conditions

maxlen bytes read EOF encountered

• Calls to rio_readlineb and rio_readnb can be interleaved arbitrarily on the same descriptor Warning: Don’t interleave with calls to rio_readn

#include "csapp.h"

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);

Return: num. bytes read if OK, 0 on EOF, -1 on error

20

Page 21: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

21

Robust I/O

#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){

rp->rio_fd = fd ;rp->rio_cnt = 0 ;rp->rio_bufptr = rio_buf ;

}

Page 22: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

22

Robust I/O

#include “csapp.h”

int main(int argc, char **argv){

int n;rio_t rio;char buf[MAXLINE];

Rio_readinitb(&rio, STDIN_FILENO);while ((n = Rio_readlineb( &rio, buf, MAXLINE) ) != 0 ) Rio_writen(STDOUT_FILENO, buf, n);

}

Page 23: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

23

1 static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n)

2 {

3 int cnt = 0;

4

5 while (rp->rio_cnt <= 0) { /* refill if buf is empty */

6 rp->rio_cnt = read(rp->rio_fd, rp->rio_buf,

7 sizeof(rp->rio_buf));

8 if ( rp->rio_cnt < 0) {

9 if (errno != EINTR)

10 return –1 ;

11 }

12 else if (rp->rio_cnt == 0) /* EOF */

13 return 0;

14 else

15 rp->rio_bufptr = rp->rio_buf;/* reset buffer ptr */

16 }

17

Page 24: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

24

18 /* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */

19 cnt = n ;

20 if ( rp->rio_cnt < n)

21 cnt = rp->rio_cnt ;

22 memcpy(usrbuf, rp->rio_bufptr, cnt) ;

23 rp->rio_buffer += cnt ;

24 rp->rio_cnt -= cnt ;

25 return cnt ;

26 }

Page 25: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

25

1 ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n)

2 {

3 size_t nleft = n; ssize_t nread ;

4 char *bufp = usrbuf;

5 while (nleft > 0) {

6 if ((nread = rio_read(rp, bufp, nleft)) < 0) {

7 if ( errno = EINTR)

8 /* interrupted by sig handler return */

9 nread = 0;

10 else

11 return –1;

12 }

13 else if (nread == 0)

14 break;

15 nleft -= nread;

16 bufp += nread;

17 }

18 return (n – nleft);

19 }

Page 26: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

26

1 ssize_t rio_readlineb (rio_t *rp, void *usrbuf, size_t maxlen)

2 {

3 int n, rc;

4 char c, *bufp = usrbuf;

5 for (n=1; n < maxlen; n++) {6 if ((rc = rio_read(rp, &c, 1)) == 1) {7 *bufp++ = c;8 if (c == ‘\n’) 9 break;10 } else if (rc == 0) {11 if (n== 1) 12 return 0; /* EOF, no data read */13 else 14 break;15 } else 16 return –1; /* error */17 }18 *bufp = 0 ;19 return n ;20 }

Page 27: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

• The C standard library (libc.solibc.so) contains a collection of higher-levelhigher-level standard I/O standard I/O functions

• Examples 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 reading and writing (fscanf and fprintf)

Standard I/O

27

Page 28: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Standard I/O

• Standard I/O models open files as streams– Abstraction for a file descriptor and a buffer in

memory.– Similar to buffered RIO

• C programs begin life with three open streams (defined in stdio.h)– stdin (standard input fd=0) – stdout (standard output fd=1)– stderr (standard error fd=2)

28

Page 29: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Buffering in Standard I/O

• Standard I/O functions use buffered I/O

• Buffer flushed to output fd on “\n” or fflush() call

printf("h");

h e l l o \n . .

printf("e");printf("l");

printf("l");printf("o");

printf("\n");

fflush(stdout);

buf

write(1, buf, 6);

29

Page 30: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Standard I/O Buffering in Action

• You can see this buffering in action for yourself, using the always fascinating Unix strace program:

linux> strace ./helloexecve("./hello", ["hello"], [/* ... */])....write(1, "hello\n", 6) = 6...exit_group(0) = ?

#include <stdio.h>

int main(){ printf("h"); printf("e"); printf("l"); printf("l"); printf("o"); printf("\n"); fflush(stdout); exit(0);} 30

Page 31: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

31

Unix I/O, Standard I/O, and Robust I/O

Unix I/O functions (accessed via system calls)

Standard I/O functions

C application program

fopen fdopenfread fwrite fscanf fprintf sscanf sprintf fgets fputs fflush fseekfclose

open readwrite lseekstat close

rio_readnrio_writenrio_readinitbrio_readlinebrio_readnb

RIOfunctions

• Standard I/O and RIO are implemented using low-level Unix I/O

• Which ones should you use in your programs?

Page 32: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Pros and Cons of Unix I/O

• Pros– Unix I/O is the most general and lowest

overhead form of I/O.• All other 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.

32

Page 33: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Pros and Cons of Unix I/O

• Cons– Dealing with short counts is tricky and error

prone.– Efficient reading of text lines requires some form

of buffering, also tricky and error prone.– Both of these issues are addressed by the

standard I/O and RIO packages.

33

Page 34: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Pros and Cons of Standard I/O

• Pros:– Buffering increases efficiency by decreasing the

number of read and write system calls– Short counts are handled automatically

34

Page 35: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Pros and Cons of Standard I/O

• Cons:– Provides no function for accessing file metadata– Standard 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 sockets• There are poorly documented restrictions on streams

that interact badly with restrictions on sockets (Sec 10.9)

35

Page 36: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Choosing I/O Functions

• General rule: use the highest-level I/O functions you can– Many C programmers are able to do all of their

work using the standard I/O functions

36

Page 37: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Choosing I/O Functions

• When to use standard I/O– When working with disk or terminal files

• When to use raw Unix I/O – Inside signal handlers, because Unix I/O is async-

signal-safe.– In rare cases when you need absolute highest

performance.

• When to use RIO– When you are reading and writing network

sockets.– Avoid using standard I/O on sockets. 37

Page 38: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Aside: Working with Binary Files

• Binary File Examples– Object code, Images (JPEG, GIF),

• Functions you shouldn’t use on binary files– Line-oriented I/O such as fgets, scanf, printf, rio_readlineb

• Different systems interpret 0x0A (‘\n’) (newline) differently:

– Linux and Mac OS X: LF(0x0a) [‘\n’]– HTTP servers & Windoes: CR+LF(0x0d 0x0a) [‘\r\n’]

• Use rio_readn or rio_readnb instead

– String functions• strlen, strcpy• Interprets byte value 0 (end of string) as special

38

Page 39: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

For Further Information

• The Unix bible:– W. Richard Stevens & Stephen A. Rago, Advanced

Programming in the Unix Environment, 2nd Edition, Addison Wesley, 2005

• Updated from Stevens’s 1993 classic text.

• Stevens is arguably the best technical writer ever.– Produced authoritative works in:

• Unix programming• TCP/IP (the protocol that makes the Internet work)• Unix network programming• Unix IPC programming

• Tragically, Stevens died Sept. 1, 1999– But others have taken up his legacy

39

Page 40: 1 System-Level I/O. 2 Outline I/O redirection Robust I/O Standard I/O Suggested Reading –10.4, 10.7~10.9

Next

• Network Programming

40