+ All Categories
Home > Documents > Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105...

Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105...

Date post: 12-Jan-2016
Category:
Upload: duane-lester
View: 224 times
Download: 3 times
Share this document with a friend
24
Input and Output Topics Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
Transcript
Page 1: 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”

Input and OutputInput and Output

TopicsTopics I/O hardware Unix file abstraction Robust I/O File sharing

io.ppt

CS 105“Tour of the Black Holes of Computing”

Page 2: 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 FilesUnix Files

A Unix A Unix filefile is a sequence of is a sequence of mm bytes: bytes: B0, B1, .... , Bk , .... , Bm-1

All I/O devices are represented as files:All I/O devices are represented as files: /dev/sda2 (/usr disk partition) /dev/tty2 (terminal)

Even the kernel is represented as a file:Even the kernel is represented as a file: /dev/kmem (kernel memory image) /proc (kernel data structures)

Page 3: 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”

– 3 – CS 105

Unix File TypesUnix File Types

Regular file: binary or text. Unix does not know the Regular file: binary or text. Unix does not know the difference!difference!

Directory file: contains the names and locations of other Directory file: contains the names and locations of other filesfiles

Character special file: keyboard and network, for exampleCharacter special file: keyboard and network, for example

Block special file: structured like disks Block special file: structured like disks

FIFO (named pipe): used for interprocess communicationFIFO (named pipe): used for interprocess communication

Socket: used for network communication between Socket: used for network communication between processesprocesses

Page 4: 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”

– 4 – CS 105

Unix I/OUnix I/O

The The elegantelegant mapping of files to devices allows kernel to mapping of files to devices allows kernel to export simple interface called Unix I/O.export simple interface called Unix I/O.

Key Unix idea: All input and output is handled in a Key Unix idea: All input and output is handled in a consistent and uniform way.consistent and uniform way.

Basic Unix I/O operations (system calls): 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()

Page 5: 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”

– 5 – CS 105

Opening FilesOpening Files

Opening a file informs the kernel that you are getting ready to access Opening a file informs the kernel that you are getting ready to access that file.that file.

Returns a small identifying integer Returns a small identifying integer file descriptorfile 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 Each process created by a Unix shell begins life with three open files associated with a terminal: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);}

Page 6: 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”

– 6 – CS 105

Closing FilesClosing Files

Closing a file informs the kernel that you are finished accessing Closing a file informs the kernel that you are finished accessing that file.that file.

Closing an already closed file is a recipe for disaster in threaded Closing an already closed file is a recipe for disaster in threaded programs (more on this later)programs (more on this later)

Moral: Always check return codes, even for seemingly benign Moral: Always check return codes, even for seemingly benign functions such as functions such as close()close()

perror is simplified strerror/fprintf; perror is simplified strerror/fprintf;

int fd; /* file descriptor */int retval; /* return value */

if ((retval = close(fd)) < 0) { perror(“close”); exit(1);}

Page 7: 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”

– 7 – CS 105

Reading FilesReading Files

Reading a file copies bytes from the current file position to Reading a file copies bytes from the current file position to memory, and then updates file position.memory, and then updates file position.

Returns number of bytes read from file Returns number of bytes read from file fdfd into into bufbuf 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);}

Page 8: 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”

– 8 – CS 105

Writing FilesWriting Files

Writing a file copies bytes from memory to the current file position, and Writing a file copies bytes from memory to the current file position, and then updates current file position.then updates current file position.

Returns number of bytes written from Returns number of bytes written from bufbuf to file to file fd.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 Transfers up to 512 bytes from address bufbuf to file to file fdfd

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

Page 9: 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”

– 9 – CS 105

Simple ExampleSimple Example

Copying standard input to standard output one byte Copying standard input to standard output one byte at a time.at a time.

Note the use of error handling wrappers for read Note the use of error handling wrappers for read and write (text has details).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);}

Page 10: 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”

– 10 – CS 105

Dealing with Short CountsDealing with Short Counts

Short counts can occur in these situations: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: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?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

Page 11: 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”

– 11 – CS 105

“Foolproof” I/O“Foolproof” I/O

Low-level I/O is difficult because of short counts and Low-level I/O is difficult because of short counts and other possible errorsother possible errors

The text provides the RIO package, a good example The text provides the RIO package, a good example of how to encapsulate low-level I/Oof how to encapsulate low-level I/O

RIO is a set of wrappers that provide efficient and RIO is a set of wrappers that provide efficient and robust I/O in applications such as network robust I/O in applications such as network programs that are subject to short counts.programs that are subject to short counts.

