Files Concepts:
• A file is a named collection of related information that is recorded on secondary storage such as magnetic disks, magnetic tapes and optical disks. The file is a sequence of bits, bytes, lines or records whose meaning is defined by the files creator and user.
Attributes of a File
Following are some of the attributes of a file:
• Name . It is the only information which is in human-readable form.
• Identifier. The file is identified by a unique tag(number) within file system.
• Protection. This controls and assigns the power of reading, writing, executing.
• Type. It is needed for systems that support different types of files.
• Location. Pointer to file location on device.
• Size. The current size of the file.
• Reading a file: To read from a file, we use a system call that specifies the name of the file and where (in memory) the next block of the file should be put. Again, the directory is searched for the associated entry, and the system needs to keep a read pointer to the location in the file where the next read is to take place. Once the read has taken place, the read pointer is updated.
• Truncating a file: The user may want to erase the contents of a file but keep its attributes. Rather than forcing the user to delete the file and then recreate it, this function allows all attributes to remain unchanged—except for file length—but lets the tile be reset to length zero and its file space released.
Time, date, and user identification. This is the data for protection, security, and usage monitoring.
File Operations The operating system must do to perform basic file operations given below.
• Creating a file: Two steps are necessary to create a file. First, space in the file system must be found for the file. Second, an entry for the new file must be made in the directory.
• Writing a file: To write a file, we make a system call specifying both the name of the file and the information to be written to the file. Given the name of the file, the system searches the directory to find the file's location. The system must keep a write pointer to the location in the file where the next write is to take place. The write pointer must be updated whenever a write occurs.
• Repositioning within a file: The directory is searched for the appropriate entry, and the current-file-position pointer is repositioned to a given value. Repositioning within a file need not involve any actual I/O. This file operation is also known as a file seek.
• Deleting a file. To delete a file, we search the directory for the named file. Having found the associated directory entry, we release all file space, it can be reused by other files, and erase the directory entry
• Protection: Access-control information determines who can do reading, writing, executing, and so on.
File Types-
A file management system supports following file types:
Transaction file: Stores input data until it can be processed
Master file: Contains all current data relevant to an application
Backup file: Copy of a file, created as a safety precaution against loss of data
Output file: Stores output produced by one program that is used as input to another program
Report file: Holds a copy of a report generated by an application
File System Structure -
A File Structure should be according to a required format that the operating system can understand.
• A source file is a sequence of procedures and functions.
• An object file is a sequence of bytes organized into blocks that are understandable by the machine.
• A file has a certain defined structure according to its type.
• A text file is a sequence of characters organized into lines.
• When operating system defines different file structures, it also contains the code to support these file structure. Unix, MS-DOS support minimum number of file structure. Files can be structured in several ways in which three common structures are given in this tutorial with their short description one by one.
File Structure 1
• Therefore, the OS doesn't care about what is in the file, as all it sees are bytes.
• Here, as you can see from the figure 1, the file is an unstructured sequence of bytes.
File Structure 2
• Central to the idea about a file being a sequence of records is the idea that read operation returns a record and write operation just appends a record.
• Now, as you can see from the figure 2 that shows the second structure of a file, where a file is a sequence of fixed-length records where each with some internal structure.
File Structure 3
• Now in the last structure of a file that you can see in the figure 3, a file basically consists of a tree of records, not necessarily all the same length, each containing a key field in a fixed position in the record. The tree is stored on the field, just to allow the rapid searching for a specific key.