Sunday, June 23, 2013
A number of operations can be carried out on a file. However, there are 6 basic file operations. As we know a file is an ADT or abstract data type. Certain operations need to be considered for defining a file. Operating systems makes calls to these operations.
Following are the six basic operations:
1. Creation of a file:
- This operation involves two steps.
- Firstly, a sufficient space has to be found for storing the file in the file system. - Secondly, for this new file an entry should be made in the directory.
2. Writing to a file:
- For writing data to a file, a system call has to be made with name and the data to be written as its arguments.
- A writer pointer is kept by the system at the location in the file where the next write operation is to be carried out.
- This pointer is updated whenever a write operation occurs.
3. Reading from a file:
- Just like the write operation, in order to read information from a file, a system call has to be generated along with the name of the file and the location of the content to be read as its arguments.
- Here, instead of a write pointer there is a read pointer that will indicate the location where the next read operation is to take place.
- The location at which the current operation is being carried out is kept as a “per – process current – file – position” pointer since the process is either writing to or reading from the file.
- The same pointer can be used by both the read and write operations in order to reduce the complexity of the system as well as for saving space.
4. Re-positioning within a file:
- System carries out search in the directory looking for the appropriate entry.
- When found, the current file position pointer is re-pointed to this position.
- This file operation does not require carrying out any input or output operation in actual.
- Another name for this file operation is the file seek.
5. Deletion of a file:
- For deletion of the file, the system searches through the directory to find the appropriate entry.
- When found, the space held by this file is released and the entry in the directory is destroyed so that this space can be reused by other files.
6. Truncating a file:
- Sometimes you may require deleting only the contents of a file while keeping it attributes.
- Deleting the file and recreating it is not an efficient solution.
- This file operation lets you to erase the contents of the file but save its attributes.
- But here the length attribute of the file will be changed to zero after truncation.
- The file space is released after truncating.
The above mentioned six basic file operations constitute the minimal file operations set. These operations are primary ones and if combined can perform some other secondary file operations such as copying. A table known as the open file table is maintained by the operating system that stores all the information about the files that are currently open. When the file is closed, its entry is deleted from the open file table. Some files have to be opened explicitly with the function open() before using it. The name of the file is passed as an argument to this function. Then it looks in the directory for this file and an entry is made in the open file table. Each file has some access rights. It is in these access modes that a process uses the file. A process can perform only those operations which are permitted by the access rights of the file.