February 1, 2006
Coverage:
[USP]
Chapter 4
Code Solution to Problem 10 of Exam 1 tested in Win32
- buffer overflow problem
- setting last element of buffer to null character ('\0')
is necessary, even in Win32
- code courtesy William Kimball
Importance of Using Appropriate Types
char c;
while ((c = getchar()) != EOF) {
...
}
I/O Recap
- read, write, open, close
- what is the difference between the library function and a system call?
- 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)
- self-exercise: code the UNIX cp command
- here's one solution,
courtesy William Kimball, 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, which does not preserve permissions (i.e.,
it behaves like cp without any options)
UNIX I/O Data Structures
- 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
- 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
Buffered Output
- flushed (i.e., write called) when full
- fflush and setvbuf
- disk i/o vs. terminal i/o
- full buffering vs. line buffering
- scanf flushes stdout buffer
- a return from main causes the buffers to be flushed
- printf followed by a fork
- with and without a newline
Redirection
- setup for free by the shell
- stdout to a file
- open disk file
- copy file descriptor for disk file
into entry for stdout
- use dup2
- involves an implicit close on the target of the copy
- close disk file using
its file descriptor
Examples
- open followed by fork
- fork followed by open
Other Things Briefly Mentioned
- handling i/o from multiple sources
- non-blocking i/o
- select
- organizes parameters by type of condition (e.g., read ready or write
ready)
- poll
- organizes parameters by file descriptor