DOC PREVIEW
CMU CS 15213 - System-Level I/O

This preview shows page 1-2-3-23-24-25-26-47-48-49 out of 49 pages.

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

Unformatted text preview:

15 213 The course that gives CMU its Zip System Level I O October 9 2008 Topics lecture 13 ppt Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O Announcements Final exam day time announced by CMU 8 30 11 30am on Friday December 12 Cheating please please don t Writing code together counts as sharing code forbidden Pair programming even w o looking at other s code forbidden z describing code line by line counts the same as sharing code Opening up code and then leaving it for someone to enjoy forbidden z in fact please remember to use protected directories and screen locking Talking through a problem can include pictures not code ok The automated tools for discovering cheating are incredibly good z please don t test them Everyone has been warned multiple times z cheating on the remaining labs will receive no mercy 2 15 213 F 08 Unix Files A Unix file is a sequence of m bytes B0 B1 Bk Bm 1 All I O devices are represented as files dev sda2 usr disk partition dev tty2 terminal Even the kernel is represented as a file 3 dev kmem kernel memory image proc kernel data structures 15 213 F 08 Unix File Types Regular file File containing user app data binary text whatever OS does not know anything about the format z other than sequence of bytes akin to main memory Directory file A file that contains the names and locations of other files Character special and block special files Terminals character special and disks block special FIFO named pipe A file type used for inter process communication Socket 4 A file type used for network comm between processes 15 213 F 08 Unix I O Key Features Elegant mapping of files 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 operations system calls Opening and closing files z open and close Reading and writing a file z read and write Changing the current file position seek z indicates next offset into file to read or write z Lseek B0 B1 5 Bk 1 Bk Bk 1 Current File Position k 15 213 F 08 Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal 6 0 standard input 1 standard output 2 standard error 15 213 F 08 Closing Files Closing a file informs the kernel that you are finished accessing that file int fd file descriptor int retval return value if retval close fd 0 perror close exit 1 Closing 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 functions such as close 7 15 213 F 08 Reading Files Reading a file copies bytes from the current file position to memory and then updates file position 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 file fd into buf 8 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 15 213 F 08 Writing Files Writing a file copies bytes from memory to the current file position and then updates current file position 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 written from buf to file fd 9 nbytes 0 indicates that an error occurred As with reads short counts are possible and are not errors 15 213 F 08 Simple Unix I O example Copying standard in to standard out one byte at a time int 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 0 printf read from stdin failed exit 10 exit 0 10 15 213 F 08 File Metadata Metadata is data about data in this case file data Per file metadata maintained by kernel accessed by users with the stat and fstat functions Metadata returned by the stat and fstat functions struct stat dev t st dev device ino t st ino inode mode t st mode protection and file type nlink t st nlink number of hard links uid t st uid user ID of owner gid t st gid group ID of owner dev t st rdev device type if inode device off t st size total size in bytes unsigned long st blksize blocksize for filesystem I O unsigned long st blocks number of blocks allocated time t st atime time of last access time t st mtime time of last modification time t st ctime time of last change 15 213 F 08 11 Example of Accessing File Metadata statcheck c Querying and manipulating a file s meta data include csapp h int main int argc char argv struct stat stat char type readok unix type unix unix type unix type unix type statcheck statcheck c regular read yes chmod 000 statcheck c statcheck statcheck c regular read no statcheck directory read yes statcheck dev kmem other read yes Stat argv 1 stat if S ISREG stat st mode type regular else if S ISDIR stat st mode type directory else type other if stat st mode S IRUSR OK to read readok yes else readok no printf type s read s n type readok exit 0 12 15 213 F 08 Repeated slide Opening Files Opening a file informs the kernel that you are getting ready to access that file int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 Returns a small identifying integer file descriptor fd 1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal 13 0 standard input stdin 1 standard output stdout 2 standard error stderr 15 213 F 08 How the Unix Kernel Represents Open Files Two descriptors referencing two distinct open disk files Descriptor 1 stdout points to terminal and descriptor 4 points to open disk file Descriptor table one table per process Open file table shared by all processes v node table shared by all processes File A terminal File pos refcnt 1 File B disk File pos refcnt 1 Info in stat struct File access File size File type 14 File access …


View Full Document

CMU CS 15213 - System-Level I/O

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 System-Level I/O
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 System-Level I/O 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 System-Level I/O 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?