Download from Download from csapp.cs.cmu.edu/public/ics/code/src/csapp.c csapp.cs.cmu.edu/public/ics/code/src/csapp.c csapp.cs.cmu.edu/public/ics/code/include/csapp.hcsapp.cs.cmu.edu/public/ics/code/include/csapp.h

Page 12: 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”

– 12 – CS 105

Unbuffered I/OUnbuffered I/O

RIO provides buffered and unbuffered routinesRIO provides buffered and unbuffered routines

Unbuffered: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.

Page 13: 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”

– 13 – CS 105

Implementation of rio_readnImplementation 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 */}

Page 14: 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”

– 14 – CS 105

Buffered InputBuffered Input

Buffered: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

Page 15: 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”

– 15 – CS 105

Buffered ExampleBuffered Example

Copying the lines of a text file from standard input Copying the lines of a text file from standard input to standard output.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);}

Page 16: 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”

– 16 – CS 105

I/O ChoicesI/O Choices

Unix I/OUnix 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/OStandard 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)

Page 17: 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”

– 17 – CS 105

I/O Choices, continuedI/O Choices, continuedRIORIO

• Buffered and unbuffered• Nicely packaged• Author’s choice for sockets and pipes• Non-standard, but built on Stevens’s work

C++ streamsC++ streams• Standard (sort of)• Very complex

Roll your ownRoll your own• Time consuming• Error-prone

Unix Bible: W. Richard Stevens, Unix Bible: W. Richard Stevens, Advanced Programming in the Advanced Programming in the Unix Environment,Unix Environment, Addison Addison Wesley, 1993.Wesley, 1993.

Page 18: 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”

– 18 – CS 105

How the Unix Kernel Represents Open FilesHow the Unix Kernel Represents Open FilesTwo descriptors referencing two distinct open disk files.Two descriptors referencing two distinct open disk files.

Descriptor 1 (stdout) points to terminal, and descriptor 4 points to Descriptor 1 (stdout) points to terminal, and descriptor 4 points to open disk file.open disk file.

fd 0fd 1fd 2fd 3fd 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

...

stderrstdoutstdin File access

...

File size

File type

File access

...

File size

File type

File A (terminal)

File B (disk)

Info in stat struct

Page 19: 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”

– 19 – CS 105

File SharingFile SharingTwo distinct descriptors sharing the same disk file through Two distinct descriptors sharing the same disk file through

two distinct open file table entriestwo distinct open file table entries E.g., Calling open twice with the same filename argument

fd 0fd 1fd 2fd 3fd 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

Page 20: 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”

– 20 – CS 105

How Processes Share FilesHow Processes Share Files

A child process inherits its parentA child process inherits its parent’’s open files. Here is the s open files. Here is the situation immediately after a situation immediately after a forkfork

fd 0fd 1fd 2fd 3fd 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 0fd 1fd 2fd 3fd 4

Child's table

File access

...

File size

File type

File access

...

File size

File type

File A

File B

Page 21: 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”

– 21 – CS 105

I/O RedirectionI/O Redirection

Question: How does a shell implement I/O redirection?Question: How does a shell implement I/O redirection?

unix> ls > foo.txt

Answer: By calling the Answer: By calling the dup2(oldfd, newfd)dup2(oldfd, newfd) function function Copies (per-process) descriptor table entry oldfd to entry newfd

a

b

fd 0

fd 1

fd 2

fd 3

fd 4

Descriptor tablebefore dup2(4,1)

b

b

fd 0

fd 1

fd 2

fd 3

fd 4

Descriptor tableafter dup2(4,1)

Page 22: 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”

– 22 – CS 105

File MetadataFile MetadataMetadataMetadata is data about data, in this case file data. is data about data, in this case file data.

Maintained by kernel, accessed by users with the Maintained by kernel, accessed by users with the stat stat and and fstatfstat functions. 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 */};

Page 23: 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”

– 23 – CS 105

Summary: Goals of Unix I/O Summary: Goals of Unix I/O

Uniform viewUniform view

user does not see actual devicesuser does not see actual devices

Devices and files look alike (almost)Devices and files look alike (almost)

Uniform drivers across devicesUniform drivers across devices

ATA disk looks same as IDE, SCSI…ATA disk looks same as IDE, SCSI…

Tape looks almost like diskTape looks almost like disk

Support for many kinds of I/O ObjectsSupport for many kinds of I/O Objects

Regular filesRegular files

DirectoriesDirectories

Pipes and socketsPipes and sockets

DevicesDevices

Even processes and kernel dataEven processes and kernel data

Page 24: 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”

– 24 – CS 105

Layers of I/O System –Lay OutLayers of I/O System –Lay Out1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-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.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• controller, map I/O ops to device ops, e.g., print,• Interrupts


Recommended