Operating Systems Chapter 6: File Management Instr: Yusuf Altunel IKU Department of Computer Engineering
Content Introduction Files Directories File System Implementation File Naming File Types File Operations Directories File System Implementation Implementing Files Implementing Directories
Introduction Long Term Information Storage It should be possible To store a very large amount of information The information must survive Even after the termination of the process Multiple processes must be able to access the information concurrently
FILES Persistent storage of information Not affected by Can be located the creation and termination of processes Can be located on disks or any other external media Processes can read and write data Managed by the Operating System
File System Part of the operating system File system responsibilities: to manage the files: File system responsibilities: Structuring the files File Naming File Access File Usage File Protection File Implementation
Naming the Files Process gives a new name to the file it creates Using the name The same file can be accessed by other processes even after the termination using the name File naming vary from one system to another All systems allow strings of 1 to 8 letters Digits and special characters are also permitted Many file systems support names as long as 255 characters Some systems (like UNIX) distinguish between upper and lower case characters While some others (like DOS and WINDOWs) does not
Naming the Files File Name Extensions Many operating system support two-part file names Separated by a period In DOS the extension can be 3 characters While UNIX allows longer extensions More than one extension is possible
Some File Extensions Extension Meaning file.bak Backup file file.c C source program file.gif Graphical Interchange Format image file.html World Wide Web HyperText Markup Language document file.iso ISO image of a CD-ROM (for burning to CD) file.jpg Still picture encoded with the JPEG standard file.mp3 Music encoded in MPEG layer 3 audio format file.mpg Movie encoded with the MPEG standard file.o Object file (compiler output, not yet linked) file.pdf Portable Document Format file file.ps PostScript file file.tex Input for the TEX formatting program file.txt General text file file.zip Compressed archive
File Structure Byte Sequence File consist of ... Byte Sequence File consist of unstructured sequences of bytes OS does not know (or care) what is in the file Any meaning must be imposed by user-level programs
File Structure Record Sequence File is structured Each record as a sequence of fixed-length records Each record has its own internal structure Each read operation returns one record at a time Record 1 Record 2 Record 3 ...
File Structure Tree File consists of a tree of records The length of the records is not necessarily the same Each record contain a key Rapid searching is allowed The OS decides where to store new records Ant Fox Pig Cat Cow Dog Goat Lion Owl Pony Rat Worm Hen Ibis Lamb
File Types Regular Files Directories Character Special Files Contain user information Data content would be any of ASCII characters Binary data (binary or hexadecimal) Directories Special System files to keep the file system structures Character Special Files Related with I/O devices Used to model serial I/O devices Such as terminals, printers, networks Block Special Files Used to model disks
(a) An executable file (b) An archive Examples of UNIX Files (a) An executable file (b) An archive
File Access Sequential access Random access read all bytes/records starting from the beginning cannot jump around, could rewind or back up convenient for magnetic tapes Random access bytes/records read in any order essential for data base systems no limitataion on read types move file marker (seek), then read … read and then move file marker …
File Attributes OS may assign additional information to files File craeation date and time Last modification time The size of the file ... See the table For a list of such attributes Notice, not all OSs use them
File Attributes Attribute Meaning Protection Who can access the file and in what way Password Password needed to access the file Creator ID of the person who created the file Owner Current owner Read-only flag 0 for read/write; 1 for read only Hidden flag 0 for normal; 1 for do not display in listings System flag 0 for normal files; 1 for system files Archive flag 0 for has been backed up; 1 for needs back up ASCII/Binary flag 0 for ASCII file; 1 for for binary Random access flag 0 for sequential access only; 1 for random access Lock flags 0 for unlocked; nonzero for locked Record length Number of bytes in a record Key position Offset of the key within each record Key length Number of bytes in the key field Creation time Date and time the file was created Time of last access Date and time the file was last accessed Time of last change Date and time the file has last changed Current size Number of bytes in the file Maximum Size Number of bytes the file may grow to
File Operations Create Delete Open Close Read Write Create a new file with no data Delete Delete the file when it is no longer needed Open fetch the attributes and disk addresses before starting to use the file Close Free table space when the file no longer used Read Data is transferred from the disk into the buffer Write Data is written to the file
File Operations Append Seek Get Attributes Set Attributes Rename To write to the end of the file increases the file size Seek To reposition the file pointer to a specific place in the file Get Attributes To get the attributes of the file Set Attributes To change the attributes of the file Rename Change the name of the file
Directories Directory organizations: Single level: Two-level: Only one directory is allowed All files stored in this directory Two-level: A root and a second level directories To distinguish the user files from other files Hierarchical Directory Systems A tree structure of directories Helps the users to organize their work
Directory Structures A B C A B C D E F G H Directory Examples Root directory Root directory Root directory A B C A B C D E F G H Single level directory system Two- level directory system Hierarchical directory system Directory Examples
cp /usr/ast/mailbox /usr/ast/mailbox.bak A UNIX Directory Tree cp /usr/ast/mailbox /usr/ast/mailbox.bak
Directory Operations Create Delete Opendir Closedir Create a new directory Delete Delete the directory. Some systems requires directories be empty before deleting Opendir To prepare for directory listing Closedir Free table space after read
Directory Operations Read Rename Link Unlink To list the directory content Rename To change the directory name Link To appear a file in more than one directory Increases the counter in file’s i-node Unlink A file link to the directory is removed If the count i-node was 1 file is removed from the file system
File System Implementation File systems are stored on disks Disks can be divided into one or more partitions Independent file systems on each partition MBR (Master Boot Record) the first sector of the disk Sector 0 the 512-byte boot sector Partition Table Keeps start-end address of each partition One of the partitions is marked as active Every partition starts with a boot block Even if it contain no bootable OS
Content of File Partitions Boot Block The first block that BIOS will read and execute Used to load the OS located in that partition Superblock Comes after the boot block Contains the key parameters about the file system Magic number to identify the file system type The number of blocks in the file system Other administrative information Loaded to memory when the computer is booted
Content of File Partitions Free Blocks Comes after the superblock Is generally a bitmap or a list of pointers To identify the free blocks in the partition i-Nodes An array used to keep file blocks Tells everything about the file the address of disk blocks belonging the file Other information for the file Each file in the system has one i-node Root Directory Contains the top of the file system tree Other directories and files
File System Layout A file system layout
File Implementation Contiguous Allocation Linked List Allocation i-nodes
Contiguous Allocation keep files as contiguous disk blocks On a disk with 1KB blocks 50KB file would allocate 50 consecutive blocks Advantages Simple to implement The read performance is perfect Drawbacks Disk becomes fragmented Needs to be compacted The file’s final size must be known Otherwise the file must be moved File A (4 blocks) File B (3 blocks) File C (6 blocks) File E File D (5 blocks) File F File A (4 blocks) 3 free blocks File C (6 blocks) 6 free File D (5 blocks) File F After B and E are removed
Linked List Allocation Keep files in linked lists of disk blocks The first word of each block is used as a pointer to the next block The rest is data No disk space is lost as a fragmentation It is sufficient for a directory entry of the file To keep only the first block address The rest can be found after reading the blocks Random access is too slow!
Linked List Allocation Storing a file as a linked list of disk blocks
Linked List Allocation FAT (File Allocation Table) Linked list allocation can be modified using a Table in memory Pointer words of each block are stored in a table in memory Such a table is called FAT The entire table must be in memory it is hard to keep FAT in memory if disc is large
FAT examples: Files A and B FAT in Memory Pyhsical Block Pointer File Name 1 2 10 3 11 4 7 File A 5 6 File B 8 9 12 14 NULL 13 FAT examples: Files A and B
i-Nodes The abbreviation of index-node To keep track of which blocks are belonging to which file Contains a list of file attributes Disk addresses of file blocks If the file grows beyond the limit some entries are reserved for blocks of addresses
Example: i-Node On the Disk In Memory An i-node example File Attributes Address of file’s block 0 Address of file’s block 1 Address of file’s block 2 Address of file’s block 3 Address of file’s block 4 Address of file’s block 5 Address of file’s block 6 Address of block of pointers In Memory Address of file’s block 7 Address of file’s block 8 Address of file’s block 9 Address of file’s block 10 Address of file’s block 11 Address of file’s block 12 Address of file’s block 13 On the Disk An i-node example
Implementing Directories Directories used to keep file attributes A simple design A directory consist of A list-of fixed size entries one per file Containing a fixed-length file name A structure of the attributes Disk addresses to tell where the disk blocks are Systems use I-Node File attributes are stored in the i-node Directory entry for each file is shorter A file name An i-node number
Directory Implementation (a) A simple directory fixed size entries disk addresses and attributes in directory entry (b) Directory entry just refers to an i-node
Handling Long File Names File 1 entry length File 1 attributes File 1 Name File 2 entry length File 2 attributes File 2 Name File 3 entry length File 3 attributes File 3 Name The simplest approach: Reserve a fixed amount of space Typically 255 characters Simple but wastes too much space Directory entries are free in size Each directory entry contains a fixed portion holding the length of the entry A fixed length header holds The file attributes The actual name of the file Can be of any length
Long File Names (a) In-line (b) In a heap
End of Chapter 6 File Management