You are on page 1of 45

Chapter 5: File-System & Memory Management

File Concept
A file is a named collection of related information that is recorded on secondary storage A file is sequence of bits, bytes, lines, or records A file has a certain defined structure according to its type The information in a file is defined by its creator Many different types of information may be stored in a file: source programs, object programs, executable programs, numeric data, text, payroll records, graphics images, sound recordings and so on

File Attributes
A file has certain attributes which vary from one OS to another Name only information kept in human-readable form Identifier unique tag (number) identifies file within file system Type needed for those systems that support different types Location pointer to device & to the 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

File OperatiOns
File is an abstract data type Creating a file : Two steps are necessary to create a file; 1. Space in file system 2. An entry for the new file must be made 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 Reading a file : To read from a file, we use a system call that specifies the name of the file and where the next block of the file should be put Repositioning within a file : The directory is searched for the appropriate entry and the current file position is set to a given value

Deleting a file : To delete a file, we search the directory for the named file Truncating a file : Keeping attribute of a file same and erasing a contents of a file. This function allows all attributes to remain unchanged but for the file to be reset to length zero.

File types

access MethOds
Sequential Access read next write next reset no read after last write (rewrite) read n write n position to n read next write next rewrite n n = relative block number

Direct Access


Information in the file is processed in order, one after other A read operation reads the next portion of the file and automatically advances a file pointer A write operation appends to the end of the file and advances to the end of the newly written material It is based on a tape model of a file

A file is made up of fixed-length logical records that allow programs to read and write records rapidly in no particular order The direct access method is based on a disk model of a file, since disks allow random access to any file block The file is viewed as a numbered sequence of blocks or records A direct access file allows arbitrary blocks to be read or written We may read block 14, then read block 53 and then write block 7, there are no restriction on the order of reading or writing



Direct access
1. It is based on random access devices like floppy disk, hard disk, CD-ROM 2. It is fast 3. It can directly access the records or blocks 4. No specific operation 5. No time consuming process

1. It is tape based systems

2. It is slow 3. To access nth record it has to scan n-1 records 4. It has only two operation forward & backward 5. It is time consuming process

Operating System

Swapping is a technique of temporarily removing inactive program from the memory of a system.

A process can be swapped temporarily out of memory to If backing store, and processprocess finishes, the lower When the higher priority arrives and wants service, a a higher priority then brought back into memory for the memory manager swapped out in lower priority priority process can becan swap back theand continued. continued execution process so that it can load and execute the higher priority process.

