![Page 1: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/1.jpg)
Recitation 11 (Nov. 22)
Outline Lab 6: interposition test Error handling I/O practice problem
Reminders Lab 6:
Due Tuesday
Minglong [email protected]
Office hours:Thursdays 5-6PMWean Hall 1315
![Page 2: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/2.jpg)
Exam2 statistics
118 exams Highest score 74/74 (2 students) Lowest score 24 Average score 49.94 Median score 49
![Page 3: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/3.jpg)
L6: Interposition test
Build shared libraries mm.so and memlib.so
make mm.so memlib.so
gcc –fpic –shared –o mm.so mm-student.c
gcc –fpic –shared –o memlib.so memlib.c Set LD_PRELOAD
bash:export LD_PRELOAD=“/path/to/mm.so /path/to/memlib.so”
Remember to restore LD_PRELOADexport LD_PRELOAD=
Make sure the heap is initialized
![Page 4: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/4.jpg)
Error handling Always check return code of system calls
There are subtle ways that things can go wrong Use the status info kernel provides us
Appendix B
![Page 5: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/5.jpg)
Different error handling styles
Unix-style e.g. kill, signal, fork, etc.
Posix-style e.g. pthread_create
DNS-style e.g. gethostbyname
![Page 6: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/6.jpg)
Unix-style error handling Special return value when encounter error (always –1) Global variable errno set to an error code Use strerror function for text description of errno
Or use perror
void unix_error(char *msg){ fprintf(stderr, “%s: %s\n“,
msg, strerror(errno)); exit(0);}
if ((pid = wait(NULL)) < 0) unix_error(“Error in wait”);
![Page 7: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/7.jpg)
Unix-style error handling cont’d
if ((pid = wait(NULL)) < 0) { perror(“Error in wait”); exit (0); }
![Page 8: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/8.jpg)
Posix-style error handling Return value indicates 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”);
![Page 9: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/9.jpg)
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”);
![Page 10: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/10.jpg)
Appendix B: csapp.h and csapp.c Unix-Style, for kill function Behaves exactly like the base function if no error Prints informative message and terminates the process
void Kill (pid_t pid, int signum){ int rc; if((rc = kill(pid, signum)) <0)
unix_error(“Kill error”);}
Example: wrappers
![Page 11: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/11.jpg)
Handle errors gracefully The wrappers shown above calls exit()
In many situations, we want to handle errors more gracefully.
For example: web server, etc.
void sigchld_handler(int signum){ pid_t pid; while((pid = waitpid(…)) > 0) printf(“Reaped %d\n”, (int)pid); if(errno != ECHILD) unix_error(“waitpid error”);}
![Page 12: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/12.jpg)
I/O practice problems from Chapter 11
11.1 ~ 11.5
![Page 13: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/13.jpg)
Problem 11.1
#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);}
What is the output of the following program?
![Page 14: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/14.jpg)
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"
![Page 15: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/15.jpg)
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
![Page 16: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/16.jpg)
Problem 11.2
foobar.txt has 6 ASCII characters "foobar".
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);}
![Page 17: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/17.jpg)
Answer to 11.2
fd1 and fd2 have different open file table entries have their own file positions for foobar.txt fd2 reads the first byte of foobar.txtThe output is
c = f
and not
c = o
![Page 18: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/18.jpg)
Problem 11.3
foobar.txt has 6 ASCII characters "foobar".
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);}
![Page 19: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/19.jpg)
Answer to 11.3
Child inherits the parent’s descriptor table
Child & parent share an open file table entry (refcount = 2)
They share file position.
c = o
![Page 20: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/20.jpg)
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
![Page 21: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/21.jpg)
Answer to 11.4
dup2(5,0);
or
dup2(5,STDIN_FILENO);
![Page 22: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/22.jpg)
Problem 11.5
foobar.txt has 6 ASCII characters "foobar".
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);}
![Page 23: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/23.jpg)
Answer to 11.5
Redirect fd1 to fd2
fd1 points to the same open file table entry as fd2
The second Read uses the file position offset of fd2.
c = o
![Page 24: Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao shaoml+213@cs.cmu.edu](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfd31a28abf838cac03b/html5/thumbnails/24.jpg)
Have a good thanksgiving break!