ui3
ui3
md 2025-04-07
Here are the answers to your questions based on the "Design of the UNIX Operating System" (Maurice J.
Bach):
The open system call establishes a connection between a process and a file.
Steps:
1. The kernel parses the pathname using namei to find the file's inode.
2. Checks permissions (read/write access based on flags like O_RDONLY, O_WRONLY, O_RDWR).
3. Allocates a file table entry (kernel-wide) and a user file descriptor (process-specific).
4. Initializes the file table entry with:
File offset = 0 (unless O_APPEND is set).
Access mode (read/write).
Pointer to the inode.
5. Returns a file descriptor (integer) to the process for future operations (e.g., read, write).
Flags:
1. Inode Table:
Contains in-core copies of disk inodes (file metadata: size, permissions, disk blocks).
Shared across processes opening the same file.
2. File Table:
Global kernel structure with entries for each open instance of a file.
Stores:
File offset (current read/write position).
Access mode (read/write).
Pointer to the inode.
Reference count (number of file descriptors pointing to it).
1/4
ui3.md 2025-04-07
Process A:
Process B:
Summary:
a.txt inode: refcount=3 (shared by 3 file table entries across both processes).
Each open creates a new file table entry, even for the same file.
mount:
Attaches a new file system (e.g., a disk partition) to the directory tree.
Kernel actions:
1. Checks if the mount point is a directory.
2. Verifies the device is a valid file system.
3. Updates the mount table with device and inode of mount point.
4. Sets flag in mount point inode to indicate it’s a mount point.
unmount:
2/4
ui3.md 2025-04-07
Steps:
1. Uses fd to find the file table entry (contains offset and inode pointer).
2. Checks if the file is opened for reading.
3. Uses bmap to convert logical offset (124 in your example) to disk block number.
4. Reads data from disk into a buffer cache block.
5. Copies data from buffer to user space (buf).
6. Updates the file offset in the file table entry.
Steps:
Given:
Calculation:
1. First block:
Bytes remaining in block 0: 1024 - 124 = 900.
Read 900 bytes (covers offset 124–1023).
2. Second block:
Remaining bytes: 2000 - 900 = 1100.
Read next full block (1024 bytes, covers 1024–2047).
3. Third block:
3/4
ui3.md 2025-04-07
Total blocks read: 3 (partial first block + full second block + partial third block).
Bytes per block:
4/4