DOC PREVIEW
CMU CS 15213 - Lecture

This preview shows page 1-2-3-27-28-29 out of 29 pages.

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

Unformatted text preview:

15 213 The course that gives CMU its Zip I O Nov 15 2001 Topics Files Unix I O Standard I O class24 ppt A typical hardware system CPU chip register file ALU system bus memory bus main memory I O bridge bus interface I O bus USB controller mouse keyboard graphics adapter disk controller Expansion slots for other devices such as network adapters monitor disk class24 ppt 2 CS 213 F 01 Reading a disk sector 1 CPU chip register file ALU CPU initiates a disk read by writing a command logical block number and destination memory address to a port address associated with disk controller main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk class24 ppt 3 CS 213 F 01 Reading a disk sector 2 CPU chip register file ALU Disk controller reads the sector and performs a direct memory access DMA transfer into main memory main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk class24 ppt 4 CS 213 F 01 Reading a disk sector 3 CPU chip register file ALU When the DMA transfer completes the disk controller notifies the CPU with an interrupt i e asserts a special interrupt pin on the CPU main memory bus interface I O bus USB controller mouse keyboard graphics adapter disk controller monitor disk class24 ppt 5 CS 213 F 01 Unix A Unix file is a sequence files of n bytes B0 B1 Bk Bn 1 All I O devices are represented as files dev sda2 dev tty2 usr disk partition terminal Even the kernel is represented as a file dev kmem kernel memory image kernel data structures proc Kernel identifies each file by a small integer descriptor 0 standard input 1 standard output 2 standard error class24 ppt 6 CS 213 F 01 Regular file Unix file types Binary or text file Unix does not know the difference Directory file A file that contains names and locations of other files Character special file For certain types of streaming devices e g terminals Block special file A file type typically used for disks FIFO named pipe A file type used for interprocess comunication Socket A file type used for network communication between processes class24 ppt 7 CS 213 F 01 I O functions fopen fwrite fscanf fseek fread fprintf fgets fclose open write stat read lseek close class24 ppt C Application program application process Standard I O functions buffered Unix I O system calls unbuffered kernel Hardware devices 8 CS 213 F 01 Standard I O vs Unix I O When should I use the Standard I O functions Whenever possible Many C programmers are able to do all of their work using the standard I O functions Why would I ever need to use the lower level Unix I O functions Fetching file metadata e g size modification date must use lower level stat function Doing network programming there are some bad interactions between sockets and the standard library functions Needing high performance class24 ppt 9 CS 213 F 01 Meta data returned by stat file info returned by the stat struct stat dev t st dev ino t st ino mode t st mode nlink t st nlink uid t st uid gid t st gid dev t st rdev off t st size unsigned long st blksize unsigned long st blocks time t st atime time t st mtime time t st ctime class24 ppt 10 function device inode protection and file type number of hard links user ID of owner group ID of owner device type if inode device total size in bytes blocksize for filesystem I O number of blocks allocated time of last access time of last modification time of last change CS 213 F 01 Kernel s file representation Before fork file A parent s per process descriptor table file pos refcnt 1 i node table entries file A st mode st size file B file B file pos refcnt 1 class24 ppt 11 st mode st size CS 213 F 01 Kernel s file representation cont After fork processes inherit open file tables parent s per process descriptor table file A file pos refcnt 2 i node table entries file A st mode st size file B child s per process descriptor table class24 ppt file B file pos refcnt 2 12 st mode st size CS 213 F 01 Unix file I O open Must open a file before you can do anything else open returns a small integer file descriptor fd 0 indicates that an error occurred predefined file descriptors 0 stdin 1 stdout 2 stderr int fd file descriptor if fd open etc hosts O RDONLY 0 perror open exit 1 class24 ppt 13 CS 213 F 01 Unix file I O read read allows a program to fetch the contents of a file from the current file position char buf 512 int fd file descriptor int nbytes number of bytes read open the file read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 read returns the number of bytes read from file fd nbytes 0 indicates that an error occurred short counts nbytes sizeof buf are possible and not errors places nbytes bytes into memory address buf class24 ppt 14 CS 213 F 01 Unix file I O write write allows a program to modify the file contents starting at the current file position char buf 512 int fd file descriptor int nbytes number of bytes read open the file write up to 512 bytes from buf to file fd if nbytes write fd buf sizeof buf 0 perror write exit 1 write returns the number of bytes written from buf to file fd nbytes 0 indicates that an error occurred short counts are possible and are not errors class24 ppt 15 CS 213 F 01 Robustly dealing with read short counts ssize t readn int fd void from buf size t count adapted Stevens size t nleft count ssize t nread char ptr buf while nleft 0 if nread read fd ptr nleft 0 if errno EINTR nread 0 and call read again else return 1 errno set by read else if nread 0 break EOF nleft nread ptr nread return count nleft return 0 class24 ppt 16 CS 213 F 01 Robustly dealing with write short counts adapted from Stevens ssize t writen int fd const void buf size t count size t nleft count ssize t nwritten const char ptr buf while nleft 0 if nwritten write fd ptr nleft 0 if errno EINTR nwritten 0 and call write again else return 1 errorno set by write nleft nwritten ptr nwritten return count class24 ppt 17 CS 213 F 01 Unix file I O lseek lseek changes the current file position int fd file descriptor open the file Move the file position to byte offset 100 if lseek fd 100 SEEK SET 0 perror lseek exit 1 lseek changes the current file position class24 ppt 18 CS 213 F 01 Unix file I O dup2 …


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

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