📝 Other file access methods in Operating System (10 MCQs)
Indexed and Alternative File Retrieval Techniques
Other file-access methods extend sequential and direct access by introducing additional structures for locating information. Indexed access uses an auxiliary structure containing keys and references to record positions, while indexed-sequential access combines ordered records with an index. Hashed access applies a hash function to a key to locate a record. These methods support efficient retrieval for different application workloads.
Why Large Files Need More Flexible Lookup Methods
Sequential scanning can become expensive for large collections, while direct access requires knowledge of the target position. Applications dealing with variable-length records or key-based retrieval need additional lookup structures. Indexes and hash tables reduce the amount of data examined for selected queries, although they introduce storage overhead and maintenance work when records change.
Index Traversal and Hash-Based Record Location
In indexed access, the application searches an index using a key and obtains the corresponding record location. A multilevel index may use structures such as B-trees to support efficient range and exact searches. In hashed access, the key is transformed into a bucket number, where matching entries are located and checked to resolve collisions.
Finding an Employee Record by Identification Number
Suppose an employee file contains 100000 records and an index maps employee ID 45021 to byte offset 820000. The application searches the index, obtains the stored offset, and retrieves the record directly. Without an index, it may need to examine numerous records sequentially, especially when the records are not ordered by employee identifier.
Fast Searching and Index Maintenance Costs
Indexed and hashed access can improve lookup speed and reduce unnecessary file scanning. However, indexes consume additional storage and must be updated when records are inserted, deleted, or relocated. Hash collisions require suitable resolution methods, while tree indexes require structural maintenance. The appropriate technique depends on query patterns, ordering requirements, and whether range searches are needed.
Distinguishing Indexed and Hashed Access
'A hash index automatically supports ordered range searches' is incorrect because hashing does not preserve key order. 'An index contains every complete file record' is incorrect because it normally stores search keys and references. 'Indexed access eliminates all storage operations' is incorrect because the located record must still be retrieved from its storage location.
Exam Focus: Indexes, Hashing and Record Lookup
Compare indexed, indexed-sequential, and hashed access according to search operations and maintenance requirements. Trace how a key identifies a record location through an index. Explain hash collisions and identify when tree structures support range searches. Questions may also calculate the benefit of avoiding a sequential scan across a large record collection.
Key Terms of Alternative File Access
- Index - Maps search keys to records.
- Hashing - Converts keys into bucket locations.
- Collision - Occurs when keys share a bucket.
- B-tree - Balanced search-tree structure.
- Indexed-sequential - Combines ordered records and indexes.
- Record key - Identifies a record.
📝 All Other file access methods in Operating System MCQs
Q1. A Java program needs to ensure that two different threads can write to the first half of a file while multiple threads read the second half concurrently. Which lock strategy using FileChannel.lock best satisfies this requirement?
📖 Explanation: The exclusive lock guarantees that only one thread can modify the first half, preventing race conditions. The shared lock on the second half allows many threads to read simultaneously, increasing throughput. Option B swaps the lock types, which would block readers on the first half. Option C needlessly restricts access to the whole file. Option D provides no protection, so concurrent writes could corrupt data. In real applications, this pattern enables safe writes while keeping read‑only sections available to many users.
Q2. Suppose a logging service writes to log.txt and a monitoring tool reads the same file. The service acquires an exclusive lock on the first 100 bytes, then releases it. The tool later attempts to acquire a shared lock on the entire file. Which outcome is most likely?
📖 Explanation: When the exclusive lock is released, the operating system no longer blocks other processes, so the advisory shared lock can be granted. Option A is wrong because exclusive locks are not permanent; they only block while held. Option C is incorrect because a shared lock does not upgrade automatically. Option D suggests an error that does not occur under normal advisory locking. In practice, releasing a lock frees the region, allowing monitoring tools to read the file safely.
Q3. A developer wants to lock the second half of a file only if the file size is an even number. Which code snippet correctly implements this using FileChannel.lock?
📖 Explanation: The snippet in option C checks the low‑order bit with to confirm the size is even, then locks the second half using as both position and length, and sets the shared flag to true. Option A uses the modulus operator correctly but the condition is the same; however, option A’s comment does not clarify the shared flag, making it ambiguous. Option B locks the whole file exclusively, which is not the requirement. Option D locks the first half, not the second. In real code, precise condition checking and correct parameters ensure only the intended region is protected.
Q4. If a process acquires an exclusive lock on the first 200 bytes of a file, what will happen when another process attempts to acquire a shared lock on bytes 150‑250?
📖 Explanation: An exclusive lock prevents any other lock—shared or exclusive—from being granted on overlapping bytes. Since bytes 150‑200 overlap, the request must be denied. Option A is wrong because the lock cannot be split; the overlapping portion blocks the whole request. Option B assumes the exclusive lock is released automatically, which is not true. Option C suggests merging locks, which the OS does not do. In practice, overlapping exclusive and shared requests lead to denial, protecting the first region from concurrent reads.
Q5. A file is 1,000 bytes long. The code `ch.lock(0, 500, false)` is executed. Which portion of the file is locked and what type of lock is it?
📖 Explanation: The call locks from position 0 for a length of 500 bytes, and the boolean false indicates an exclusive lock, meaning no other process can acquire any lock on that region. Option B incorrectly labels the lock as shared. Options C and D refer to the second half of the file, which is not affected by the given parameters. Understanding these arguments lets developers protect critical sections of a file while allowing other parts to remain accessible.
Q6. What is the main difference between mandatory and advisory file locking?
📖 Explanation: Mandatory locking is imposed by the OS; any attempt to access a region under an exclusive mandatory lock is blocked regardless of the program's behavior. Advisory locking, by contrast, requires cooperating processes to query and respect the lock before accessing the file. Option A reverses the definitions, making it incorrect. Option C confuses lock semantics with concurrency limits. Option D claims a performance difference that is not defined by the locking mode. In real systems, choosing between mandatory and advisory determines whether the OS alone protects the file or whether all programs must cooperate.
Q7. Why might a program choose a shared lock for the second half of a file after modifying the first half?
📖 Explanation: A shared lock permits multiple readers while preventing writers, which is ideal for a region that is not being modified. After changing the first half exclusively, the program can safely let other threads or processes read the second half simultaneously, improving performance. Option A would block all access, defeating the purpose of a shared lock. Option B suggests an implicit whole‑file lock, which does not happen. Option D misstates the effect; the lock does not upgrade automatically. In practice, this pattern balances safety for modified data with accessibility for stable data.
Q8. If a process holds an exclusive lock on a file region and then attempts to acquire a shared lock on the same region without releasing the exclusive lock first, what is the likely result?
📖 Explanation: The operating system will reject the second request because the region is already locked exclusively; the lock state does not change, and the process continues to hold the exclusive lock only. Option A is wrong because the shared lock cannot be added while the exclusive lock is active. Option B misinterprets the effect—no new lock is granted. Option C suggests a deadlock, which does not happen; the call simply fails. Real‑world code must release an exclusive lock before requesting a shared lock to avoid such failures.
Q9. Which Java class provides a getChannel() method that returns a FileChannel suitable for locking?
📖 Explanation: RandomAccessFile’s getChannel() method returns a java.nio.channels.FileChannel, which supports locking operations such as lock() and tryLock(). While FileInputStream and FileOutputStream also expose getChannel(), their channels are primarily for streaming I/O and may not support full locking semantics on all platforms. BufferedReader does not have a getChannel() method at all. Therefore, RandomAccessFile is the most reliable choice for acquiring file locks in Java.
Q10. What constant value is used in the LockingExample code to indicate a shared lock?
📖 Explanation: In the example, the boolean constant SHARED is set to true, and this value is passed to FileChannel.lock to request a shared (read‑only) lock. Option A (false) represents an exclusive lock. Options C and D refer to nonexistent constants; the lock type is determined solely by the boolean argument. Recognizing that true signals a shared lock helps developers correctly specify the desired lock mode when using the lock API.