- Authors

- Name
- Youngju Kim
- @fjvbn20031
File-System Interface
The file system is one of the most visible parts of the operating system. It provides the logical interface through which users store and manage data. This article examines file concepts, access methods, directory structures, and file protection mechanisms.
1. File Concepts
A file is a named collection of related information recorded on secondary storage.
File Attributes
| Attribute | Description |
|---|---|
| Name | Human-readable identifier |
| Identifier | Unique number within the file system (inode, etc.) |
| Type | Identified by extension or magic number |
| Location | Pointer to physical location on device |
| Size | Current file size (bytes, blocks) |
| Protection | Read, write, execute permissions |
| Timestamps | Creation, modification, access times |
| Owner | User who owns the file |
File Operations
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
// 1. 파일 생성 및 열기
int fd = open("example.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) {
perror("open failed");
return 1;
}
// 2. 파일 쓰기
const char *data = "Hello, File System!";
write(fd, data, 19);
// 3. 파일 닫기
close(fd);
// 4. 읽기 모드로 다시 열기
fd = open("example.txt", O_RDONLY);
// 5. 파일 읽기
char buffer[256];
ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
buffer[bytes_read] = '\0';
printf("읽은 내용: %s\n", buffer);
// 6. 파일 위치 이동 (seek)
lseek(fd, 0, SEEK_SET); // 파일 시작으로 이동
// 7. 파일 닫기
close(fd);
// 8. 파일 삭제
unlink("example.txt");
return 0;
}
Open File Table
The operating system maintains tables to manage open files.
Process A Process B
┌──────────────┐ ┌──────────────┐
│ fd table │ │ fd table │
│ 0: stdin │ │ 0: stdin │
│ 1: stdout │ │ 1: stdout │
│ 2: stderr │ │ 2: stderr │
│ 3: ─────────┐│ │ 3: ─────────┐│
└─────────────┘│ └─────────────┘│
↓ ↓
┌──────────────────────────────────────┐
│ System-wide Open File Table │
│ ┌─────────────────────────────────┐ │
│ │ File A: offset=100, refCount=1 │ │
│ │ File B: offset=0, refCount=2 │ ← shared
│ └─────────────────────────────────┘ │
└──────────────────────────────────────┘
│
↓
┌──────────────────┐
│ inode table │
│ (disk location) │
└──────────────────┘
2. Access Methods
Sequential Access
Accesses the file from beginning to end in order. Based on the tape model.
┌───┬───┬───┬───┬───┬───┬───┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ ← Records
└───┴───┴───┴───┴───┴───┴───┘
↑
Current position pointer
(moves forward or rewinds)
// 순차 접근 예시
FILE *fp = fopen("log.txt", "r");
char line[1024];
// 처음부터 끝까지 순서대로 읽기
while (fgets(line, sizeof(line), fp) != NULL) {
printf("%s", line);
}
// 되감기 (처음으로)
rewind(fp);
fclose(fp);
Direct Access (Random Access)
Allows direct access to any arbitrary position. Based on the disk model.
┌───┬───┬───┬───┬───┬───┬───┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ ← Block numbers
└───┴───┴───┴───┴───┴───┴───┘
↑ ↑ ↑
Direct access possible (any block)
// 직접 접근 예시 - 데이터베이스의 레코드 접근
#include <stdio.h>
typedef struct {
int id;
char name[50];
double balance;
} Account;
// 특정 레코드 번호로 직접 접근
Account read_account(FILE *fp, int record_num) {
Account acc;
// 레코드 번호로 오프셋 계산하여 직접 이동
fseek(fp, record_num * sizeof(Account), SEEK_SET);
fread(&acc, sizeof(Account), 1, fp);
return acc;
}
void write_account(FILE *fp, int record_num, Account *acc) {
fseek(fp, record_num * sizeof(Account), SEEK_SET);
fwrite(acc, sizeof(Account), 1, fp);
}
Indexed Access
Finds records in the data file through an index file.
Index File: Data File:
┌──────┬─────────┐ ┌─────────────────┐
│ Key │ Pointer │ │ Record 0 │
├──────┼─────────┤ ┌───→│ Smith, 1000 │
│ Kim │ ──────────────┐ │ ├─────────────────┤
│ Lee │ ────────────┐ │ │ │ Record 1 │
│ Park │ ──────┐ │ │ │ │ Lee, 2000 │
│ Smith│ ──────┼─────┼─┼──┘ ├─────────────────┤
└──────┴───────┘ │ │ │ Record 2 │
│ └──────→│ Kim, 3000 │
│ ├─────────────────┤
└────────→│ Record 3 │
│ Park, 4000 │
└─────────────────┘
3. Directory Structure
A directory is a table that maps file names to their metadata.
Single-Level Directory
┌──────────────────────────────────────┐
│ Root Directory │
│ │
│ cat bo a test data mail hex │
│ │ │ │ │ │ │ │ │
│ ○ ○ ○ ○ ○ ○ ○ │
└──────────────────────────────────────┘
All files in the same directory
→ Name collision issues, no grouping possible
Two-Level Directory
┌────────────────────────────────────┐
│ Master File Directory │
│ │
│ user1 user2 user3 │
│ │ │ │ │
│ ┌──┴──┐ ┌──┴──┐ ┌──┴──┐ │
│ │ UFD │ │ UFD │ │ UFD │ │
│ │cat a│ │cat b│ │ a │ │
│ └─────┘ └─────┘ └─────┘ │
└────────────────────────────────────┘
Separate directory per user → resolves name collisions
Tree-Structured Directory
The basic structure of modern file systems.
/ (root)
╱ │ ╲
home etc var
╱ ╲ │ │
user1 user2 passwd log
│ │ │
docs code syslog
│ │ │
a.txt b.txt main.c
# 트리 구조에서의 경로
# 절대 경로: 루트부터 시작
/home/user1/docs/a.txt
# 상대 경로: 현재 디렉토리 기준
# 현재 디렉토리가 /home/user1 일 때
docs/a.txt
Acyclic-Graph Directory
Files or directories can be shared from multiple locations. (Hard links, symbolic links)
/
│
home
╱ ╲
user1 user2
│ │
shared ──┘ (symbolic or hard link)
│
project
// 하드 링크 생성 (같은 inode를 가리킴)
#include <unistd.h>
int main() {
// 하드 링크: 동일한 inode에 대한 새 이름
link("/home/user1/shared/project",
"/home/user2/project");
// 심볼릭(소프트) 링크: 경로명을 가리키는 특수 파일
symlink("/home/user1/shared/project",
"/home/user2/project_link");
return 0;
}
Hard Link vs Symbolic Link
| Property | Hard Link | Symbolic Link |
|---|---|---|
| inode | Same as original | Separate |
| Cross FS | Not possible | Possible |
| Directory links | Not allowed (loop) | Allowed |
| Original deleted | Data preserved | Broken link |
| Size | Directory entry | Path name |
4. File Sharing
File Sharing in Multi-User Environments
User A (Owner)
│
├── File X (read/write/execute)
│ │
│ ├── User B (group) → read/execute
│ │
│ └── Others → read only
│
└── Group: developers
File Locking
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("shared_data.txt", O_RDWR);
struct flock lock;
lock.l_type = F_WRLCK; // 쓰기 잠금
lock.l_whence = SEEK_SET;
lock.l_start = 0; // 시작 오프셋
lock.l_len = 0; // 전체 파일
// 잠금 획득 (다른 프로세스는 대기)
fcntl(fd, F_SETLKW, &lock);
// 파일 작업 수행
write(fd, "critical data", 13);
// 잠금 해제
lock.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &lock);
close(fd);
return 0;
}
Lock Types
| Type | Description | Compatibility |
|---|---|---|
| Shared Lock (Read Lock) | Multiple processes can read concurrently | Compatible with shared |
| Exclusive Lock (Write) | Only one process can access | Incompatible with all |
| Mandatory Lock | Enforced by the kernel | System-dependent |
| Advisory Lock | Processes comply voluntarily | POSIX default |
5. Protection
Access Control Lists (ACL)
File: /project/secret.txt
ACL (Access Control List):
┌──────────┬─────────────────┐
│ User │ Permissions │
├──────────┼─────────────────┤
│ admin │ read, write, exec│
│ user1 │ read, write │
│ user2 │ read │
│ group:dev│ read, exec │
│ others │ (none) │
└──────────┴─────────────────┘
UNIX Permission Model
UNIX combines three categories -- owner, group, and others -- with three permissions -- read (r), write (w), and execute (x).
$ ls -l example.txt
-rwxr-x--- 1 user1 developers 1024 Mar 19 10:00 example.txt
│││││││││
│├┤├┤├┤
│ │ │ └── others: --- (no permissions)
│ │ └──── group: r-x (read, execute)
│ └────── owner: rwx (read, write, execute)
└──────── file type: - (regular file)
#include <sys/stat.h>
int main() {
// 8진수로 권한 설정
// 7=rwx, 5=r-x, 0=---
chmod("example.txt", 0750);
// 소유자 변경
chown("example.txt", 1000, 100); // uid=1000, gid=100
// 새 파일 생성 시 기본 권한 마스크
umask(0022); // 기본 권한에서 group/others의 쓰기 제거
return 0;
}
Special Permission Bits
| Bit | Symbol | Effect |
|---|---|---|
| setuid | s (owner exec) | Runs with file owner's permissions |
| setgid | s (group exec) | Runs with file group's permissions |
| sticky bit | t (others exec) | Only owner can delete files in the directory |
# setuid 설정 예시 (passwd 명령어)
# 일반 사용자도 root 소유 파일을 수정 가능
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 ... /usr/bin/passwd
^
setuid 비트 설정됨
# sticky bit 설정 예시 (/tmp 디렉토리)
$ ls -ld /tmp
drwxrwxrwt 15 root root 4096 ... /tmp
^
sticky bit 설정됨
6. Summary
- File Concepts: A named collection of information with defined attributes and operations
- Access Methods: Sequential (tape model), direct (disk model), and indexed access
- Directory Structure: Evolved from single-level to acyclic graphs. Modern systems use trees with links
- File Sharing: Concurrent access managed through locking mechanisms (shared/exclusive, mandatory/advisory)
- Protection: Access control via UNIX permission model (rwx, owner/group/others) and ACLs
Quiz: File-System Interface
Q1. Explain the differences between hard links and symbolic links.
A1. Hard links share the same inode as the original file, so data remains accessible even if the original is deleted. Symbolic links have a separate inode and store a path name, so they become broken links if the original is deleted. Hard links cannot cross file system boundaries or be applied to directories, but symbolic links can do both.
Q2. What does the UNIX file permission 0754 mean?
A2. Owner (7=rwx): read, write, and execute. Group (5=r-x): read and execute, no write. Others (4=r--): read only.
Q3. What is the purpose of the sticky bit?
A3. When the sticky bit is set on a directory, files within that directory can only be deleted by the file's owner, the directory's owner, or root. It is set on the /tmp directory so all users can create files but cannot delete other users' files.