Download presentation
Presentation is loading. Please wait.
1
Lecture 9 : chapter 9 and 10 file system
Dr. Nermin Hamza
2
File Concept Contiguous logical address space Types: numeric character
Data numeric character binary Program The operating system abstracts from the physical properties of its storage devices to define a logical storage unit, the file.
3
File Concept Files are mapped by the operating system onto physical devices. These storage devices are usually nonvolatile, so the contents are persistent through power failures and system reboots. From a user’s perspective, a file is the smallest allotment of logical secondary storage; that is, data cannot be written to secondary storage unless they are within a file.
4
File Structure None - sequence of words, bytes Simple record structure
Lines Fixed length Variable length Complex Structures Formatted document Relocatable load file Can simulate last two with first method by inserting appropriate control characters Who decides: Operating system Program
5
A text file is a sequence of characters organized into lines (and possibly pages).
A source file is a sequence of subroutines and functions, each of which is further organized as declarations followed by executable statements. An object file is a sequence of bytes organized into blocks understandable by the system’s linker. An executable file is a series of code sections that the loader can bring into memory and execute.
6
File Attributes Name – only information kept in human-readable form
Identifier – unique tag (number) identifies file within file system Type – needed for systems that support different types Location – pointer to file location on device Size – current file size Protection – controls who can do reading, writing, executing Time, date, and user identification – data for protection, security, and usage monitoring Information about files are kept in the directory structure, which is maintained on the disk
7
File Operations File is an abstract data type
Create: space, an entry for the new file must be made in the directory. Write: system call specifying both the name of the file and the information to be written to the file Read: system call that specifies the name of the file and where (in memory) Reposition within 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.
8
File Operations Delete: search the directory for the named file. Having found the associated directory entry, we release all file space, so that it can be reused by other files, and erase the directory entry. Truncate: The user may want to erase the contents of a file but keep its attributes. appending - renaming - copy Open(Fi) – search the directory structure on disk for entry Fi, and move the content of entry to memory Close (Fi) – move the content of entry Fi in memory to directory structure on disk
9
Open Files The operating system keeps a small table, called the open-file table, containing information about all open files. The open() call can also accept access mode information—create, read-only, read–write, append-only, and so on
10
Open Files Several pieces of data are needed to manage open files:
File pointer: pointer to last read/write location, per process that has the file open File-open count: counter of number of times a file is open – to allow removal of data from open-file table when last processes closes it Disk location of the file: cache of data access information Access rights: per-process access mode information
11
Open File Locking Some operating systems provide facilities for locking an open file (or sections of a file). File locks allow one process to lock a file and prevent other processes from gaining access to it. File locks are useful for files that are shared by several processes Provided by some operating systems and file systems Mediates access to a file Mandatory or advisory: Mandatory – access is denied depending on locks held and requested Advisory – processes can find status of locks and decide what to do
12
File Locking Example – Java API
import java.io.*; import java.nio.channels.*; public class LockingExample { public static final boolean EXCLUSIVE = false; public static final boolean SHARED = true; public static void main(String arsg[]) throws IOException { FileLock sharedLock = null; FileLock exclusiveLock = null; try { RandomAccessFile raf = new RandomAccessFile("file.txt", "rw"); // get the channel for the file FileChannel ch = raf.getChannel(); // this locks the first half of the file - exclusive exclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE); /** Now modify the data */ // release the lock exclusiveLock.release();
13
File Locking Example – Java API (cont)
// this locks the second half of the file - shared sharedLock = ch.lock(raf.length()/2+1, raf.length(), SHARED); /** Now read the data */ // release the lock sharedLock.release(); } catch (java.io.IOException ioe) { System.err.println(ioe); }finally { if (exclusiveLock != null) exclusiveLock.release(); if (sharedLock != null) }
14
File Types – Name, Extension
15
Access Methods Sequential Access read next write next reset
no read after last write (rewrite) The simplest access method is sequential access. Information in the file is processed in order, one record after the other
16
Sequential-access File
17
Access methods Direct Access read n write n position to n read next
write next rewrite n n = relative block number
18
Simulation of Sequential Access on Direct-access File
19
Access methods Indexed Access
It contained key fields and pointers to the various blocks. To find an entry in the file for a key value, we first search the index and then use the pointer to directly access a file and find the desired entry
20
Example of Index and Relative Files
21
Directory Structure A collection of nodes containing information about all files Directory Files F 1 F 2 F 3 F 4 F n Both the directory structure and the files reside on disk Backups of these two structures are kept on tapes
22
File-System Structure
File structure Logical storage unit Collection of related information File system resides on secondary storage (disks) File system organized into layers File control block (FCB) – storage structure consisting of information about a file A directory structure (per file system) is used to organize the files. In NTFS, it is stored in the master file table.
23
A Typical File Control Block
24
Terminology A boot-control block: contains information needed by the system to boot an operating system from that volume. If the disk does not contain an operating system, this block can be empty. It is typically the first block of a volume A volume-control block: contains volume (or partition) details, such as the number of blocks in the partition, the size of the blocks, a free-block count and free-block pointers, and a free-FCB count and FCB pointers. The system-wide open-file table contains a copy of the FCB of each open file, as well as other information. The per-process open-file table contains a pointer to the appropriate entry in the system-wide open-file table, as well as other information
25
In-Memory File System Structures
The open() call passes a file name to the logical file system. The open() system call first searches the system- wide open-file table to see if the file is already in use by another process. If it is, a per-process open-file table entry is created pointing to the existing system-wide open-file table. If the file is not already open, the directory structure is searched for the given file name
26
In-Memory File System Structures
The following figure illustrates the necessary file system structures provided by the operating systems. Figure 12-3(a) refers to opening a file. Figure 12-3(b) refers to reading a file.
27
In-Memory File System Structures
28
Directory Implementation
1- Linear list of file names with pointer to the data blocks. simple to program time-consuming to execute To create a new file, we must first search the directory to be sure that no existing file has the same name Then, we add a new entry at the end of the directory. To delete a file, we search the directory for the named file and then release the space allocated to it. The real disadvantage of a linear list of directory entries is that finding a file requires a linear search.
29
Directory Implementation
2-Hash Table – linear list with hash data structure. a linear list stores the directory entries, but a hash data structure is also used. The hash table takes a value computed from the file name and returns a pointer to the file name in the linear list. decreases directory search time major difficulties with a hash table are: fixed size
30
Allocation Methods An allocation method refers to how disk blocks are allocated for files: Contiguous allocation Linked allocation Indexed allocation
31
Contiguous Allocation
Each file occupies a set of contiguous blocks on the disk Simple – only starting location (block #) and length (number of blocks) are required Random access Wasteful of space (dynamic storage-allocation problem) Files cannot grow
32
Contiguous Allocation
Mapping from logical to physical Q LA/512 R Block to be accessed = ! + starting address Displacement into block = R
33
Contiguous Allocation of Disk Space
34
The contiguous-allocation problem can be seen as a particular application of the general dynamic storage-allocation: how to satisfy a request of size n from a list of free holes First fit and Best fit and Worst fit First fit and Best fit are the most common strategies used to select a free hole from the set of available holes. both first fit and best fit are more efficient than worst fit in terms problem of external fragmentation ….compact all free space into one hole.
35
Extent-Based Systems Many newer file systems (I.e. Veritas File System) use a modified contiguous allocation scheme Extent-based file systems allocate disk blocks in extents An extent is a contiguous block of disks Extents are allocated for file allocation A file consists of one or more extents.
36
Linked Allocation Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk. The directory contains a pointer to the first and last blocks of the file. pointer block =
37
Linked Allocation For example, a file of five blocks might start at block 9 and continue at block 16, then block 1, then block 10, and finally block 25
38
Linked Allocation (Cont.)
Simple – need only starting address Free-space management system – no waste of space No random access Mapping Q LA/511 R Block to be accessed is the Qth block in the linked chain of blocks representing the file. Displacement into block = R + 1 File-allocation table (FAT) – disk-space allocation used by MS-DOS and OS/2.
39
File-Allocation Table
An important variation on linked allocation is the use of a file-allocation table (FAT). This simple but efficient method of disk-space allocation is used in the MS-DOS and OS/2 operating systems
40
Indexed Allocation Brings all pointers together into the index block.
Logical view. index table
41
Example of Indexed Allocation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.