chapter 39 - interlude: files and directories Flashcards
what is a file
a linear sequence of bytes identified by an inode number
What is a directory?
A special file mapping user-readable names to inode numbers
What is a directory tree?
A hierarchy starting from root /, containing nested directories and files
what does int fd = open(“foo”, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR); do
- O_CREAT → create if not exist
- O_WRONLY → open for writing
- O_TRUNC → empty the file if it exists
- S_IRUSR | S_IWUSR → user can read/write
- Returns a file descriptor, placed in the process’s
ofile[NOFILE]
array
What is a file descriptor?
A per-process integer used to access an open file
What does read(fd, buf, size) do?
Reads size bytes into buf from file fd, and updates the offset
What does write(fd, buf, size) do?
Writes size bytes from buf to file fd, also updating the offset
What does lseek(fd, offset, whence) do?
Moves the file offset (SEEK_SET, SEEK_CUR, or SEEK_END)
What is stored in the Open File Table?
Read/write flags, inode pointer, offset, and reference count
What happens after fork() to file descriptors?
Child gets copies of parent’s FDs, sharing the same offset and inode
What does dup(fd) do?
Duplicates fd, both share offset, inode, and permissions
What does fsync(fd) do?
Forces all dirty data for that file to disk immediately
What does rename(“old”, “new”) do?
Atomically renames a file; ensures no corruption even during crashes
What does stat() or fstat() return?
Metadata like inode, size, permissions, owner, and timestamps
What does unlink() do?
Removes a file; deletes it once all links and open FDs are gone