DOC PREVIEW
CMU CS 15213 - Recitation

This preview shows page 1-2-20-21 out of 21 pages.

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

Unformatted text preview:

15213 Recitation Section C Shimin Chen Nov 18 2002 Outline Robust I O package Chapter 11 practice problems Important Dates Lab 6 Malloc due on Thursday Nov 21 Lab 7 Proxy due on Thursday Dec 5 Final Exam Tuesday Dec 17 15213 Recitation C 2 Shimin Chen Robust 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 O 15213 Recitation C 3 Shimin Chen Rio Unbuffered Input Output Use Unix I O No internal buffering Useful for reading writing binary data to from networks ssize t rio readn int fd void usrbuf size t n reads n bytes from fd and put into usrbuf only returns short count on EOF ssize t rio writen int fd void usrbuf size t n writes n bytes from usrbuf to fd never returns short count 15213 Recitation C 4 Shimin Chen RIO Buffered Input Internal buffers define RIO BUFSIZE 8192 typedef 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 15213 Recitation C 5 Shimin Chen Rio Buffered Input void 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 rp ssize 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 read 15213 Recitation C 6 Shimin Chen rio readlineb ssize 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 15213 Recitation C 7 Shimin Chen Interleaving RIO Read Functions Do not interleave calls on the same fd between the buffered and unbuffered functions Within each set it is ok unbuffered buffered rio readinitb rio readn rio writen rio readlineb rio readnb Why 15213 Recitation C 8 Shimin Chen Rio 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 process 15213 Recitation C 9 Shimin Chen Problems from Chapter 11 11 1 11 5 15213 Recitation C 10 Shimin Chen Problem 11 1 What is the output of the following program 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 15213 Recitation C 11 Shimin Chen Answer 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 15213 Recitation C 12 Shimin Chen File 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 processes 15213 Recitation C 13 Shimin Chen Problem 11 2 Suppose 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 15213 Recitation C 14 Shimin Chen Answer to 11 2 The 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 is c f and not c o as you might have thought initially 15213 Recitation C 15 Shimin Chen Problem 11 3 As 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 15213 Recitation C 16 Shimin Chen Answer to 11 3 Child 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 o 15213 Recitation C 17 Shimin Chen Problem 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 newfd 15213 Recitation C 18 Shimin Chen Answer to 11 4 dup2 5 0 or dup2 5 STDIN FILENO 15213 Recitation C 19 Shimin Chen Problem 11 5 Assuming 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 15213 Recitation C 20 Shimin Chen Answer to 11 5 We 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 o 15213 Recitation C 21 Shimin Chen


View Full Document

CMU CS 15213 - Recitation

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

47 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 Recitation
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 Recitation 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 Recitation 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?