File System Basics βββ
File System Basics βββ¶
Overview¶
File systems define how operating systems store and manage data on disks. Learn core concepts including file concepts, attributes, operations, directory structures, and access methods.
Table of Contents¶
- File Concept
- File Attributes
- File Operations and System Calls
- Directory Structures
- File Access Methods
- File System Mounting
- Practice Problems
1. File Concept¶
1.1 What is a File?¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File Definition β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β File = Named collection of related information β
β β
β Operating System Perspective: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β β
β β Disk (Physical Storage Device) β β
β β βββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ β β
β β βBlock0βBlock1βBlock2βBlock3βBlock4βBlock5βBlock6βBlock7β β β
β β βββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ β β
β β β β β β β β β β
β β ββββββ¬βββββ ββββββ¬βββββ ββββ¬βββ β β
β β β β β β β
β β File A File B File C β β
β β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β User Perspective: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β β
β β Logical Storage Units β β
β β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β β
β β β report.docx β β photo.jpg β β program.exe β β β
β β β (Document) β β (Image) β β (Executable) β β β
β β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β β
β β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β File System: Abstraction layer connecting these two views β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1.2 File Types¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File Types β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββ¬βββββββββββββββ¬ββββββββββββββββββββββββββββββββββ β
β β Type β Extension β Description β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Executable β .exe, .com β Machine code β β
β β β .bin, (none) β β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Object β .o, .obj β Compiled object code β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Source Code β .c, .py, .js β Programming language source β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Text β .txt, .md β Plain text β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Library β .a, .so β Static/dynamic library β β
β β β .lib, .dll β β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Image β .jpg, .png β Graphics data β β
β β β .gif, .bmp β β β
β βββββββββββββββββΌβββββββββββββββΌββββββββββββββββββββββββββββββββββ€ β
β β Archive β .zip, .tar β Compressed archive β β
β β β .gz, .7z β β β
β βββββββββββββββββ΄βββββββββββββββ΄ββββββββββββββββββββββββββββββββββ β
β β
β Unix doesn't require extensions (convention only) β
β Type detection by content: file command β
β β
β $ file /bin/ls β
β /bin/ls: ELF 64-bit LSB shared object, x86-64... β
β β
β $ file report.pdf β
β report.pdf: PDF document, version 1.4 β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2. File Attributes¶
2.1 Basic Attributes (Metadata)¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File Attributes β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Attribute β Description β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Name β Human-readable file name β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Identifier β Unique number in file system (inode number) β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Type β Regular file, directory, symlink, device, etc β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Location β Storage location on disk (block pointers) β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Size β File size in bytes β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Protection β Access permissions (rwxrwxrwx) β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Owner β File owner (UID) β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Group β File group (GID) β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Timestamps β Creation, modification, access time β β
β βββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2.2 Unix/Linux stat Structure¶
#include <sys/stat.h>
#include <time.h>
// stat structure (stores file attributes)
struct stat {
dev_t st_dev; // Device ID
ino_t st_ino; // inode number
mode_t st_mode; // File mode (type + permissions)
nlink_t st_nlink; // Number of hard links
uid_t st_uid; // Owner UID
gid_t st_gid; // Group GID
dev_t st_rdev; // Device ID (special files)
off_t st_size; // Total size in bytes
blksize_t st_blksize; // I/O block size
blkcnt_t st_blocks; // Number of 512B blocks allocated
// Timestamps
struct timespec st_atim; // Last access time
struct timespec st_mtim; // Last modification time
struct timespec st_ctim; // Last status change time
};
// File attribute query example
void print_file_info(const char* path) {
struct stat sb;
if (stat(path, &sb) == -1) {
perror("stat");
return;
}
printf("File: %s\n", path);
printf("inode: %lu\n", sb.st_ino);
printf("Size: %ld bytes\n", sb.st_size);
printf("Blocks: %ld\n", sb.st_blocks);
printf("Permissions: %o\n", sb.st_mode & 0777);
printf("Owner: %d\n", sb.st_uid);
printf("Links: %lu\n", sb.st_nlink);
// File type
if (S_ISREG(sb.st_mode)) printf("Type: Regular file\n");
if (S_ISDIR(sb.st_mode)) printf("Type: Directory\n");
if (S_ISLNK(sb.st_mode)) printf("Type: Symbolic link\n");
if (S_ISCHR(sb.st_mode)) printf("Type: Character device\n");
if (S_ISBLK(sb.st_mode)) printf("Type: Block device\n");
printf("Modified: %s", ctime(&sb.st_mtime));
}
2.3 Interpreting ls -l Output¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ls -l Output Analysis β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β $ ls -l β
β -rw-r--r-- 1 user group 4096 Jan 15 10:30 document.txt β
β βββββ€βββ€β β β β β β β βββ Filename β
β ββ β β β β β β β β βββ Time β
β ββ β β β β β β β βββ Date β
β ββ β β β β β β βββ Size (bytes) β
β ββ β β β β β βββ Group β
β ββ β β β β βββ Owner β
β ββ β β β βββ Hard link count β
β ββ β β βββ Others permissions β
β ββ β βββ Group permissions β
β ββ βββ Owner permissions β
β ββββ File type β
β βββ - regular, d directory, l link, c char device, b block device β
β β
β Permissions: β
β r (4) = read β
β w (2) = write β
β x (1) = execute (for directory: access) β
β β
β Example: rw-r--r-- = 644 = Owner: rw, Group: r, Others: r β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
3. File Operations and System Calls¶
3.1 Basic File Operations¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File Operations β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Operation β Description β β
β βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β Create β Create new file, add entry to directory β β
β β Open β Prepare file access, return file descriptor β β
β β Read β Read data from current position β β
β β Write β Write data at current position β β
β β Seek β Change current position in file β β
β β Close β End file access, release resources β β
β β Delete β Remove file, delete directory entry β β
β β Truncate β Set file size to 0 (delete content) β β
β βββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Additional operations: β
β - Append: Add data to end of file β
β - Rename: Change file name β
β - Get/Set Attributes: Query/modify attributes β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
3.2 System Call Examples¶
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
// Create and write file
void create_and_write(const char* path) {
// O_CREAT: create if doesn't exist
// O_WRONLY: write-only
// O_TRUNC: delete existing content
// 0644: permissions (rw-r--r--)
int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) {
perror("open");
exit(1);
}
const char* data = "Hello, File System!\n";
ssize_t written = write(fd, data, strlen(data));
if (written == -1) {
perror("write");
} else {
printf("%zd bytes written\n", written);
}
close(fd);
}
// Read file
void read_file(const char* path) {
int fd = open(path, O_RDONLY);
if (fd == -1) {
perror("open");
return;
}
char buffer[1024];
ssize_t bytes_read;
// read() returns bytes read, 0 is EOF
while ((bytes_read = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytes_read] = '\0';
printf("%s", buffer);
}
if (bytes_read == -1) {
perror("read");
}
close(fd);
}
// File position seek
void seek_example(const char* path) {
int fd = open(path, O_RDONLY);
if (fd == -1) return;
// Seek to end to check size
off_t size = lseek(fd, 0, SEEK_END);
printf("File size: %ld bytes\n", size);
// Return to beginning
lseek(fd, 0, SEEK_SET);
// Move forward 10 bytes
lseek(fd, 10, SEEK_CUR);
char buffer[100];
read(fd, buffer, 10);
buffer[10] = '\0';
printf("10 chars from offset 10: %s\n", buffer);
close(fd);
}
// File copy implementation
int copy_file(const char* src, const char* dst) {
int src_fd = open(src, O_RDONLY);
if (src_fd == -1) return -1;
int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dst_fd == -1) {
close(src_fd);
return -1;
}
char buffer[4096];
ssize_t bytes;
while ((bytes = read(src_fd, buffer, sizeof(buffer))) > 0) {
ssize_t written = write(dst_fd, buffer, bytes);
if (written != bytes) {
close(src_fd);
close(dst_fd);
return -1;
}
}
close(src_fd);
close(dst_fd);
return 0;
}
int main() {
create_and_write("test.txt");
read_file("test.txt");
seek_example("test.txt");
copy_file("test.txt", "test_copy.txt");
return 0;
}
3.3 Open File Table¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Open File Table Structure β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Process A Process B β
β ββββββββββββββββββββ ββββββββββββββββββββ β
β β fd table β β fd table β β
β ββββββββββββββββββββ€ ββββββββββββββββββββ€ β
β β 0: stdin β β 0: stdin β β
β β 1: stdout β β 1: stdout β β
β β 2: stderr β β 2: stderr β β
β β 3: βββββββββββββββ β 3: βββββββββββββββ β
β β 4: βββββββββββββββ βββββββββββββββββββΌβ β
β ββββββββββββββββββΌβΌβ β β
β ββ β β
β βΌβΌ β β
β System-wide Open File Table β β
β βββββββββββββββββββββββββββββββββββββββββββ β β
β β Entry 1: β β β
β β - File offset: 100 βββ β
β β - Access mode: O_RDONLY β β
β β - Reference count: 2 β β
β β - inode pointer: βββββββββββββββββββββββΌββββ β
β βββββββββββββββββββββββββββββββββββββββββββ€ β β
β β Entry 2: β β β
β β - File offset: 500 β β β
β β - Access mode: O_RDWR β β β
β β - Reference count: 1 β β β
β β - inode pointer: βββββββββββββββββββββββΌββββΌββββ β
β βββββββββββββββββββββββββββββββββββββββββββ β β β
β β β β
β In-Memory inode Table β β β
β βββββββββββββββββββββββββββββββββββββββββββ β β β
β β inode 1234: βββββ β β
β β - File size: 4096 β β β
β β - Disk block locations β β β
β β - Permissions, owner, etc. β β β
β βββββββββββββββββββββββββββββββββββββββββββ€ β β
β β inode 5678: βββββββββ β
β β - File size: 8192 β β
β β - ... β β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β
β After fork(): fd table copied, open file entry shared (offset shared!)β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4. Directory Structures¶
4.1 Single-Level Directory¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Single-Level Directory β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Root Directory β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β
β β cat β bo β a β test β data β mail β cont β hex β records β β β
β ββββ¬βββ΄βββ¬ββ΄βββ¬ββ΄ββββ¬βββ΄ββββ¬βββ΄ββββ¬βββ΄ββββ¬βββ΄βββ¬βββ΄βββββ¬βββββ β β
β β β β β β β β β β β β
β βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ β β
β file file file file file file file file file β β
β β
β Problems: β
β - Name collision: All users cannot use same names β
β - Management difficulty: Hard to find files as count grows β
β - No grouping: Cannot organize related files β
β β
β Used in early OS (CP/M) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4.2 Two-Level Directory¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Two-Level Directory β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Master File Directory (MFD) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β user1 user2 user3 β β
β βββββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β User File Directory (UFD) β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β cat test a β β cat data b β β hex mail β β
β ββββ¬βββββββ¬βββββ¬βββ ββββ¬βββββββ¬βββββ¬βββ ββββ¬βββββββ¬βββββββ β
β β β β β β β β β β
β βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ β
β β
β Advantages: β
β - Independent namespace per user β
β - user1's cat β user2's cat β
β β
β Disadvantages: β
β - Difficult to share files between users β
β - No subdirectories β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4.3 Tree-Structured Directory¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Tree-Structured Directory β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β / β
β β β
β ββββββββββββββββββββΌβββββββββββββββββββ β
β β β β β
β bin home etc β
β β β β β
β βββββββ΄ββββββ βββββββββΌββββββββ ββββββ΄βββββ β
β ls cat β β β passwd hosts β
β user1 user2 user3 β
β β β β β
β ββββββ΄βββββ β documents β
β docs code data β β
β β β β ββββ΄βββ β
β βββββΌββββ main.c β report notes β
β a.txt b.txt file.txt β
β β
β Features: β
β - Arbitrary depth subdirectories allowed β
β - Absolute path: /home/user1/docs/a.txt β
β - Relative path: ./docs/a.txt (from current directory) β
β - Current directory (.), parent directory (..) β
β β
β Used by most modern operating systems β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
4.4 Acyclic Graph Directory¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Acyclic Graph Directory β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β / β
β β β
β ββββββββββββββββββββΌβββββββββββββββββββ β
β β β β β
β home shared β β
β β β β β
β βββββββ΄ββββββ project βββββββββββ β
β user1 user2 β β² β
β β β βββββββ΄ββββββ β
β myproj ββββββββββββββββ β β
β β shared file β
β ourproj βββββ β
β β
β File/directory sharing methods: β
β β
β 1. Hard Link: β
β - Different name pointing to same inode β
β - ln target link_name β
β - On delete, only link count decreases β
β - Cannot use on directories (prevents cycles) β
β β
β 2. Symbolic Link: β
β - Stores path to another file β
β - ln -s target link_name β
β - Broken link if original deleted (dangling link) β
β - Can use on directories β
β β
β $ ln /shared/project myproj # Hard link β
β $ ln -s /shared/project myproj # Symbolic link β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5. File Access Methods¶
5.1 Sequential Access¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Sequential Access β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β File β
β βββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ¬ββββ β
β β A β B β C β D β E β F β G β H β I β J β β
β βββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ΄ββββ β
β β β
β Current position β
β β
β Operations: β
β - read(): read from current position, advance β
β - write(): write at current position, advance β
β - reset(): return to beginning β
β β
β Features: β
β - Originated from tape-based systems β
β - Commonly used in editors, compilers β
β - Suitable for log file processing β
β β
β Example: Sequential read to end of file β
β while (read(fd, &buf, size) > 0) { β
β process(buf); β
β } β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5.2 Direct/Random Access¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Direct Access β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β File (logical blocks) β
β Block: 0 1 2 3 4 5 6 7 8 9 β
β βββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ¬ββββββ β
β β β β β β β β β β β β β
β βββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ΄ββββββ β
β β β β
β Block 1 Block 5 β
β read read β
β β
β Operations: β
β - read(n): read block n β
β - write(n): write block n β
β - seek(n): move to block n β
β β
β Features: β
β - Disk-based systems β
β - Suitable for databases β
β - Random position access β
β β
β Example: Direct record access β
β #define RECORD_SIZE 100 β
β lseek(fd, record_num * RECORD_SIZE, SEEK_SET); β
β read(fd, &record, RECORD_SIZE); β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5.3 Indexed Access¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Indexed Access β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Index File Data File β
β ββββββββββββββββββββββ βββββββββββββββββββββββββββ β
β β Key βBlock Ptr β β Block 0 β β
β βββββββββββΌβββββββββββ€ βββββββββββββββββββββββββββ€ β
β β "Apple" β 7 ββββββββββββββββββΆβ Block 7: Apple data β β
β β "Banana"β 3 ββββββββββββββ βββββββββββββββββββββββββββ€ β
β β "Cherry"β 12 ββββββββββββ β β Block 3: Banana data βββββ€
β β "Date" β 5 ββββββββββ β β βββββββββββββββββββββββββββ€ β
β β ... β ... β β β β β Block 12: Cherry data βββββ
β βββββββββββ΄βββββββββββ β β β βββββββββββββββββββββββββββ€
β β β β β Block 5: Date data βββββ
β β β ββββΆβ β β
β β β βββββββββββββββββββββββββββ β
β β β β
β βββΌβββββββββββββββββββββββββββββββββββββ
β β
β βββ (Find location from index, direct access)
β
β Features: β
β - Fast search in large files β
β - Multi-level index if index itself too large β
β - Database B+tree index β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
6. File System Mounting¶
6.1 Mount Concept¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β File System Mounting β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Before mount: β
β β
β Root file system (/) USB drive β
β / Independent tree β
β β±ββ² β β
β bin home etc β± β² β
β β photo doc β
β user1 β
β β β
β mnt (empty directory) β
β β
β After mount (mount /dev/sdb1 /home/user1/mnt): β
β β
β / β
β β±ββ² β
β bin home etc β
β β β
β user1 β
β β β
β mnt ββββββββββββ Mount point β
β β± β² β
β photo doc β USB drive contents visible here β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
6.2 Mount Commands¶
# Check mount status
$ mount
/dev/sda1 on / type ext4 (rw,relatime)
/dev/sdb1 on /mnt/usb type vfat (rw,user)
# Mount USB drive
$ sudo mount /dev/sdb1 /mnt/usb
# Mount with options
$ sudo mount -t ext4 -o ro,noexec /dev/sdc1 /mnt/backup
# Mount options:
# ro: read-only
# rw: read/write
# noexec: cannot execute programs
# nosuid: ignore setuid
# user: allow normal users to mount
# Unmount
$ sudo umount /mnt/usb
# /etc/fstab - automatic mount at boot
# Device Mount point Type Options Dump Pass
/dev/sda1 / ext4 defaults 1 1
/dev/sda2 /home ext4 defaults 1 2
/dev/sdb1 /mnt/data ext4 defaults,nofail 0 2
UUID=xxxx-xxxx /mnt/usb vfat user,noauto 0 0
Practice Problems¶
Problem 1: File Attribute Interpretation¶
Interpret each field from the following ls -l output:
-rwxr-x--- 2 alice developers 8192 Mar 15 14:30 script.sh
Show Answer
- : Regular file (d for directory, l for link)
rwx : Owner (alice) permissions - read/write/execute
r-x : Group (developers) permissions - read/execute
--- : Others permissions - none
2 : Hard link count (2 names for this file)
alice : Owner
developers : Owner group
8192 : File size (8KB)
Mar 15 14:30 : Last modification time
script.sh : File name
Numeric permissions: 750 (7=rwx, 5=r-x, 0=---)
Problem 2: System Call Sequence¶
Write the system call sequence to append "Hello World" to /tmp/log.txt.
Show Answer
int fd = open("/tmp/log.txt", O_WRONLY | O_APPEND | O_CREAT, 0644);
// O_APPEND: always write at end of file
// O_CREAT: create if doesn't exist
// 0644: permission setting
if (fd == -1) {
perror("open failed");
exit(1);
}
const char* msg = "Hello World\n";
ssize_t written = write(fd, msg, strlen(msg));
if (written == -1) {
perror("write failed");
}
close(fd);
Problem 3: Hard Link vs Symbolic Link¶
Explain the behavior of each link type in this scenario:
$ echo "original" > file.txt
$ ln file.txt hardlink.txt # Hard link
$ ln -s file.txt symlink.txt # Symbolic link
$ rm file.txt
$ cat hardlink.txt
$ cat symlink.txt
Show Answer
$ cat hardlink.txt
original
β Hard link points to same inode
β Data still exists even after file.txt deleted
β Link count just decreased by 1
$ cat symlink.txt
cat: symlink.txt: No such file or directory
β Symbolic link stores path "file.txt"
β Broken link (dangling link) after file.txt deleted
β Target doesn't exist, so error
Hard link characteristics:
- Shares same inode number
- Equal status with original
- Cannot use on directories
Symbolic link characteristics:
- Separate inode (stores path string)
- Dependent on original
- Can use on directories
- Can point to different file systems
Problem 4: Directory Navigation¶
Canonicalize the path /home/user/docs/../code/./main.c.
Show Answer
/home/user/docs/../code/./main.c
1. Navigate to /home/user/docs
2. .. goes up: /home/user
3. Navigate to code: /home/user/code
4. . is current directory (ignore): /home/user/code
5. main.c file: /home/user/code/main.c
Canonicalized path: /home/user/code/main.c
Verify in Linux:
$ realpath /home/user/docs/../code/./main.c
/home/user/code/main.c
Problem 5: Open File Table¶
Process A opens a file and creates process B via fork(). If both processes write 100 bytes each to the same file, what is the final file size?
Show Answer
After fork(), child copies parent's fd table.
Both processes' fds share the same open file entry.
Therefore they share the same offset!
Scenario:
1. Parent open(), offset = 0
2. fork(), child shares same open file entry
3. Parent write(100), offset = 100
4. Child write(100), offset = 200 (continues writing)
Final file size: 200 bytes
If child had separately open()ed:
- Separate open file entry created
- Separate offset
- Both starting from 0 would overwrite!
- Final size: 100 bytes
Note: In reality, results may vary based on write() atomicity
and buffering. O_APPEND usage recommended.
Next Steps¶
Continue to 17_File_System_Implementation.md to learn file system internals!
References¶
- Silberschatz, "Operating System Concepts" Chapter 13
- Linux man pages:
open(2),read(2),write(2),stat(2) - POSIX file system standards
- The Linux Programming Interface by Michael Kerrisk