DOC PREVIEW
CMU CS 15213 - Recitation

This preview shows page 1-2-3-24-25-26 out of 26 pages.

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

Unformatted text preview:

15 213 Recitation 9 11 04 02 Annie Luo Outline Error Handling I O Linux man pages e mail luluo cs cmu edu Office Hours Thursday 6 00 7 00 Reminder Exam 2 Tuesday Nov 12 Review session next Monday evening L6 Due Friday Nov 19 This week s lectures Text book 10 9 Wean 8402 Coverage of Exam 2 Similar to last year s exam 2 Performance optimization Cache simulation Cache miss rate Cache miss analysis Processes Signals Virtual memory Come to the special recitation Mon 11 11 evening Location TBD Error Handling Should always check return code of system calls Not only for 5 points in your lab There are subtle ways that things can go wrong Use the status info kernel provides us Approach in this class Wrappers Different error handling styles Unix Style Posix Style DNS Style Unix Style Error Handling Special return value when encounter error always 1 Set global variable errno to an error code Indicates the cause of the error Use strerror function for text description of void unix error char msg errno fprintf stderr s s n msg strerror errno exit 0 if pid wait NULL 0 unix error Error in wait POSIX Style Error Handling Return value only indicate success 0 or failure nonzero Useful results returned in function arguments void posix error int code char msg fprintf stderr s s n msg strerror code exit 0 if retcode pthread create 0 posix error retcode Error in pthread DNS Style Error Handling Return a NULL pointer on failure Set the global h errno variable void dns error char msg fprintf stderr s DNS error d n msg h errno exit 0 if p gethostbyname name NULL dns error Error in gethostbyname Example Wrappers void Kill pid t pid int signum int rc if rc kill pid signum 0 unix error Kill error Appendix B csapp c and csapp h Unix Style for kill function Behaves exactly like the base function if no error Prints informative message and terminates the process Not All Errors are Fatal Wrappers are not always the correct path Treat all information as fatal errors Terminate the program with exit Sometimes an error is not fatal void sigchld handler int signum pid t pid while pid waitpid 1 NULL 0 0 printf Reaped d n int pid if errno ECHILD unix error waitpid error I O Full coverage in Lecture 24 and Chapter 11 in textbook But folks were having some issues with the Shell Lab fflush printf fprintf scanf fscanf And these issues will pop up with the Malloc Lab Unix I O Why need I O copy data between main memory and external devices All devices modeled as files in Unix Disk drives terminals networks A Unix file is a sequence of bytes Input Output performed by reading and writing files Kernel I O Why Kernel I O System level I O functions provided by the kernel Understand system concepts process VM etc When impossible inappropriate to use standard library I O Info such as file size creation time Networking programming How kernel I O works Kernel maintains all file information Apps only keep track of file descriptor returned from kernel Functions Open int open const char pathname int flags Return value is a small integer file descriptor Special file descriptors Defined in unistd h Default open with each shell created process Standard Input descriptor 0 Standard Output descriptor 1 Standard Error descriptor 2 Functions Read and Write ssize t read int fd void buf size t count Copy count 0 bytes from a file fd to memory buf From current position k maintained by kernel Trigger EOF when k is greater than file size No EOF character ssize t write int fd void buf size t count Copy count 0 bytes from memory buf to a file fd Functions Close int close int fd Kernel frees data structures created by file open if any Restores file descriptor to a pool of available descriptors What if a process terminates Kernel closes all open files and free memory for this proc Standard I O Higher level I O functions fopen fclose fread fwrite fgets fputs scanf and printf formated I O Eventually calls the kernel I O routines Models an open file as a stream A pointer to FILE Abstraction for file descriptor and for a stream buffer Why use stream buffer Reduce expensive Unix system calls Example buffered io c include stdio h int main void printf 1 printf 5 printf 2 printf 1 printf 3 return 0 How many system calls kernel I O routine used Use strace to Check strace program Runs program and prints out info about all the system calls Let s run strace on buffered io unix strace buffered io write 1 15213 515213 5 File Streams C Library equivalent of file descriptors More formatted I O routines fprintf and fscanf Take an extra first argument FILE int printf const char format int fprintf FILE stream const char format printf args fprintf stdout args scanf args fscanf stdin args So Which Should I Use Use standard I O routines for disks and terminals Full duplex can input and output on the same stream Nasty restrictions when use stdio on sockets Input cannot follow output and vice versa without fflush fseek fsetpos or rewind Flush the buffer for the first restriction Use two streams for the second restriction So do not use standard I O functions for network sockets Flushing a File System stderr is not buffered stdout is line buffered when it points to a terminal Partial lines not appear until fflush or exit Can produce unexpected results esp with debugging output There may also be input buffering when stdin points to a terminal Flushing a File System cont Forces a write of all buffered data int fflush FILE stream fflush stdout Example buffered io flush c include stdio h int main void printf 1 printf 5 fflush stdout printf 2 printf 1 printf 3 return 0 strace Revised Let s run strace on buffered io flush unix strace buffered io flush write 1 15 215 2 write 1 213 3213 3 Unix Man pages unix man kill KILL 1 Linux Programmer s Manual KILL 1 NAME kill terminate a process SYNOPSIS kill s signal p kill l signal a pid DESCRIPTION kill sends the specified signal to the specified process If no signal is specified the TERM signal is sent The TERM signal will kill processes which do not catch this signal For other processes if may be necessary to use the KILL 9 signal since this signal cannot be caught Most modern shells have a builtin kill function OPTIONS pid Specify the list of processes that kill should signal Each pid can be one of four things A process name in which case processes called that will Man Page Sections cont Section 1 Commands Stuff that you could run from a Unix prompt cp 1 bash 1 kill 1 Section 2 System Calls Talking with the Kernel kill 2 open 2 Section 3


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?