Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
15-213, Fall 06 Outline Shell Lab Processes Signals.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
System-Level I/O May 28, 2008 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CSE 451 Section 4 Project 2 Design Considerations.
Processes, Signals, I/O, Shell lab
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
File IO and command line input CSE 2451 Rong Shi.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Operating Systems Process Creation
System-Level I/O.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
CS 105 “Tour of the Black Holes of Computing”
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Error handling I/O Man pages
Week 3 Redirection, Pipes, and Background
Operating Systems Moti Geva
Robust I/O package Chapter 11 practice problems
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Precept 14 : Ish dup() & Signal Handling
LINUX System : Lecture 8 Programming with Processes
Nikhil, Kashish, Krishanu, Stan
Pipe.
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
LINUX System Programming with Processes (additional)
Recitation 8 Processes, Signals, Tshlab
Programming Assignment # 2 – Supplementary Discussion
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Reference: Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series,
I/O and Process Management
IPC Prof. Ikjun Yeom TA – Hoyoun
CSCI 380: Operating Systems William Killian
dup, dup2 An existing file descriptor (filedes) is duplicated
CS 105 “Tour of the Black Holes of Computing”
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao Office hours: Thursdays 5-6PM Wean Hall 1315

Exam2 statistics 118 exams Highest score 74/74 (2 students) Lowest score 24 Average score Median score 49

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_PRELOAD export LD_PRELOAD= Make sure the heap is initialized

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

Different error handling styles Unix-style e.g. kill, signal, fork, etc. Posix-style e.g. pthread_create DNS-style e.g. gethostbyname

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”);

Unix-style error handling cont’d if ((pid = wait(NULL)) < 0) { perror(“Error in wait”); exit (0); }

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”);

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”);

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

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”); }

I/O practice problems from Chapter ~ 11.5

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?

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"

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

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); }

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.txt The output is c = f and not c = o

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); }

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

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

Answer to 11.4 dup2(5,0); or dup2(5,STDIN_FILENO);

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); }

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

Have a good thanksgiving break!