Chapter 4, File I/O
- This is at least part of chapter 4.
- Give it a read as we work our way through it.
- I/O in general is far more complex than you think.
- Many of the details of I/O are hidden in streams.
- I believe that you can do most of this with streams.
- But I don't know streams well enough to say for certain.
- Streams are much more complex than what we present.
- The title: The Universal I/O model
- This is good for more than just "regular" files.
- To accomplish this, the system has more depth than you might think.
- It is going to take several chapters to understand the I/O system.
- First off, there are a number of different types of files
- Regular files, this includes hard links.
- Symbolic links.
- Directories
- FIFOs
- Sockets
- (see server.c and client.c from the
man 7 unix
pages.
- (run the server, client 1 2 3 4, client down)
- Block Special Devices
- Character Special Devices
- When I ask you for the different file types on the midterm I want the list above
- They are not binary, text, ...
- These are all examples of regular files.
- Most IO is done through a basic set of commands
- This changes slightly for different file types
- But the basis is in chapter 4.
- The basic unit for a file is a file descriptor
- This is a small integer.
- Actually it is an index into a per process table.
-
- This is from chapter 5.
- But it is ok to look at now.
- The file descriptor maps to an open file in the kernel.
- Which maps to the appropriate place in the file system.
- A single process can have the same file open twice.
- An implication of this is that two processes can have the same file open
- By default three file descriptors are open for every process
- 0 is mapped to standard in.
- 1 is mapped to stdout
- 2 is mapped to std err
- The basic calls
-
open
- takes a file name,
- takes a set of arguments on how to open that file.
- If successful returns a non-negative file descriptor.
- On fail, returns a -1.
- Open is the most complex.
-
write
- Takes a fd
- And a buffer
- And a buffer size (in bytes)
- And attempts to write from the buffer to the fd for up to the size.
- It returns the number of bytes written.
-
read
- Takes a fd
- And a buffer
- And a size
- And attempts to read from that buffer for that size.
- It returns the number of bytes read.
-
close
- Takes a fd
- Attempts to close the file
- Returns a status.
- Note that write and read read and write raw bits.
- simple.cpp will demonstrate this.
- Take a look at book.cpp
- This is a modification of his code from the book.
- He suggests
- ./book ocap.txt new.txt
- ./book ocap.txt /dev/tty
- ./book ocap.txt /dev/pts/x - where x is another terminal.
- ./book /dev/tty capture
- Use a named pipe.
- This is what they mean by a universal i/o system.