File Operations - aryanjoshi0823/5143-Operating-System GitHub Wiki
File operations are essential functionalities provided by operating systems (OS) to enable users and applications to create, manipulate, and manage files on a storage device. These operations form the backbone of data interaction and are implemented to balance performance, reliability, and security.
File Access Lifecycle
-
Opening a File
- Files must be explicitly opened before access. This sets up the necessary context, such as associating a file descriptor with the file.
- In rare cases, some systems use automatic file handling, opening files at first access and managing closure automatically.
-
Closing a File
- Files must be explicitly closed to release system resources and ensure that buffered data is written to disk.
- Failure to close files can result in resource leaks and potential data corruption.
Core File Operations
-
Creating a File
- Allocates space on the storage device and creates an entry in the directory structure.
- Example:
open()
withO_CREAT
in Linux.
-
Opening a File
- Prepares the file for read, write, or append operations by associating a file descriptor or handle.
- Modes: Read-only, Write-only, Read/Write, Append.
-
Reading from a File
- Retrieves data from the file starting at the current file pointer position.
- Example:
read(fd, buffer, count)
readscount
bytes intobuffer
.
-
Writing to a File
- Stores data into the file at the current file pointer position.
- Example:
write(fd, buffer, count)
writescount
bytes frombuffer
.
-
Seeking within a File
- Moves the file pointer to a specific position for random access.
- Example:
lseek(fd, offset, whence)
adjusts the pointer based onwhence
(start, current, end).
-
Deleting a File
- Removes the file entry and marks the space as free.
- Example:
unlink(filename)
in Unix/Linux.
-
Renaming a File
- Changes the name or location of a file.
- Example:
rename(oldname, newname)
.
-
Closing a File
- Releases the file descriptor or handle.
- Example:
close(fd)
.
Open File Table
The OS maintains an open file table to manage all files currently in use. Key elements of the table include:
-
File Pointer
- Tracks the current position for the next read or write operation.
-
File-Open Count
- Indicates how many processes have the file open.
- When the count reaches zero, the file is removed from the table.
-
Disk Location
- Specifies the physical storage location of the file.
-
Access Rights
- Defines the permissions for interacting with the file.
File Locking
File locking ensures safe and coordinated access in multi-process environments. Types of locks:
-
Shared Lock
- Allows multiple processes to read a file simultaneously.
-
Exclusive Lock
- Restricts file access to a single process for both reading and writing.
-
Advisory Lock
- A non-enforced signal that other processes may choose to ignore (like a "Keep Out" sign).
-
Mandatory Lock
- Enforces strict access restrictions, acting like a locked door.
Buffering and Caching
- Buffers: Temporarily store data in memory to reduce I/O overhead.
- Caching: Consolidates multiple small operations into fewer physical disk accesses for efficiency.