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

lect10

Uploaded by

Ray M
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)
6 views

lect10

Uploaded by

Ray M
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/ 3

Date: February 5-6, 2001 CSI 2131 Page: 1

Prof. Lucia Moura Lecture 10

Reclaiming Space in Files


Last Time : File Compression : Lempel-Ziv Codes

Today : Reclaiming Space in Files (Section 6.2)

Reclaiming Space in Files (Section 6.2)


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

We know how to create a file, add records to a file and modify the content
of a record. These actions can be performed physically by using the various
basic file operations we have seen (open a file for writing or appending, go-
ing to a position of a file using “seek” and writing the new information there).

What happens if records need to be deleted ?

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.

Strategies for record deletion and for reusing the unused space :

1) Record Deletion and Storage Compaction

Deletion can be done by “marking” a record as deleted.

Ex.: - Place ‘*’ at the beginning of the record; or


- Have a special field that indicates one of two states: “deleted” or “not
deleted”.

Note that the space for the record is not released, but the program that ma-
nipulates the file must include logic that checks if record is deleted or not.

After a lot of records have been deleted, a special program is used to squeeze
the file - this is called Storage Compaction.
Date: February 5-6, 2001 CSI 2131 Page: 2
Prof. Lucia Moura Lecture 10

2) Deleting Fixed-Length Records and Reclaiming Space Dynami-


cally

How to use the space of deleted records for storing records that are added
later ?

Use an “avail list”, a linked list of available records.


• a header record (at the beginning of the file) stores the beginning of
the avail list (-1 can represent the null pointer).
• 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.

Ex.: After deletions the file may look like :

List head → 4
Edwards Williams *-1 Smith *2 Sethi
0 1 2 3 4 5

If we add a record, it can go to the first available spot in the avail list
(RRN=4).

3) Deleting Variable Length Records

Use an avail list as before, but take care of the variable-length difficulties.

The records in avail list must store its size as a field. RRN can not be
used, but exact byte offset must be used.

List head → 34
Edwards|M| Wu|F| *-1|10| Smith|M| *16|30|
0 1 2 3 4
10 bytes 5 10 bytes 8 bytes 30 bytes
bytes
Addition of records must find a large enough record in avail list.
Date: February 5-6, 2001 CSI 2131 Page: 3
Prof. Lucia Moura Lecture 10

There are several Placement Strategies for selecting a record in avail


list when adding a new record:

1. First-Fit Strategy

• avail list is not sorted by size.


• First record large enough to hold new record is chosen.

2. Best-Fit Strategy

• avail list is sorted by size.


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

3. Worst-Fit Strategy

• avail list is sorted by decreasing order of size.


• Largest record is used for holding new record; unused space can
be placed again in avail list.

When choosing between strategies we must consider two types of fragmenta-


tion :

Internal Fragmentation: wasted space within a record.

External Fragmentation: space is available at avail list, but it is so


small that cannot be reused.

Ways of combating external fragmentation :

• Coalescing the holes : if two records in avail list are adjacent, com-
bine them into a larger record.

• Minimize fragmentation by using one of the previously mentioned place-


ment strategies (for example : worst-fit strategy is better than best-fit
strategy in terms of external fragmentation).

You might also like