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”

Slides:



Advertisements
Similar presentations
© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
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.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
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.
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.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
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.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
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.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 16, 2014 Instructors: Greg Ganger, Greg Kesden,
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
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.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
CS 105 “Tour of the Black Holes of Computing”
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,
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
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.
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 24, 2014 Instructors: Gregory Kesden.
Week 12 - Thursday CS222.
Robust I/O package Chapter 11 practice problems
System-Level I/O.
Supplementary Material on Unix System Calls and Standard I/O
System-Level I/O : Introduction to Computer Systems
Introduction to Operating Systems File I/O
Files CMSC 15400: Introduction to Computer Systems Chapter 10
CS 105 “Tour of the Black Holes of Computing”
System-Level I/O Topics Unix I/O Robust reading and writing
System-Level I/O October 9, 2008
“The course that gives CMU its Zip!”
“The course that gives CMU its Zip!”
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,
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
IPC Prof. Ikjun Yeom TA – Hoyoun
System-Level I/O.
System-Level I/O CSCI 380: Operating Systems
CSCI 380: Operating Systems William Killian
Chien-Chung Shen CIS, UD
CS 105 “Tour of the Black Holes of Computing”
Instructors: David Trammell
Presentation transcript:

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”

– 2 – CS 105 Unix Files A Unix file is a sequence of m bytes: B 0, B 1,...., B k,...., B m-1 All I/O devices are represented as files: /dev/sda2 ( /usr disk partition) /dev/tty2 (terminal) Even the kernel is represented as a file: /dev/kmem (kernel memory image) /proc (kernel data structures)

– 3 – CS 105 Unix File Types Regular file: binary or text. Unix does not know the difference! Directory file: contains the names and locations of other files Character special file: keyboard and network, for example Block special file: structured like disks FIFO (named pipe): used for interprocess communication Socket: used for network communication between processes

– 4 – CS 105 Unix I/O The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. Key Unix idea: All input and output is handled in a consistent and uniform way. Basic Unix I/O operations (system calls): Opening and closing files: open() and close() Changing the current file position (seek): lseek, etc. Reading and writing a file: read() and write()

– 5 – CS 105 Opening Files Opening a file informs the kernel that you are getting ready to access that file. Returns a small identifying integer file descriptor fd == -1 indicates that an error occurred strerror converts to English (note: use sterror_r fo thread safety) Each process created by a Unix shell begins life with three open files associated with a terminal: 0: standard input 1: standard output 2: standard error int fd; /* file descriptor */ if ((fd = open(“/etc/hosts”, O_RDONLY)) < 0) { perror(“open”); exit(1); }

– 6 – CS 105 Closing Files Closing a file informs the kernel that you are finished accessing that file. Closing an already closed file is a recipe for disaster in threaded programs (more on this later) Moral: Always check return codes, even for seemingly benign functions such as close() perror is simplified strerror/fprintf; int fd; /* file descriptor */ int retval; /* return value */ if ((retval = close(fd)) < 0) { perror(“close”); exit(1); }

– 7 – CS 105 Reading Files Reading a file copies bytes from the current file position to memory, and then updates file position. Returns number of bytes read from file fd into buf nbytes < 0 indicates that an error occurred. short counts ( nbytes < sizeof(buf) ) are possible and are not errors! char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open file fd... */ /* Then read up to 512 bytes from file fd */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { perror(“read”); exit(1); }

