Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
File I/OCS 3113 Fall 2018
CG and AHF: Introduction to Operating Systems 1
Be sure to install all code prerequisites
CG and AHF: Introduction to Operating Systems2
Download TLPI book code
CG and AHF: Introduction to Operating Systems 3
File Descriptors
| A nonnegative integer that may refer to:regular files, pipes, FIFOs, sockets, terminals or devices.
| Each process has its own assigned set of file descriptors.
| Used by the system to refer to files (not filenames)
| When requested, the lowest-numbered unused file descriptor is assigned
CG and AHF: Introduction to Operating Systems 4
Standard File Descriptors
| When a shell program is run, these descriptors are copied from the terminal to the running program.
| I/O redirection may modify this assignment.
| IDEs may map output to stderr to a red color
| POSIX names are available in <unistd.h>
CG and AHF: Introduction to Operating Systems 5
./myprog <input.txt >output.txt 2>error.txt
New process/program to be run
stdin is mapped to input.txt
stdout is mapped to output.txt
stderr is mapped to error.txt
Key I/O System Calls
| opens the file identified by pathname, returning a file descriptor.
| reads at most count bytes from the open file referred to by fd and stores them in buffer.
| writes up to count bytes from buffer to the open file referred to by fd.
| is called after all I/O has been completed, in order to release the file descriptor fdand its associated kernel resources.
CG and AHF: Introduction to Operating Systems 6
Example: fileio/copy.c
CG and AHF: Introduction to Operating Systems 7
Universality of I/O
same four system calls—open(), read(), write(), and close()—are used to perform I/O on all types of files.
CG and AHF: Introduction to Operating Systems 8
Openopens the file identified by pathname, returning a file descriptor.
CG and AHF: Introduction to Operating Systems 9
CG and AHF: Introduction to Operating Systems 10
Readreads at most count bytes from the open file referred to by fd and stores them in buffer.
CG and AHF: Introduction to Operating Systems 11
CG and AHF: Introduction to Operating Systems 12
Writewrites up to count bytes from buffer to the open file referred to by fd.
CG and AHF: Introduction to Operating Systems 13
CG and AHF: Introduction to Operating Systems 14
Closeis called after all I/O has been completed, in order to release the file descriptor fdand its associated kernel resources.
CG and AHF: Introduction to Operating Systems 15
CG and AHF: Introduction to Operating Systems 16
Always check for errors.
Seeking
CG and AHF: Introduction to Operating Systems 17
File offset
| Also called read- write offset or pointer
| the kernel records a file offset for each open file.
| The first byte of the file is at offset 0.
| The file offset is set to point to the start of the file when the file is opened and is automatically adjusted by each subsequent call to read() or write()
CG and AHF: Introduction to Operating Systems 18
CG and AHF: Introduction to Operating Systems 19
Example: fileio/seek_io.c
20
lseek + read + write
CG and AHF: Introduction to Operating Systems 21
du tfile # The number of blocks used
du tfile # The number of blocks used
./seek_io tfile s10000 wefg # write efg starting at byte point 10000