Recitation 11: I/O Problems Andrew Faulring 15213 Section A 18 November 2002.

Slides:



Advertisements
Similar presentations
4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
Advertisements

1. I/O. I/O Unix I/O RIO (robust I/O) package Metadata, sharing, and redirection Standard I/O Conclusions and examples.
Carnegie Mellon 1 System-Level I/O : Introduction to Computer Systems 14 th Lecture, Oct. 12, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
System-Level I/O November 4, 2003 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
System-Level I/O October 8, 2008 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O class14.ppt.
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.
System-Level I/O November 11, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
System-Level I/O Nov 14, 2002 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O class24.ppt
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
I/O April 16, 2002 Topics Files Unix I/O Standard I/O Reading: (Beta) or (New) Problems: 12.9 (Beta) or 12.4 (New) class24.ppt
System-Level I/O May 22, 2006 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Introduction to Operating Systems File I/O Most of the following slides are adapted from slides of Gregory Kesden and Markus Püschel of Carnegie Mellon.
System-level I/O Alan L. Cox Some slides adapted from CMU slides.
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”
System-Level I/O Topics Unix I/O Robust reading and writing
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 18, 2012 Instructors: Dave O’Hallaron, Greg Ganger,
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.
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.
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.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 16, 2014 Instructors: Greg Ganger, Greg Kesden,
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.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 25, 2013 Instructors: Gregory Kesden.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
System-Level I/O March 25, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
Lee CSCE 312 TAMU 1 System Level I/O Instructor: Dr. Hyunyoung Lee Based on slides provided by Randy Bryant and Dave O’Hallaron.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
System-Level I/O.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
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.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
1 System-Level I/O. 2 Outline Unix I/O Reading File Metadata Sharing Files & I/O redirection Robust I/O Standard I/O Suggested Reading –10.1~10.3, 10.5~10.7,
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition System-Level I/O : Introduction to Computer.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 24, 2014 Instructors: Gregory Kesden.
Error handling I/O Man pages
Robust I/O package Chapter 11 practice problems
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
System-Level I/O.
System-Level I/O : Introduction to Computer Systems
Introduction to Operating Systems File I/O
CS 105 “Tour of the Black Holes of Computing”
System-Level I/O Topics Unix I/O Robust reading and writing
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
System-Level I/O October 9, 2008
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Programming Assignment # 2 – Supplementary Discussion
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,
IPC Prof. Ikjun Yeom TA – Hoyoun
System-Level I/O.
Section 3 Syscalls, I/O, Signals February 3rd, 2017 Taught by Josh Don.
CS 105 “Tour of the Black Holes of Computing”
Instructors: David Trammell
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002

Logistics Office hours –NSH 2504 –Permanently moving to Tuesday 2–3 What’s left –Lab 6 Malloc: due on Thursday, 21 Nov –Lab 7 Proxy: due on Thursday, 5 Dec –Final Exam: 8:30am on Tuesday, 17 Dec, in Porter Hall 100

Today’s Plan Robust I/O Chapter 11 Practice Problems

Why Use Robust I/O Handles interrupted system calls –Signal handlers Handles short counts –Encountering end-of-file (EOF) on reads (disk files) –Reading text lines from a terminal –Reading and writing network sockets or Unix pipes Useful in network programs –Subject to short counts –Internal buffering constraints –Long network delays –Unreliable

Rio: Unbuffered Input/Output Transfer data directly between memory and a file No application level buffering Useful for reading/writing binary data to/from networks –(Though text strings are binary data.) ssize_t rio_readn(int fd, void* usrbuf, size_t n) –Reads n bytes from fd into usrbuf –Only returns short on EOF ssize_t rio_writen(int fd, void* usrbuf, size_t n) –Writes n bytes from usrbuf to file fd –Never returns short count

Rio: Buffered Input void rio_readinitb(rio_t* rp, int fd); –Called only once per open file descriptor –Associates fd with a read buffer rp ssize_t rio_readlineb(rio_t* rp, void* usrbuf, size_t maxlen); –For reading lines from a text file only –Read a line (stop on ‘\n’) or maxlen-1 chars from file rp to usrbuf –Terminate the text line with null (zero) character –Returns number of chars read ssize_t rio_readnb(rio_t* rp, void* usrbuf, size_t n); –For both text and binary files –Reads n bytes from rp into usrbuf –Result string is NOT null-terminated! –Returns number of chars read

rio_readlineb ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) { int n, rc; char c, *bufp = usrbuf; for (n = 1; n < maxlen; n++) { if ((rc = rio_read(rp, &c, 1)) == 1) { *bufp++ = c; if (c == '\n') break; } else if (rc == 0) { if (n == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ } else return -1; /* error */ } *bufp = 0; return n; }

Do not interleave rio_readinitb rio_readlineb rio_readnb rio_readn rio_writen Do not interleave calls on the same file descriptor to these two sets of functions Why?

Rio Error Checking All functions have upper case equivalents ( Rio_readn … ), which call unix_error if the function encounters an error –Short reads are not errors –Also handles interrupted system calls –But does not ignore EPIPE errors, which are not fatal errors for Lab 7

Problems from Chapter –11.5 Handout contains the problems

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 Default file descriptors: –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

Kernel Structure for Open Files Descriptor table –One per process –Children inherit from parents File Table –The set of all open files –Shared by all processes –Reference count of number of file descriptors pointing to each entry V-node table –Contains information in the stat structure –Shared by all processes

Problem 11.2 Suppose that the disk file foobar.txt consists of the 6 ASCII characters "foobar". Then 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 Two descriptors fd1 and fd2 Two open file table entries, each with their own file positions for foobar.txt The read from fd2 also reads the first byte of foobar.txt So, the output is c = f and not c = o

Problem 11.3 As before, suppose the disk file foobar.txt consists of 6 ASCII characters "foobar". Then 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 and parent share an open file table entry (refcount == 2). Hence they share a file position! The output is 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 Assuming that the disk file foobar.txt consists of 6 ASCII characters "foobar". Then 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 We are redirecting fd1 to fd2. So the second Read uses the file position offset of fd2. c = o