– 8 – CS 105 Writing Files Writing a file copies bytes from memory to the current file position, and then updates current file position. Returns number of bytes written from buf to file fd. nbytes < 0 indicates that an error occurred. As with reads, short counts are possible and are not errors! Transfers up to 512 bytes from address buf to file fd char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open the file fd... */ /* Then write up to 512 bytes from buf to file fd */ if ((nbytes = write(fd, buf, sizeof(buf)) < 0) { perror(“write”); exit(1); }

– 9 – CS 105 Simple Example Copying standard input to standard output one byte at a time. Note the use of error handling wrappers for read and write (text has details). #include "csapp.h" int main(void) { char c; while(Read(STDIN_FILENO, &c, 1) != 0) Write(STDOUT_FILENO, &c, 1); exit(0); }

– 10 – CS 105 Dealing with Short Counts Short counts can occur in these situations: Encountering (end-of-file) EOF on reads. Reading text lines from a terminal. Reading and writing network sockets or Unix pipes. Short counts never occur in these situations: Reading from disk files, except for EOF Writing to disk files. How should you deal with short counts in your code? Use the RIO (Robust I/O) package from your textbook’s csapp.c file (Appendix B). Use C stdio or C++ streams Ignore the problem and accept that your code is fragile

– 11 – CS 105 “Foolproof” I/O Low-level I/O is difficult because of short counts and other possible errors The text provides the RIO package, a good example of how to encapsulate low-level I/O RIO is a set of wrappers that provide efficient and robust I/O in applications such as network programs that are subject to short counts. Download from csapp.cs.cmu.edu/public/ics/code/src/csapp.c csapp.cs.cmu.edu/public/ics/code/include/csapp.h

– 12 – CS 105 Unbuffered I/O RIO provides buffered and unbuffered routines Unbuffered: Especially useful for transferring data on network sockets Same interface as Unix read and write rio_readn returns short count only if it encounters EOF. rio_writen never returns a short count. Calls to rio_readn and rio_writen can be interleaved arbitrarily on the same descriptor.

– 13 – CS 105 Implementation of rio_readn /* * rio_readn - robustly read n bytes (unbuffered) */ ssize_t rio_readn(int fd, void *usrbuf, size_t n) { size_t nleft = n; ssize_t nread; char *bufp = usrbuf; while (nleft > 0) { if ((nread = read(fd, bufp, nleft)) < 0) { if (errno == EINTR) /* interrupted by sig handler return */ nread = 0; /* and call read() again */ else return -1; /* errno set by read() */ } else if (nread == 0) break; /* EOF */ nleft -= nread; bufp += nread; } return (n - nleft); /* return >= 0 */ }

– 14 – CS 105 Buffered Input Buffered: Efficiently read text lines and binary data from a file partially cached in an internal memory buffer rio_readlineb reads a text line of up to maxlen bytes from file fd and stores the line in usrbuf. Especially useful for reading text lines from network sockets. rio_readnb reads up to n bytes from file fd. Calls to rio_readlineb and rio_readnb can be interleaved arbitrarily on the same descriptor. Warning: Don’t interleave with calls to rio_readn with calls to *b versions

– 15 – CS 105 Buffered Example Copying the lines of a text file from standard input to standard output. #include "csapp.h" int main(int argc, char **argv) { int n; rio_t rio; char buf[MAXLINE]; Rio_readinitb(&rio, STDIN_FILENO); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) Rio_writen(STDOUT_FILENO, buf, n); exit(0); }

– 16 – CS 105 I/O Choices Unix I/O Most general and basic; other libraries are implemented using it Unbuffered; efficient input requires buffering Tricky and error-prone; short counts, for example Standard I/O Buffered; cannot be used on network sockets Badly documented; potential interactions with other I/O on streams and sockets Not all info is available (see later slide on metadata)

– 17 – CS 105 I/O Choices, continued RIO Buffered and unbuffered Nicely packaged Author’s choice for sockets and pipes Non-standard, but built on Stevens’s work C++ streams Standard (sort of) Very complex Roll your own Time consuming Error-prone Unix Bible: W. Richard Stevens, Advanced Programming in the Unix Environment, Addison Wesley, 1993.

– 18 – CS 105 How the Unix Kernel Represents Open Files Two descriptors referencing two distinct open disk files. Descriptor 1 (stdout) points to terminal, and descriptor 4 points to open disk file. fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A (terminal) File B (disk) Info in stat struct

– 19 – CS 105 File Sharing Two distinct descriptors sharing the same disk file through two distinct open file table entries E.g., Calling open twice with the same filename argument fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... File access... File size File type File A File B

– 20 – CS 105 How Processes Share Files A child process inherits its parent’s open files. Here is the situation immediately after a fork fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor tables Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=2... File pos refcnt=2... Parent's table fd 0 fd 1 fd 2 fd 3 fd 4 Child's table File access... File size File type File access... File size File type File A File B

– 21 – CS 105 I/O Redirection Question: How does a shell implement I/O redirection? unix> ls > foo.txt Answer: By calling the dup2(oldfd, newfd) function Copies (per-process) descriptor table entry oldfd to entry n ewfd a b fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table before dup2(4,1) b b fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table after dup2(4,1)

– 22 – CS 105 File Metadata Metadata is data about data, in this case file data. Maintained by kernel, accessed by users with the stat and fstat functions. /* Metadata returned by the stat and fstat functions */ struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ mode_t st_mode; /* protection and file type */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device type (if inode device) */ off_t st_size; /* total size, in bytes */ unsigned long st_blksize; /* blocksize for filesystem I/O */ unsigned long st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last change */ };

– 23 – CS 105 Summary: Goals of Unix I/O Uniform view user does not see actual devices Devices and files look alike (almost) Uniform drivers across devices ATA disk looks same as IDE, SCSI… Tape looks almost like disk Support for many kinds of I/O Objects Regular files Directories Pipes and sockets Devices Even processes and kernel data Even processes and kernel data

– 24 – CS 105 Layers of I/O System –Lay Out 1.User Level – processes make I/O call, format I/O, spooling 2.Device-Independent software naming, protection blocking, buffering, allocation perform I/O functions common to all devices provide uniform interface to the user level software buffering, block sizes, etc. e.g., map symbolic device names onto driver 3.Device-Dependent software setup device registers, check status specific code for device operations, i.e., driver accept abstract requests, e.g., open, and execute 4.Hardware controller, map I/O ops to device ops, e.g., print, Interrupts