/    /  OS – File Operations in OS

File Operations in OS

 

The Operating system is responsible for performing the following file operations using various system calls:

  1. Read
  2. Write
  3. Create
  4. Delete
  5. Truncate files
  6. Reposition 

 

Read files: The OS needs a read pointer to read a file from a specific location in the file. 

To read files through command line, we use the type command. For huge files, we use the more command to read files one page at a time.

 

Write files: The system call uses the same pointers to write a file, it helps to save space and reduce complexity.

 

Create files: The user should have enough space to create a file. When a file is created, a directory entry is made.

To create files through command line, the syntax is type nul > (filename).(filetype)

 

Delete files: The user can look up the file name and delete it. It will release the space occupied by the file and remove the directory entries for the file.

The del command is used when we want to delete a file through command line.

 

Truncate files: The user can delete information from the file, instead of deleting it as a whole. This changes the file length, though the other attributes remain the same.

 

Repositioning files: The current file-position pointer can be repositioned to a new given value. 

 

There are also other file operations like appending a file, creating a duplicate of the file, and renaming a file.

 

Open-File Table

 

To avoid frequent searches in the directory the OS keeps an open file table. When a file operation is requested, the system calls it via an index value. 

If the file is not searched or accessed frequently the OS closes the file and removes its entry from the open file table.

 

There can be several files open at the same time, therefore the system maintains two file tables:

  1. Per-process table
  2. System-wide table.

 

File operations in os reference