Sub topic 11
Files: attributes and operations
This chapter assumes you know what a system call is, how kernel mode differs from user mode, and what a process is. It also assumes you can convert between KB, MB and GB using powers of two. Here you will learn what an operating system means by a "file", what information it stores about each file, and which operations it offers. Later chapters build on this when they cover access methods, directories, allocation and inodes.
What a file is
A file is a named collection of related information stored on secondary storage. To the user it is the smallest unit of logical storage: data cannot be written to disk unless it is inside a file. To the operating system it is an abstraction. The OS hides the physical details (sectors, cylinders, flash pages) and presents a sequence of bytes, or a sequence of records, under a name.
The OS does not interpret most file contents. A text file, a program, and an image are all bytes to the kernel. Interpretation belongs to applications, with a few exceptions such as executable files, which the kernel must understand to load a program.
File attributes
Attributes (also called metadata) describe a file without being part of its contents. Typical attributes are:
- Name: the human-readable identifier.
- Identifier: a unique number the file system uses internally, such as the inode number in Unix. Users normally do not see it.
- Type: needed by systems that support several kinds of files.
- Location: where the data lives on the device.
- Size: current size in bytes or blocks, and sometimes the maximum allowed size.
- Protection: owner, group and permission bits that control who may read, write or execute.
- Timestamps and user identification: creation, last modification and last access times, and the owner's ID. These support security and usage monitoring.
The attributes are stored in the directory structure on disk, or in a separate structure such as an inode that the directory entry points to. Because they are stored on disk, a directory entry can be much larger than the file name alone.
File types and extensions
A common way to record the type is a name extension, such as .c or .txt. The extension is a hint to the user and to applications. Windows uses it to choose a program to open the file. Unix treats it as a convention only. Some systems store a "magic number" at the start of the file instead. Executable formats such as ELF begin with a fixed signature, and the loader checks it before running the file.
File operations
The OS exposes a file through system calls. There are six basic operations.
- Create: find space for the file and add a directory entry. Two steps are needed: allocate space, then record the name and attributes.
- Write: given the file name or handle and the data, the system writes at the current write position. It then advances the position.
- Read: read data from the current read position. Most systems keep one per-process current-file-position pointer for both reading and writing.
- Reposition (seek): set the current-file-position pointer to a given value. No data moves, so it needs no device I/O in most designs. This "file seek" is unrelated to the disk-head seek you will meet in the disk scheduling chapters, although the names collide.
- Delete: find the directory entry, release the file's space, and remove the entry.
- Truncate: erase the contents but keep the attributes, except the length, which becomes zero. The space is released.
Other operations, such as append, rename and copy, are built from these six. Appending is a seek to the end followed by a write. A copy is a create followed by a loop of reads and writes.
Open and close
Almost every operation first searches the directory for the named file. Searching on every read or write would be slow. To avoid it, the OS requires a process to call open() first.
When open() runs, the kernel:
- Searches the directory for the file name.
- Copies the directory entry (attributes) into an in-memory table, the open-file table.
- Checks permissions against the requested mode (read, write or both).
- Returns a small integer, the file descriptor in Unix or a file handle in Windows.
Later calls use that descriptor instead of the name. When the process calls close(), the kernel removes its entry from the per-process table. If no other process has the file open, the system-wide entry is removed too, and any modified attributes are written back to the directory.
The next chapters cover the in-memory tables in detail. Two levels matter here:
- Per-process open-file table: one for each process. It holds the current file position and the access mode, and points into the system-wide table.
- System-wide open-file table: one for the whole system. It holds the copied attributes, the location of the data on disk, and an open count.
The open count records how many processes have the file open. The entry is deleted only when the count falls to zero.
Locking and sharing
When several processes open the same file, the OS may offer file locks. A shared lock works like a reader lock: several processes can hold it at once. An exclusive lock works like a writer lock: only one process can hold it. Locking can be mandatory, where the OS enforces it, or advisory, where it only works if every process checks the lock voluntarily. Unix commonly provides advisory locking. Windows can enforce it.
Worked example in Python
The Python calls below map directly onto the operations above.
import os
path = "demo.txt"
# create + write
with open(path, "w") as f:
f.write("hello file system\n")
# read, reposition (seek), read again
with open(path, "r") as f:
print(f.read(5)) # hello
f.seek(6) # reposition the file position pointer
print(f.read(4)) # file
# attributes (metadata) from the file system
info = os.stat(path)
print(info.st_size) # 18 bytes
print(oct(info.st_mode & 0o777)) # permission bits
print(info.st_ino > 0) # an inode number exists
# truncate: keep the file, drop contents
with open(path, "r+") as f:
f.truncate(0)
print(os.stat(path).st_size) # 0
# delete
os.remove(path)
print(os.path.exists(path)) # Falseos.stat returns the attributes. open performs the open step and returns a handle. Leaving the with block calls close.
Common errors and exam traps
- Confusing truncate with delete. Truncate keeps the directory entry and the attributes. Delete removes both.
- Treating the file-seek pointer as disk-head movement. It is only a number in the open-file table.
- Forgetting the open count. Closing a file in one process does not remove the system-wide entry while another process still has it open.
- Assuming the name is stored with the file's data or in the inode. In Unix it is in the directory entry.
- Mixing mandatory and advisory locks. Advisory locks protect nothing from a process that ignores them.
Summary table
| Item | Held where | Purpose |
|---|---|---|
| Attributes | Directory entry or inode on disk | Describe the file |
| System-wide open-file table | Memory | Shared copy of attributes, open count |
| Per-process open-file table | Memory | Current position, access mode |
| File descriptor | Process | Index into the per-process table |
Exercise
Process A and process B both open the same file. Process A calls close(). What happens to the system-wide open-file table entry, and why?
Show answer
The entry stays. The system-wide table keeps an open count, which was 2 and drops to 1 when A closes. The entry is removed only when the count reaches 0, after B also closes. A's per-process entry, including its file position, is removed at its own close.
Exercise
Name the basic file operations that a "copy file" command is built from, and say which one involves no device I/O in most designs.
Show answer
Copy uses create, then repeated read and write calls, then close. Reposition (seek) usually involves no device I/O because it only changes the current-file-position pointer in memory.
Exercise
In a Unix system, a file has two hard links with different names. Where are the two names stored, and where is the size stored?
Show answer
Each name is stored in its own directory entry, and both entries point to the same inode number. The size is an attribute stored once, in the inode.