DOC PREVIEW
CMU CS 15213 - Lecture

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Carnegie Mellon System Level I O 15 213 Introduc0on to Computer Systems 14th Lecture Oct 12 2010 Instructors Randy Bryant and Dave O Hallaron 1 Carnegie Mellon Today Unix I O RIO robust I O package Metadata sharing and redirecEon Standard I O Conclusions and examples 2 Carnegie Mellon Unix Files A Unix le is a sequence of m bytes B0 B1 Bk Bm 1 All I O devices are represented as les dev sda2 usr disk par00on dev tty2 terminal Even the kernel is represented as a le dev kmem proc kernel memory image kernel data structures 3 Carnegie Mellon Unix File Types Regular le File containing user app data binary text whatever OS does not know anything about the format other than sequence of bytes akin to main memory Directory le A le that contains the names and loca0ons of other les Character special and block special les Terminals character special and disks block special FIFO named pipe A le type used for inter process communica0on Socket A le type used for network communica0on between processes 4 Carnegie Mellon Unix I O Key Features Elegant mapping of les to devices allows kernel to export simple interface called Unix I O Important idea All input and output is handled in a consistent and uniform way Basic Unix I O operaEons system calls Opening and closing les open and close Reading and wri0ng a le read and write Changing the current le posi on seek indicates next o set into le to read or write lseek B0 B1 Bk 1 Bk Bk 1 Current le posiEon k 5 Carnegie Mellon Opening Files Opening a le informs the kernel that you are geUng ready to access that le int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small idenEfying integer le descriptor fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open les associated with a terminal 0 standard input 1 standard output 2 standard error 6 Carnegie Mellon Closing Files Closing a le informs the kernel that you are nished accessing that le int fd file descriptor int retval return value if retval close fd 0 perror close exit 1 Closing an already closed le is a recipe for disaster in threaded programs more on this later Moral Always check return codes even for seemingly benign funcEons such as close 7 Carnegie Mellon Reading Files Reading a le copies bytes from the current le posiEon to memory and then updates le posiEon char buf 512 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 fd buf sizeof buf 0 perror read exit 1 Returns number of bytes read from le fd into buf Return type ssize t is signed integer nbytes 0 indicates that an error occurred Short counts nbytes sizeof buf are possible and are not errors 8 Carnegie Mellon WriEng Files WriEng a le copies bytes from memory to the current le posiEon and then updates current le posiEon char buf 512 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 Returns number of bytes wriXen from buf to le fd nbytes 0 indicates that an error occurred As with reads short counts are possible and are not errors 9 Carnegie Mellon Simple Unix I O example Copying standard in to standard out one byte at a Eme include csapp h int main void char c while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 cpstdin c Note the use of error handling wrappers for read and write Appendix A 10 Carnegie Mellon Dealing with Short Counts Short counts can occur in these situaEons Encountering end of le EOF on reads Reading text lines from a terminal Reading and wri0ng network sockets or Unix pipes Short counts never occur in these situaEons Reading from disk les except for EOF Wri0ng to disk les One way to deal with short counts in your code Use the RIO Robust I O package from your textbook s csapp c le Appendix B 11 Carnegie Mellon Today Unix I O RIO robust I O package Metadata sharing and redirecEon Standard I O Conclusions and examples 12 Carnegie Mellon The RIO Package RIO is a set of wrappers that provide e cient and robust I O in apps such as network programs that are subject to short counts RIO provides two di erent kinds of funcEons Unbu ered input and output of binary data rio readn and rio writen Bu ered input of binary data and text lines rio readlineb and rio readnb Bu ered RIO rou0nes are thread safe and can be interleaved arbitrarily on the same descriptor Download from hXp csapp cs cmu edu public code html src csapp c and include csapp h 13 Carnegie Mellon Unbu ered RIO Input and Output Same interface as Unix read and write Especially useful for transferring data on network sockets include csapp h ssize 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 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 14 Carnegie Mellon ImplementaEon 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 if nread read fd bufp nleft 0 if errno EINTR interrupted by sig handler return nread 0 and call read again else return 1 errno set by read else if nread 0 break EOF nleft nread bufp nread return n nleft return 0 csapp c 15 Carnegie Mellon Bu ered I O MoEvaEon ApplicaEons o en read write one character at a Eme getc putc ungetc gets fgets Read line of text on character at a 0me stopping at newline ImplemenEng as Unix I O calls expensive read and write require Unix kernel calls 10 000 clock cycles SoluEon Bu ered read Use Unix read to grab block of bytes User input func0ons take one byte at a 0me from bu er Re ll bu er when empty Bu er already read unread 16 Carnegie Mellon Bu ered I O ImplementaEon For reading from le File has associated bu er to hold bytes that have been read from le but not yet read by user code rio cnt Bu er already read rio buf unread rio bufptr Layered on Unix le Bu ered PorEon not in bu er already read unread unseen Current File PosiEon 17 Carnegie Mellon Bu ered I O DeclaraEon All informaEon …


View Full Document

CMU CS 15213 - Lecture

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?