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

Lab 6

The document discusses reclaiming space in files by using an avail list to track deleted records. It covers deletion and addition of fixed-length and variable-length records, and different dynamic replacement strategies like first-fit, best-fit, and worst-fit allocation policies.

Uploaded by

Ali Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 6

The document discusses reclaiming space in files by using an avail list to track deleted records. It covers deletion and addition of fixed-length and variable-length records, and different dynamic replacement strategies like first-fit, best-fit, and worst-fit allocation policies.

Uploaded by

Ali Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Reclaiming Spaces in

Files
LAB 6
Lab Agenda
▪ Avail List
▪ Deletion and Addition of Fixed-Length Records
▪ Deletion and Addition Variable-Length Records (Project)
▪ Dynamic Replacement Strategies
▪ First-Fit
▪ Best-Fit
▪ Worst-Fit
Avail List
▪ A linked list of available records.
▪ Used to easily track the space of deleted records for storing new
records added later.
▪ Has a header record that stores the last deleted record.
Avail List Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2, Rec.5, and Rec.3

NO records deleted: -1
Avail List Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2, Rec.5, and Rec.3

Record 2 deleted: 2 *Rec.2 -1


Avail List Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2, Rec.5, and Rec.3

Record 5 deleted: 5 *Rec.5 2 *Rec.2 -1


Avail List Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2, Rec.5, and Rec.3

Record 3 deleted: 3 *Rec.3 5 *Rec.5 2 *Rec.2 -1


Avail List : Fixed-Length Record
▪ Each deleted record contains the deletion mark and the RRN of the
next deleted record.
▪ Header contains only the RRN of the last deleted record.

Header RRN: 3 RRN: 5 RRN: 2

3 *5 *2 *-1
Avail List : Fixed-Length Record Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2 and Rec.5
NO records deleted : -1

Record 2 deleted: 2 *-1

Record 5 deleted: 5 *2 *-1

New record is added: 2 *-1


Avail List : Variable-Length Record
▪ Each deleted record contains the deletion mark followed by the
byte offset of the next deleted record then the size of the record
itself .
▪Header contains only the byte offset of the last deleted record.

Rec.3 Rec.5 Rec.2


byte: 30 byte: 100 byte: 10
Header Size: 30 Size: 50 Size: 20

30 *100|30 *10|50 *-1|20


Avail List : Variable-Length Record
Example
▪ Assume we have a file with 10 records and the following records
has been deleted in the same order: Rec.2, Rec.5, and Rec.3
NO records deleted : -1

Record 2 deleted: 10 *-1|20

Record 5 deleted: 100 *10|50 *-1|20

Record 3 deleted: 30 *100|30 *10|50 *-1|20


Fixed-Length Record Deletion : Steps
1. Read and store the header value (RRN).
2. Go to the record to be delete (using RRN) and put deletion flag ‘*’
and write the header value.
3. Go to the header and write the deleted RRN
Fixed-Length Record Deletion : Code
Fixed-Length Record Addition : Steps
1. Read and store the header value.
2. If header = -1, then add record at the end of file, else go to step 3
3. Go to the deleted record (using RRN stored in header) and,
a. Read the deletion flag.
b. Read and store next deleted RRN.
4. Go back to the beginning of the record and write the new record.
5. Go to the header and write the next deleted RRN.
Fixed-Length Record Addition : Code
Fixed-Length Record Addition : Code
(Cont.)
Product Example (Try It!)
Assume we have the following five products
(id, name, price):
"001", "Book", "19.09"
"002", "Large Cup", "28.64"
"003", "Battery", "25.23" Trace the file content after applying each of the
following operations:
"004", "Photo Frame", "21.13"
1. Adding: product 1, product2, and product 3
"005", "Vase", "27.73“
2. Adding: product 4
3. Deleting: product 3, and product 2
4. Adding: product 5, product 3, and product 2
Product Example (Solution!)
Dynamic Replacement Strategies
First-Fit Best-Fit Worst-Fit
AVAIL LIST is NOT sorted by size. AVAIL LIST is sorted ascendingly by AVAIL LIST is sorted descendingly
size. by size.
First record large enough to Smallest record large enough to Largest record large enough to
hold new record is chosen hold new record is chosen hold new record is chosen

Suitable with internal Suitable with external


fragmentation fragmentation
Where will a 35 byte record be added?
First-Fit

Best-Fit

Worst-Fit
Where will a 25 byte record be added?
First-Fit

Best-Fit

Worst-Fit
Where will a 15 byte record be added?
First-Fit

Best-Fit

Worst-Fit
First-Fit with Variable Length Record
▪ Assume we have a file and the following operations has been
performed in the same order:
Record 2 was deleted: 10 *-1|20

Record 5 deleted: 100 *10|50 *-1|20

Record 3 deleted: 30 *100|30 *10|50 *-1|20

35 byte record added: 30 *10|30 *-1|20


First-Fit with Variable Length Record
▪ Assume we have a file and the following operations has been
performed in the same order: (Return unused space to Avail List)
Record 2 was deleted: 10 *-1|20

Record 5 deleted: 100 *10|50 *-1|20

Record 3 deleted: 30 *100|30 *10|50 *-1|20

35 byte record added: 30 *135|30 *10|15 *-1|20


Practice Challenge!
▪ If you are given a file with the following structure:
▪ 2 bytes → Record list header offset
▪ 3 bytes Records:
1 byte → Data character + 2 bytes → next record offset

▪ Can you read the sentence in this file whose characters are written in random order?
▪ Note: the next record of the last character in the sentence will be “-1”

You might also like