Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

File I/O

CS 3113 Fall 2018

CG and AHF: Introduction to Operating Systems 1


Be sure to install all code prerequisites

2
CG and AHF: Introduction to Operating Systems
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.
stdin is mapped to stderr is mapped to
| IDEs may map output to stderr to a input.txt error.txt
red color
| POSIX names are available in ./myprog <input.txt >output.txt 2>error.txt
<unistd.h>

New process/program stdout is mapped to


to be run output.txt

CG and AHF: Introduction to Operating Systems 5


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 fd
and its associated kernel resources.
CG and AHF: Introduction to Operating Systems 6
Example:
fileio/copy.c

CG and AHF: Introduction to Operating Systems 7


same four system calls—open(), read(),
write(), and close()—are used to perform
I/O on all types of files.
Universality of
I/O

CG and AHF: Introduction to Operating Systems 8


Open
opens 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
Read
reads 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
Write
writes 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
Close
is called after all I/O has been completed, in order to release the file descriptor fd
and its associated kernel resources.

CG and AHF: Introduction to Operating Systems 15


Always check for errors.

CG and AHF: Introduction to Operating Systems 16


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

du tfile # The number of blocks used

./seek_io tfile s10000 wefg # write efg starting at byte point 10000

du tfile # The number of blocks used

CG and AHF: Introduction to Operating Systems 21

You might also like