DOC PREVIEW
UNM ECE 335 - 14-io

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

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

Unformatted text preview:

University of New Mexico1System-Level I/OECE 335: Integrated Software SystemsInstructor:Edward NavaUniversity of New Mexico2Today Unix I/O RIO (robust I/O) package Metadata, sharing, and redirection Standard I/O Conclusions and examplesUniversity of New Mexico3Unix Files A Unix file is a sequence of m bytes: B0 , B1 , .... , Bk, .... , Bm-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)University of New Mexico4Unix File Types Regular file File containing user/app data (binary, text, whatever) OS does not know anything about the format other than “sequence of bytes”, akin to main memory Directory file A file that contains the names and locations of other files Character special and block special files Terminals (character special) and disks (block special) FIFO (named pipe) A file type used for inter-process communication Socket A file type used for network communication between processesUniversity of New Mexico5Unix I/O Key Features Elegant mapping of files to devices allows kernel to export simple interface called Unix I/O Important 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() Reading and writing a file read() and write() Changing the current file position (seek) indicates next offset into file to read or write lseek()B0B1• • • Bk-1BkBk+1• • •Current file position = kUniversity of New Mexico6Opening 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 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 errorint fd; /* file descriptor */if ((fd = open("/etc/hosts", O_RDONLY)) < 0) {perror("open");exit(1);}University of New Mexico7Closing 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()int fd; /* file descriptor */int retval; /* return value */if ((retval = close(fd)) < 0) {perror("close");exit(1);}University of New Mexico8Reading 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 Return type ssize_t is signed integer 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);}University of New Mexico9Writing 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!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);}University of New Mexico10Simple Unix I/O example Copying standard in to standard out, one byte at a time#include "csapp.h"int main(void){char c;while(Read(STDIN_FILENO, &c, 1) != 0)Write(STDOUT_FILENO, &c, 1);exit(0);}Note the use of error handling wrappers for read and write (Appendix A).cpstdin.cUniversity of New Mexico11Dealing 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 One way to deal with short counts in your code: Use the RIO (Robust I/O) package from your textbook’s csapp.cfile (Appendix A)University of New Mexico12Today Unix I/O RIO (robust I/O) package Metadata, sharing, and redirection Standard I/O Conclusions and examplesUniversity of New Mexico13The RIO Package RIO is a set of wrappers that provide efficient and robust I/O in apps, such as network programs that are subject to short counts RIO provides two different kinds of functions Unbuffered input and output of binary data rio_readn and rio_writen Buffered input of binary data and text lines rio_readlineb and rio_readnb Buffered RIO routines are thread-safe and can be interleaved arbitrarily on the same descriptor Download from http://csapp.cs.cmu.edu/public/code.html src/csapp.c and include/csapp.hUniversity of New Mexico14Unbuffered RIO Input and Output Same interface as Unix read and write Especially useful for transferring data on network sockets rio_readn returns short count only if it encounters EOF Only use it when you know how many bytes to read rio_writen never returns a short count Calls to rio_readn and rio_writen can be interleaved arbitrarily on the same descriptor#include "csapp.h"ssize_t rio_readn(int fd, void *usrbuf, size_t n);ssize_t rio_writen(int fd, void *usrbuf, size_t n);Return: num. bytes transferred if OK, 0 on EOF (rio_readn only), -1 on errorUniversity of New Mexico15Implementation 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 */elsereturn -1; /* errno set by read() */ } else if (nread == 0)break; /* EOF */nleft -= nread;bufp += nread;}return (n - nleft); /* return >= 0 */}csapp.cUniversity of New Mexico16Buffered I/O: Motivation Applications often read/write one character at a time getc, putc, ungetc


View Full Document

UNM ECE 335 - 14-io

Documents in this Course
Final

Final

7 pages

Load more
Download 14-io
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 14-io 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 14-io 2 2 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?