An allocation method refers to how disk blocks are allocated for files Three major methods of allocating disk space are wide use: Contiguous allocation, Linked allocation, Indexed allocation It requires each file to occupy a set of contiguous blocks on the disk No head movement requires, Only starting location (block #) and length (number of blocks) are required

Allocation Methods

Contiguous Allocation of Disk Space

Accessing a file that contiguously is easy




For sequential access, the file system remembers the disk address of the last block referenced and when necessary, reads the next block For direct access to block i of a file that starts at block b, can access block b+i, thus both sequential & direct access can be supported by contiguous allocation
Demerits Wasteful of space (dynamic storage-allocation problem) Files cannot grow Suffers from external fragmentation

Contiguous Allocation of Disk Space

It is defined by the disk address and length of the first block The directory entry for each file indicates the address of the starting block and the length of the area allocated for this file

Linked Allocation
Each file is a linked list of disk blocks, the disk blocks may be scattered anywhere on the disk The directory contains a pointer to the first and last block of the file Each block contains a pointer to the next block To create a file, we simply create a new entry in the directory, each entry has a pointer to the first disk block of the file To read a file, we simply read blocks by following the pointers from block to block There is no external fragmentation Can be used effectively for sequential access files Space required for the pointers

directory file Jeep

0 1 10 2 3

start 9

end 25

4 8 12 16 1 20 24 28

5 9 16 13 17 21 25 -1 29

6 10 25 14 18 22 26 30

7 11 15 19 23 27 31

A file of the five blocks starts at block 9, continue at block 16, then block 1, block 10, and finally block 25

Indexed Allocation
Each file has its own index block The directory contains the address of the index block To read the ith block , we use the pointer in the ith index-block entry to find and read the desired block When the ith block is first written, a block is obtained from the free-space manager, and its address is put in the ith index-block entry Indexed allocation supports direct access, without suffering from external fragmentation Indexed allocation does suffer from wasted space

directory file Jeep

0 4 8 12 16 20 24 28 1 5 9 13 17 21 25 29 2 6 10 14 18 22 26 30 3 7 11 15 19 23 27 31

index block 19

9 16 19 1 10 25

Basic Memory Management Functions of Memory Management: Keeping track of the status of each memory location, allocated or free Determining allocation policy for memory Updating memory allocation information De-allocation of memory

1. Fixed Partition Allocation : Divide memory into a number of fixed-sized partition. Each partition may contain exactly one process When a partition is free, a process is selected from the input queue and is loaded into the free partition When the process terminates, the becomes available for another process partition

When a process arrives and needs memory, we search for a hole large enough for this process

O. S. 10 K 20 K 10 K 10 K 10 K 10 K 08 K 10 K 12 K 10 K 10 K 10 K 20 K 10 K

All memory is available for user processes and is considered as one large block of available memory, a hole Any process whose size is less than or equal to the partition size can be loaded into any available partition. If all memory partitions are full and needs memory, operating system searches for large hole for this process. If all memory partitions are full and no process is in the ready or running state, the operating system can swap a process out of any of the partitions and load in another process.

Equal size fixed Unequal size fixed Partition Partition

2. Variable Partitioning : All memory is available for user processes and is considered as one large block of available memory, called hole Memory partitions are variable length and number. When a process arrives & needs memory, it is allocated exactly as much memory as it requires, keeping the rest available to satisfy future requests.

For ex, we have 2560K of memory available and a resident operating system of 400K. This leaves 2160K for user processes

400K Job queue
Process memory time



10 5 20 8 15

2160K hole

P2 P3 P4 P5

300K 700K 500K


Allocate memory to processes P1, P2 & P3 0 OS 400K P1

Using RR, P2 terminates releasing its memory 0 OS 400K P1

Schedule next process P4

Process P1 Schedule terminate, next process releasing its P5 memory 0 0 OS 400K 400K
(600k) (600K)

0 OS 400K P1









2160 P2 1560


1000K P2 (1000k) (1000K)


1000K P4 (1000k)

(500K) P5 (600k) 900K 100K 1000K










1700K 2000K P3 560 (300K) 2000K


1700K 2000K




1700K 2000K


2300K 2560K 260K

2300K 2560K 260K

2300K (300K) 2560K 260K


2300K (300K) 2560K 260K


2300K (300K) 2560K 260K


Hole 260K size

Problem of general dynamic storage allocation i.e. how to satisfy a request of size n from a list of free holes Solution to this problem is search to determine which hole is best to allocate First-fit : Allocate the first hole that is big enough. Start searching the hole, as soon as we find a free hole that is large enough, stop searching Best-fit : Allocate the smallest hole that is big enough. We must search entire list, unless the list is kept ordered by size. Worst-fit : Allocate the largest hole. We must search the entire list, unless it is sorted by size


12 KB

6 KB

14 KB

12 KB 14

19 KB

19 12 KB

Best-fit First-fit Allocate the Worst-fit :: : Allocate the Allocate the smallest hole. We must first hole that is big largest hole that is big enough. the entire list, Start searching search We must search entire it is unless the list the hole, sorted as we unless list, as soonby size is kept ordered by that is find a free hole size. large enough, stop searching

11 KB

13 KB Primary memory

12 KB Worstfit First fit Best

6 KB

14 KB

12 KB 14

19 KB

19 12 KB

11 KB

13 KB Primary memory First fit

12 KB Best fit Worst fit

Given mem. partitions of 10K, 4K, 20K, 18K, 7K, 9K, 12K, 15K Place processes of 12K, 10K & 9K using algorithms.
10K 10K 4K 8K 20K 12K 9K 9K 18K 7K 9K 12K 15K 12K 20K 10K 18K 9K 7K 9K 12K 15K 9K 7K 9K 9K 12K 12K 15K 10K 20K 18K 10K 4K 12K 10K 10K 4K

First Fit

Memory Partitions

Best Fit

10K 4K 20K 18K 7K 9K 12K 9K 15K 10K 12K

10K 4K 12K 20K 8K 8K 10K 18K 8K 7K 9K 12K 9K 15K 6K

Memory Partitions

Worst Fit

Free Space Management

In dynamic storage allocation, to keep track of free disk space, the system maintains a free-space list The free-space list records all disk blocks that are free-those not allocated to some file or directory To create a file, we search the free-space list for the required amount of space, and allocate that space to the new file This space is then removed from the free-space list When a file is deleted, its disk space is added to the free-space list.

Bit Vector : The free-space list is implemented as bit map or bit vector Each block is represented by 1 bit. If the block is free, the bit is 1; if the block is allocated, the bit is 0 For ex, a disk blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are free, & rest of the blocks are allocated. The free-space bit map would be
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0011110011 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0

Linked 1 0 0 1 : List 1 00111

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0

Link all the free disk blocks, keeping a pointer to the first free block in a special location on the disk & caching it in memory The first block contains a pointer to the next free disk block, and so on For ex, a pointer is on block 2 as the first block. Block 2 would contain a pointer to block 3 & so on

A Typical File-system Organization

Operations Performed on Directory


for a file Create a file Delete a file List a directory Rename a file Traverse the file system

Organize the Directory (Logically) to Obtain Efficiency locating a file quickly Naming convenient to users

users can have same name for different files The same file can have several different names

Grouping logical grouping of files by properties, (e.g., all Java programs, all games, )

Single-Level Directory

A single directory for all users All files are contained in the same directory, which is easy to support & understand

Naming problem Grouping problem

Two-Level Directory

Separate directory for each user Each user has his own user file directory (UFD) Each UFD has a similar structure, but lists only the files of a single user When a user job starts, the systems master file directory (MFD) is searched When a user refers to a particular file, only his own UFD is searched

Tree-Structured Directories

Tree-Structured Directories (Cont)

Efficient searching Allows users to create their own subdir. Grouping Capability Current directory (working directory)


/spell/mail/prog type list

Tree-Structured Directories (Cont) Absolute or relative path name Creating a new file is done in current directory Delete a file

rm <file-name> Creating a new subdirectory is done in current directory

mkdir <dir-name>

Ex., if in current directory /mail

mkdir count
prog mail copy prt exp count

Deleting mail deleting the entire subtree rooted by mail


File owner/creator should be able to control:


can be done by whom

Types of access
Read Write Execute Append Delete List

Access Lists and Groups

Mode of access: read, write, execute Three classes of users RWX a) owner access 7 111

b) group access c) public access

6 1

110 001

Windows XP Access-control List Management

A Sample UNIX Directory Listing