0% found this document useful (0 votes)
1 views

L05

Uploaded by

nouryones38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

L05

Uploaded by

nouryones38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1/8/23

Organizing Files for Performance

Lecture No. 5

Contents

1 Motivation

2 Reclaiming Spaces in Files

3 Fragmentation in Physical Storage

7 LOGO
7

1
1/8/23

Motivation

8 LOGO
8

Motivation
v Let us consider a file of records (fixed length or variable
length)

v We know how to create a file, how to add records to a file,


modify the content of a record. These actions can be
performed physically by using the various basic file
operations we have seen (open, close, seek, read, write)

What happens if records need to be deleted?

v There is no basic operation that allows us to remove part


of a file. Record deletion should be taken care by the
program responsible for file organization
9 LOGO
9

2
1/8/23

Reclaiming
Spaces in Files

10 LOGO
10

Strategies for Record Deletion


v How to delete records and reuse the unused space?

1. Record Deletion and Storage


Compaction

2. Deleting Fixed-Length Records


and Reclaiming Space
Dynamically
File of fixed length records

3. Deleting Variable-Length Records Deleted Record

11 LOGO
11

3
1/8/23

Strategies for Record Deletion


1. Record Deletion and Storage Compaction
v Deletion can be done by marking a
record as deleted

v Note that the space for the record is


not released, but the program that
manipulates the file must include
logic that checks if record is deleted
or not. File of fixed length records

v After a lot of records have been deleted, a special program is


used to squeeze the file-that is called Storage Compaction

12 LOGO
12

Strategies for Record Deletion


2. Deleting Fixed-Length Records and Reclaiming
Space Dynamically
v How to use the space of deleted
records for storing records that are
added later?
v Use an “AVAIL LIST”, a linked list of
available records.
v A header record stores the beginning
of the AVAIL LIST File of fixed length records

v When a record is deleted, it is marked as deleted and


inserted into the AVAIL LIST. The record space is in the same
position as before, but it is logically placed into AVAIL LIST
13 LOGO
13

4
1/8/23

Strategies for Record Deletion


2. Deleting Fixed-Length Records and Reclaiming
Space Dynamically

v If we add a record, it can go to the first available spot in


the AVAIL LIST where RRN=4.

14 LOGO
14

Strategies for Record Deletion


3. Deleting Variable-Length Records

v Use an AVAIL LIST as before,


but take care of the variable-
length difficulties

v The records in AVAIL LIST


must store its size as a field. File of variable length records

v RRN can not be used, but exact byte offset must be used

v Addition of records must find a large enough record in AVAIL


LIST.
15 LOGO
15

5
1/8/23

Strategies for Record Deletion


3. Deleting Variable-Length Records

v Addition of records must find a large enough record in AVAIL


LIST.

16 LOGO
16

Placement Strategies for New Records


v There are several strategies for selecting a record from
AVAIL LIST when adding a new record:

1. First-Fit Strategy

v AVAIL LIST is not sorted by size.


v First record large enough to hold new record is chosen.
v Example:
§ AVAIL LIST: size=10,size=50,size=22,size=60
§ record to be added: size=20
§ Which record from AVAIL LIST is used for the new record?

17 LOGO
17

6
1/8/23

Placement Strategies for New Records


2. Best-Fit Strategy

v AVAIL LIST is sorted by size.


v Smallest record large enough to hold new record is
chosen.

v Example:
§ AVAIL LIST: size=10,size=22,size=50,size=60
§ record to be added: size=20
§ Which record from AVAIL LIST is used for the new record?

18 LOGO
18

Placement Strategies for New Records


3. Worst-Fit Strategy

v AVAIL LIST is sorted by decreasing order of size.


v Largest record is used for holding new record; unused
space is placed again in AVAIL LIST.

v Example:
§ AVAIL LIST: size=60,size=50,size=22,size=10
§ record to be added: size=20
§ Which record from AVAIL LIST is used for the new record?

19 LOGO
19

7
1/8/23

How to choose between Strategies?


v We must consider two types of fragmentation within a file:

v Internal Fragmentation
§ wasted space within a record.

v External Fragmentation
§ space is available at AVAIL LIST, but it is so small that cannot be
reused.

20 LOGO
20

Study This !
v For each of the following approaches, which type of
fragmentation arises, and which placement strategy is more
suitable?

v When the added record is smaller than the item taken from
AVAIL LIST:

§ Leave the space unused within record


• type of fragmentation: internal
• suitable placement strategy: best-fit

§ Return the unused space as a new available record to AVAIL


LIST
• type of fragmentation: external
• suitable placement strategy: worst-fit
21 LOGO
21

8
1/8/23

Fragmentation in
Physical Storage

22 LOGO
22

Physical File Storage


v Each of your disks contains its own index file so that
information about its contents is always available when the
disk is in use.

v When you save a file, the operating system looks at the


index file to see which clusters are empty. It selects one of
these empty clusters, records the file data there, and then
revises the index file to include the new file name and its
location.

23 LOGO
23

9
1/8/23

Physical File Storage


v A file that does not fit into a single cluster spills over into the
next contiguous (meaning adjacent) cluster, unless that
cluster already contains data.

v When contiguous clusters are not available, the operating


system stores parts of a file in noncontiguous ( nonadjacent)
clusters.

24 LOGO
24

Physical File Storage

Bio.txt is stored in contiguous clusters.


Jordan.wks is stored in noncontiguous clusters.
A computer locates and displays the Jordan.wks
file by looking for its name in the Master File
Table.

25 LOGO
25

10
1/8/23

What happens when a file is deleted


v When a file is deleted, the operating system simply changes
the status of the file’s clusters to “empty” and removes the
file name from the index file.

v The file name no longer appears in a directory listing, but the


file’s data remains in the clusters until a new file is stored
there.

v It is possible to purchase utilities that recover a lot of this


supposedly deleted data.

26 LOGO
26

Fragmentation & Defragmentation


v As a computer writes files on a disk, parts of files tend to
become scattered all over the disk.

v These fragmented files are stored in noncontiguous clusters.


Drive performance generally declines as the read-write heads
move back and forth to locate the clusters containing the parts
of a file.

v To regain peak performance, you can use a defragmentation


utility, such as Windows Disk Defragmenter, to rearrange the
files on a disk so that they are stored in contiguous clusters

27 LOGO
27

11
1/8/23

Fragmentation & Defragmentation

On the fragmented disk, the When the disk is defragmented,


purple, orange, and blue files the sectors of data for each file
are stored in noncontiguous are moved to contiguous
clusters. clusters.

28 LOGO
28

Fragmentation & Defragmentation

29 LOGO
29

12

You might also like