File I/O Flashcards
6 basic I/O system calls
- open()
- write()
- read()
- close()
- lseek()
- fcntl()
Flags in open()
Specifies access and creation mode, i.e.
O_WRONLY, O_RDWR, O_CREAT, etc.
Mode in open()
Specifies file permission if file is created using O_CREAT or O_TMPFILE
File descriptor
A small, non-negative integer used as a handle for a particular file to read/write to
Can change each time the file is opened
File offset
The file offset is a pointer to a specific byte in a file we are reading/writing to
read() automatically increments the file offset, write() starts at the end and increments to end each time
lseek()
Used to manually adjust the file offset, 3 parameters: fd, offset, whence - adjusts file offset by offset bytes, specified by ‘whence’ which can be:
SEEK_SET - beginning of file
SEEK_CUR - current offset
SEEK_END - end of the file
Stream vs. File Descriptor
A stream is a “wrapper” around a file descriptor that also has a buffer attached to it
Buffered I/O vs. system calls
Buffered I/O writes to a buffer before making a system call because system calls are costly
fflush()
immediately sends buffered data to the kernel - setbuf(NULL) does the same thing
Blocking vs Nonblocking
Blocking call doesn’t return until the operation can be done, a nonblocking call will return anyway (usually w/ an error)
Everything is a File model
Everything is represented as a file including hardware like keyboard, mouse, and others like the terminal
Disk Partitions
A disk is divided into partitions - can be found at /proc/partitions - usually used as a file system, also used as swap space
File System
a system that manages files/directories, each system has a file tree that starts with root directory /
i-node
An i-node contains metadata for every file, i.e. type, permissions, owner, etc. - identified by a number
Hard link
Giving another name to an existing file - will have the same metadata and content
Hard links can’t be made on directories or outside the same file system