π§ 1. Create Operation
- Purpose: Allocate space for a new file and set up its metadata.
- System Call:
creat()
in Unix/Linux, CreateFile()
in Windows.
- Steps:
- The OS checks if a file with the same name exists.
- If not, it allocates an inode (in Unix-like systems) or an entry in the file allocation table.
- Sets default permissions and ownership.
- Example in C (Linux): cCopyEdit
int fd = creat("example.txt", 0644);
π 2. Open Operation
- Purpose: Prepare a file for reading, writing, or both.
- System Call:
open()
(Linux), fopen()
(C library), OpenFile()
(Windows).
- Steps:
- OS reads the fileβs metadata from disk.
- Creates a file descriptor (an integer handle).
- Places an entry in the system-wide Open File Table and links it to a per-process table.
- Modes:
"r"
(read), "w"
(write), "a"
(append), "r+"
(read/write).
- Example in C: cCopyEdit
FILE *fp = fopen("example.txt", "r");
π 3. Read Operation
- Purpose: Transfer data from the file to the process memory.
- System Call:
read()
in Unix, ReadFile()
in Windows.
- Steps:
- OS checks if the file is opened in read mode.
- The data is fetched into a kernel buffer (if not already in cache).
- Then, it is copied into the user-space buffer.
- Example in C: cCopyEdit
char buffer[100]; read(fd, buffer, 100);
βοΈ 4. Write Operation
- Purpose: Store data from memory into the file.
- System Call:
write()
in Unix, WriteFile()
in Windows.
- Steps:
- The data from user space is copied to a kernel buffer.
- Depending on buffering mode, it may be written immediately or later (lazy writing).
- Example: cCopyEdit
write(fd, "Hello, World!", 13);
β 5. Close Operation
- Purpose: Terminate the file session and release resources.
- System Call:
close()
in Unix, CloseHandle()
in Windows.
- Steps:
- Flushes any unwritten data from the buffer to the disk.
- Removes the entry from the Open File Table.
- Releases file descriptors.
- Example: cCopyEdit
fclose(fp); // Or close(fd);
π 6. Delete Operation
- Purpose: Remove a file from the file system.
- System Call:
unlink()
in Unix, DeleteFile()
in Windows.
- Steps:
- Removes directory entry.
- Frees inode and disk blocks if no process is using the file.
- If open by a process, it is marked for deletion but removed only after closing.
- Example: cCopyEdit
remove("example.txt");
π 7. Seek Operation
- Purpose: Move the file pointer to a specific location for random access.
- System Call:
lseek()
in Unix.
- Use Case: Skip headers, jump to middle of large files.
- Example: cCopyEdit
lseek(fd, 10, SEEK_SET); // Move pointer to byte 10
βοΈ 8. Truncate Operation
- Purpose: Shrink or extend a file.
- System Call:
truncate()
or ftruncate()
.
- Use Case: Limit log file size, remove extra data.
- Example: cCopyEdit
truncate("example.txt", 50); // File is now 50 bytes long
β 9. Append Operation
- Purpose: Add content to the end without overwriting.
- Behavior:
- File pointer is automatically moved to the end.
- Used in logging systems.
- Example: cCopyEdit
FILE *fp = fopen("log.txt", "a"); fprintf(fp, "New log entry\n");
π 10. Rename Operation
- Purpose: Change file name or path.
- System Call:
rename()
in Unix, MoveFile()
in Windows.
- Example: cCopyEdit
rename("oldname.txt", "newname.txt");
π§ Internal OS Handling
- File Descriptors: Integers representing an open file (e.g., 0 = stdin, 1 = stdout, 2 = stderr).
- Buffers: OS maintains internal buffers for performance (read-ahead, write-behind).
- File Tables:
- Global Open File Table: Tracks all open files.
- Per-Process File Table: Each process tracks its open files via file descriptors.
- Access Control: Checked via file permissions, user IDs, and ACLs (Access Control Lists).