16 Io
16 Io
System-Level I/O
15-213: Introduction to Computer Systems
16th Lecture, Oct. 22, 2015
Instructors:
Randal E. Bryant and David R. O’Hallaron
Today
Unix I/O
RIO (robust I/O) package
Metadata, sharing, and redirection
Standard I/O
Closing remarks
File Types
Each file has a type indicating its role in the system
Regular file: Contains arbitrary data
Directory: Index for a related group of files
Socket: For communicating with a process on another machine
Regular Files
A regular file contains arbitrary data
Applications often distinguish between text files and binary
files
Text files are regular files with only ASCII or Unicode characters
Binary files are everything else
e.g., object files, JPEG images
Kernel doesn’t know the difference!
Text file is sequence of text lines
Text line is sequence of chars terminated by newline char (‘\n’)
Newline is 0xa, same as ASCII line feed character (LF)
End of line (EOL) indicators in other systems
Linux and Mac OS: ‘\n’ (0xa)
line feed (LF)
Windows and Internet protocols: ‘\r\n’ (0xd 0xa)
Carriage return (CR) followed by line feed (LF)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Carnegie Mellon
Directories
Directory consists of an array of links
Each link maps a filename to a file
Each directory contains at least two entries
. (dot) is a link to itself
.. (dot dot) is a link to the parent directory in the directory
hierarchy (next slide)
Commands for manipulating directories
mkdir: create empty directory
ls: view directory contents
rmdir: delete empty directory
Directory Hierarchy
All files are organized as a hierarchy anchored by root directory
named / (slash)
/
unistd.h
Kernel maintains current working directory (cwd) for each process
Modified using the cd command
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8
Carnegie Mellon
Pathnames
Locations of files in the hierarchy denoted by pathnames
Absolute pathname starts with ‘/’ and denotes path from root
/home/droh/hello.c
Relative pathname denotes path from current working directory
../home/droh/hello.c
/ cwd: /home/bryant
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition unistd.h 9
Carnegie Mellon
Opening Files
Opening a file informs the kernel that you are getting ready to
access that file
int fd; /* file descriptor */
Closing Files
Closing a file informs the kernel that you are finished
accessing that file
int fd; /* file descriptor */
int retval; /* return value */
Reading Files
Reading a file copies bytes from the current file position to
memory, and then updates file position
char buf[512];
int fd; /* file descriptor */
int nbytes; /* number of bytes read */
Writing Files
Writing a file copies bytes from memory to the current file
position, and then updates current file position
char buf[512];
int fd; /* file descriptor */
int nbytes; /* number of bytes read */
#include "csapp.h"
int main(void)
{
char c;
while(Read(STDIN_FILENO, &c, 1) != 0)
Write(STDOUT_FILENO, &c, 1);
exit(0);
}
On 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
Today
Unix I/O
RIO (robust I/O) package
Metadata, sharing, and redirection
Standard I/O
Closing remarks
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;
rio_buf
rio_bufptr
Buffered Portion
rio_cnt
rio_buf
rio_bufptr
typedef struct {
int rio_fd; /* descriptor for this internal buf */
int rio_cnt; /* unread bytes in internal buf */
char *rio_bufptr; /* next unread byte in internal buf */
char rio_buf[RIO_BUFSIZE]; /* internal buffer */
} rio_t;
RIO Example
Copying the lines of a text file from standard input to
standard output
#include "csapp.h"
Rio_readinitb(&rio, STDIN_FILENO);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0)
Rio_writen(STDOUT_FILENO, buf, n);
exit(0);
} cpfile.c
Today
Unix I/O
RIO (robust I/O) package
Metadata, sharing, and redirection
Standard I/O
Closing remarks
File Metadata
Metadata is data about data, in this case file data
Per-file metadata maintained by kernel
accessed by users with the stat and fstat functions
...
...
File B (disk)
File access
File size
File pos
refcnt=1
File type
...
...
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
...
...
File B (disk)
File pos
refcnt=1
...
...
...
File B (disk)
File access
File size
File pos
refcnt=1
File type
...
...
...
...
...
...
fd 4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Carnegie Mellon
I/O Redirection
Question: How does a shell implement I/O redirection?
linux> ls > foo.txt
...
...
File B
File access
File size
File pos
refcnt=1
File type
...
...
...
...
File B
File access
File size
File pos
refcnt=2
File type
...
...
Today
Unix I/O
RIO (robust I/O) package
Metadata, sharing, and redirection
Standard I/O
Closing remarks
#include <stdio.h>
extern FILE *stdin; /* standard input (descriptor 0) */
extern FILE *stdout; /* standard output (descriptor 1) */
extern FILE *stderr; /* standard error (descriptor 2) */
int main() {
fprintf(stdout, "Hello, world\n");
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37
Carnegie Mellon
h e l l o \n . .
fflush(stdout);
Today
Unix I/O
RIO (robust I/O) package
Metadata, sharing, and redirection
Standard I/O
Closing remarks
fopen fdopen
fread fwrite
fscanf fprintf
sscanf C application program
sprintf fgets rio_readn
fputs fflush rio_writen
fseek Standard I/O RIO
rio_readinitb
fclose functions functions
rio_readlineb
open read rio_readnb
Unix I/O functions
write lseek
(accessed via system calls)
stat close
Cons
Dealing with short counts is tricky and error prone
Efficient reading of text lines requires some form of buffering, also tricky
and error prone
Both of these issues are addressed by the standard I/O and RIO packages
String functions
strlen, strcpy, strcat
Interprets byte value 0 (end of string) as special
Extra Slides
Accessing Directories
Only recommended operation on a directory: read its entries
dirent structure contains information about a directory entry
DIR structure contains information about directory while stepping
through its entries
#include <sys/types.h>
#include <dirent.h>
{
DIR *directory;
struct dirent *de;
...
if (!(directory = opendir(dir_name)))
error("Failed to open directory");
...
while (0 != (de = readdir(directory))) {
printf("Found file: %s\n", de->d_name);
}
...
closedir(directory);
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 52