CPS 346 & 444/544 Lecture notes: Low-level I/O
Coverage: [UPE] §2.7 (pp. 65-70) and
§§7.1-7.2 (pp. 201-214), and [USP] §2.4
(pp. 26-29) and
§§4.1-4.6
(pp. 91-128)
Overview
- file descriptors
- 0 (STDIN_FILENO): stdin
- 1 (STDOUT_FILENO): stdout
- 2 (STDERR_FILENO): stderr
- file descriptor table
- resides in user program area
- entry removed per close
- system file table
- resides in kernel area
- shared by all processes
- one entry per active open; fork?
- each entry contains an offset for the open file
- entry deleted when pointer count becomes 0
- in-memory inode table
- resides in kernel area
- entry deleted when pointer count becomes 0
flushed (i.e., write called) when full
(regenerated from [USP] Fig. 4.2, p. 120)
Context switching (library vs. system calls)
- develop iterative versions of UNIX cat
- (verion 1: catv1.c) using only standard I/O and library functions
getchar/putchar
- (verion 2: catv2.c) using only standard I/O and library
functions fread/fwrite with
a buffer of size 1
- (verion 3: catv3.c)
using the system calls read/write with
a buffer of size 1
- compile (to cat1, cat2, and
cat3) and time these programs (using /usr/bin/time) on
a large input stream redirecting the output to the system's trashcan
(/dev/null); what do you notice?
- experimentation
- modify versions 2 (catv2.c) and 3 (catv3.c)
so that they take the buffer size
as a command-line argument and call these version 4 (catv4.c)
and version 5 (catv5.c), respectively
- increase the buffer size
- what do you notice?
- at what buffer size does the performance begin to remain constant?
- now time the UNIX cat command. what can you infer?
Self-exercise: code the UNIX cp command
- here's a solution
(courtesy William Kimball, CPS 445,
Winter 2006), which, by default (i.e., without
requiring the -p option), only
preserves the owner's permissions on the source in the target;
how to copy while preserving the permissions of group and other?
- here's another solution
(courtesy Matt Mize, CPS 445, Winter 2006), which does not preserve permissions (i.e.,
it behaves like cp without any options)
Restart library
- [USP] example 2.8 on p. 29: close
- [USP] example 2.9 on p. 29: r_close
Character and block special files in UNIX
- files which represent hardware devices are located in /dev
- character special file
$ ls -l /devices/pci@1e,600000/ide@d/dad@0,0:a,raw
crw-r----- 1 root sys 136, 8 Feb 19 17:47 /devices/pci@1e,600000/ide@d/dad@0,0:a,raw
- block special file
$ ls -l /devices/pci@1e,600000/ide@d/dad@0,0:a
brw-r----- 1 root sys 136, 8 Feb 19 02:33 /devices/pci@1e,600000/ide@d/dad@0,0:a
- read and write devices just as we do files
- for more information, see Chapter 7: `Device and Network Interfaces'
of the UNIX manual ($ man -s 7 intro)
I/O recap
- what is the difference between the library function and a system call?
UNIX as opposed to C
- what is the difference between a FILE* and a file descriptor?
- UNIX as opposed to C
- both referred to as a handle
- former involves another level of indirection (handle to handle)
- read, write, open, close
- hint on opening files:
open(path, O_WRONLY | O_CREAT | O_TRUNC, mode) =
creat(const char *path, mode_t mode) because former
is so common
select and poll
- handling I/O from multiple sources
- non-blocking I/O
- select: organizes parameters by type of condition (e.g., read ready or writeready)
- poll:
organizes parameters by file descriptor
References
| [UPE] |
B.W. Kernighan and R. Pike. The UNIX Programming Environment.
Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
|
| [USP] |
K.A. Robbins and S. Robbins.
UNIX Systems Programming: Concurrency, Communication, and Threads.
Prentice Hall, Upper Saddle River, NJ, Second edition, 2003
|
|