Chapter 4 System File
Chapter 4 System File
1. What is a File?
• A file is a named collection of related data stored on
secondary storage (e.g., disk, tape, or optical storage).
• A file can be a sequence of bits, bytes, lines, or records.
• Its meaning depends on how it is created and used.
2. File Structure
• A file must have a structure the operating system (OS) understands.
• Different types of file structures include:
• Text File: Contains characters organized into lines.
• Source File: Contains programming instructions.
• Object File: Contains machine-readable code.
• Some OSs like MS-DOS and UNIX support only a few file structures.
3. Types of Files
Operating systems classify files into different types:
(a) Ordinary Files
• Contain user data such as text, databases, or executable programs.
• Users can perform operations like add, modify, delete, and remove.
(b) Directory Files
• Contain lists of filenames and related information.
• Help in organizing files into folders.
(c) Special Files (Device Files)
• Represent physical devices like disks, terminals, printers, and networks.
• Two types:
• Character Special Files: Handle data character by character (e.g., keyboards,
printers).
• Block Special Files: Handle data in blocks (e.g., disks, tapes).
4. File Access Techniques
File access techniques determine how data is retrieved from a file. The three main
techniques are:
(a) Sequential Access
• Data is read in order, one record after another.
• Used in compilers and log files.
Example of Sequential Access
Scenario 1: Reading a Log File
A web server stores log files that record every visitor's activity. The system reads
this file line by line, from start to end.
Example:
[10:00] User A logged in
[10:05] User B visited the homepage
[10:10] User A logged out
he log file is read sequentially, meaning records must be accessed one after
another.
Scenario 2: Processing a Payroll System
A company has an employee payroll file stored in sequential order. When
processing salaries, the system reads each employee's record one by one.
Example File Content (Stored Sequentially)
1001, John, $5000
1002, Mary, $4500
1003, Ahmed, $5500
• The system reads the first record, processes it, then moves to the next.
• If you want to find Ahmed’s salary, you must read John’s and Mary’s
records first.
Key Points:
✅ Easy to implement
✅ Good for simple data processing
❌ Slow for searching specific records (you must read all previous records)
(b) Direct (Random) Access
• Each record has a unique address, allowing immediate access.
• Suitable for databases where records are accessed individually.
Example of Direct (Random) Access
Scenario 1: Accessing a Student Record in a Database
A university stores student records in a database. Each student has a unique
student ID. If a professor wants to retrieve a specific student's record, the system
directly accesses that record using its ID without scanning other records.
Example Table:
If the system needs Ahmed’s record (ID: 1003), it jumps directly to it instead of
reading John’s and Mary’s records first.
Scenario 2: Retrieving an Item from a Hard Disk
A hard disk drive (HDD) or solid-state drive (SSD) allows direct access to data
blocks.
• If a file is stored at block #500, the OS can directly retrieve it without reading
blocks 1-499.
• This is why SSDs are much faster than sequential storage devices like magnetic
tapes.
Scenario 3: ATM Transactions
• When you insert an ATM card, the system retrieves your account details
immediately, not by searching through all customers.
• This is possible because each account is stored with a unique account number.
Key Points:
✅ Fast and efficient for large databases
✅ Allows quick searching and updating
❌ More complex to implement
(c) Indexed Sequential Access
• A combination of sequential and direct access.
• An index is created, allowing direct access through pointers.
• Used in large databases.
Example of Indexed Sequential Access
Indexed Sequential Access combines sequential and direct access by using an index that helps in locating records
quickly. This method is commonly used in large databases where records need to be accessed efficiently.
Scenario 1: Library Book Catalog
A university library has thousands of books stored in a database.
Instead of searching through all records sequentially, an index is created.
The system first searches the index, which contains pointers to the actual book records.
Example Index Table (Sorted by Book ID)
Key Points:
Key Points:
✅ Simple and fast read performance (no extra searching needed).
✅ Supports random access (directly jump to any part of the file).
❌ External fragmentation (unused spaces between files).
❌ File resizing is difficult (if no continuous space is available).
(b) Linked Allocation
• Each file is stored as interconnected blocks scattered across the disk.
• Each block contains a pointer to the next block.
• Advantages:
• No external fragmentation.
• Allows file growth.
• Disadvantages:
• Slow for direct access.
• A broken pointer can corrupt the file.
• he first block (5) contains Page 1 and a pointer to the next block (12).
• Block 12 contains Page 2 and a pointer to the next block (8).
• Block 8 contains Page 3 and marks the end of the file.
When you open the document, the OS follows the pointers to read each page in order.
Advantages of Linked Allocation:
✅ No External Fragmentation – Any available block can be used.
✅ File Can Grow Easily – Just add a new block and update the pointer.
Disadvantages of Linked Allocation:
❌ Slow for Direct Access – To read Page 3, you must start from Page 1 and follow the pointers.
❌ Pointer Corruption – If the pointer in Block 12 is lost or damaged, the rest of the file is
unreadable.
(c) Indexed Allocation
• Uses an index block to store pointers to file blocks.
• Advantages:
• Supports direct access.
• A bad data block affects only that block.
• Disadvantages:
• Overhead of index blocks.
• A corrupted index block loses the entire file.
Example of Indexed Allocation
Scenario: Storing a Book in a Computer
Imagine you are saving a digital book (PDF file) on your computer. Instead of storing the file in
one continuous block (contiguous allocation) or using pointers inside each block (linked
allocation), the OS creates a separate index block that keeps track of where each part of the file
is stored.
How It Works (Indexed Allocation Example)
📖 Book File: "Computer Science.pdf"
The OS creates an index block for this file.
The index block stores all the locations of the book’s pages (data blocks).
• When you open the book, the OS looks at the index block first and then jumps to the correct data
block.
• If you want Page 3, the OS directly jumps to Block 42, without scanning other pages.
Advantages of Indexed Allocation:
✅ Supports Direct Access – You can jump directly to any part of the file.
✅ A Bad Data Block Affects Only That Block – If Block 42 is corrupted, only Page
3 is lost.
Disadvantages of Indexed Allocation:
❌ Extra Overhead – The index block takes up additional space.
❌ A Corrupted Index Block Loses the Entire File – If the index block is damaged,
the OS cannot find any pages of the book.
Real-Life Example of Indexed Allocation:
📁 File Allocation Table (FAT) – Used in older Windows file systems.
📀 DVDs & Blu-ray Discs – Use index tables to locate files instantly.
📚 Table of Contents in Books – Similar to how an index block works.
6. Operating System Security
Security in operating systems protects system resources from unauthorized access
and threats.
(a) Authentication Methods
• Username & Password: Traditional method.
• User Card/Key: Uses a physical card or key generator.
• Biometric Authentication: Uses fingerprints, retina scans, etc.
(b) One-Time Passwords (OTP)
• A new password is generated every time the user logs in.
• Types:
• Random Numbers (printed cards with numbers).
• Secret Key (generated by a hardware device).
• Network Password (sent via email/SMS).
(c) Program Threats
Malicious programs can harm the system:
• Trojan Horse: Fake programs that steal credentials.
• Trap Door: A hidden vulnerability in software.
• Logic Bomb: Activates when certain conditions are met.
• Virus: Spreads by modifying files, causing system damage.
(d) System Threats
Threats that affect system resources and networks:
• Worms: Replicate themselves and slow down systems.
• Port Scanning: Used by hackers to find weaknesses.
• Denial of Service (DoS): Overloads a system to prevent legitimate
use.
######################END##########################