Skip to content

file operations

πŸ”§ 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): cCopyEditint 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: cCopyEditFILE *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: cCopyEditchar 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: cCopyEditwrite(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: cCopyEditfclose(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: cCopyEditremove("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: cCopyEditlseek(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: cCopyEdittruncate("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: cCopyEditFILE *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: cCopyEditrename("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).