🎓 BookMCQ
← Back to 11. File System Interface

📝 Sequential file access Method in Operating System (9 MCQs)

Processing File Contents in Stored Order

Sequential file access is a file-access method in which information is processed in a predetermined order, usually from the beginning toward the end. Applications read or write successive bytes or records using the current file position. The operating system maintains the associated position information and performs storage operations through the file system. This method is suitable for text processing, log files, streaming, and backup operations.

Why Ordered Access Simplifies File Processing

Many applications naturally process information from beginning to end rather than repeatedly jumping between unrelated locations. Sequential access provides a simple interface with efficient buffering and read-ahead opportunities. It also works well with magnetic tape and sequential data streams. Its limitation is that retrieving an arbitrary record may require processing or skipping all preceding data.

Current Position and Successive Read Operations

When a file is opened, the operating system establishes an initial file position. Each successful read retrieves data beginning at that position and advances it by the number of bytes read. Write operations similarly update contents and usually advance the position. End-of-file conditions indicate that no further file data is available through the current read.

Reading Five Records from a Sequential File

Suppose a file contains 100 fixed-length records, each occupying 80 bytes. Reading record 1 retrieves bytes 0 through 79, while reading record 2 retrieves bytes 80 through 159. The file position advances after each complete record, allowing an application to process the records consecutively without calculating unrelated storage locations.

Efficient Streaming and Random-Access Limitations

Sequential access supports efficient buffering, prefetching, and large transfers, particularly when the underlying storage can retrieve adjacent data efficiently. However, accessing record 90 directly may require traversing earlier records unless a separate index or positioning mechanism is available. Concurrent modifications and variable-length records can also complicate sequential processing and position management.

Common Errors About Sequential Access

'Sequential access means records cannot be skipped' is incorrect because an application can read or reposition past unwanted information. 'Every sequential file contains fixed-length records' is incorrect because records may have variable lengths or consist of bytes. 'Sequential access always requires magnetic tape' is incorrect because disks and SSDs also support sequential file operations.

Exam Focus: File Position and Record Traversal

Trace the file position after successive read and write operations. Calculate byte offsets for fixed-length records and explain how variable-length records affect positioning. Compare sequential access with direct access and identify workloads suited to each method. Questions may also examine end-of-file handling, buffering, and the cost of reaching a distant record.

Key Terms of Sequential File Access

  • Sequential access - Processes data in order.
  • File pointer - Tracks current file position.
  • Record - Group of related information.
  • Read-ahead - Loads upcoming file data.
  • End-of-file - Marks the end of available data.
  • Buffering - Temporarily holds transferred data.
3
Easy
3
Medium
3
Hard

📝 All Sequential file access Method in Operating System MCQs

Q1. A program wants to lock the first half of a file exclusively using a FileChannel. If the file length is retrieved by raf.length(), which call correctly acquires that lock?

A.ch.lock(0, raf.length()/2, false) ✅
B.ch.lock(0, raf.length(), false)
C.ch.lock(0, raf.length()/2, true)
D.ch.lock(raf.length()/2, raf.length(), false)
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The call in option A locks from byte 0 for a size equal to half the file length and uses false to indicate an exclusive lock, matching the requirement. Option B locks the entire file, which is unnecessary and would block other operations. Option C creates a shared lock, not exclusive, so other processes could still read the region. Option D starts at the midpoint and locks beyond, missing the first half entirely. Therefore A is the correct choice, ensuring only the first half is locked exclusively.

Q2. Process A obtains an exclusive lock on bytes 0–500 of a file. Process B then attempts to acquire a shared lock on bytes 400–800. What will happen?

A.Both locks succeed because they are on different parts ✅
B.Process B's lock is granted but reads only unlocked region
C.Process B is blocked until Process A releases its lock
D.Process A's lock is automatically downgraded to shared
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Because the two lock ranges overlap (400–500), the operating system cannot grant a shared lock while an exclusive lock is held on any overlapping bytes. Thus Process B must wait until Process A releases its exclusive lock. Option A is wrong because the ranges are not disjoint. Option B misstates the behavior; shared locks cannot be granted over locked bytes. Option D is incorrect because exclusive locks are not automatically converted to shared without explicit action.

Q3. A file is 2000 bytes long. Which FileChannel.lock call will correctly lock the second half (bytes 1001‑2000) as a shared lock?

A.ch.lock(1000, 1000, true)
B.ch.lock(1001, 999, true) ✅
C.ch.lock(1000, 2000, true)
D.ch.lock(1001, 1000, true)
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The second half starts at length/2+1=1001\text{length}/2 + 1 = 1001 and extends to the end, giving a size of 2000−1001=9992000 - 1001 = 999 bytes. Option B uses those exact parameters and sets the third argument to true for a shared lock, satisfying the requirement. Option A starts one byte too early and locks an extra byte beyond the file size. Option C uses an oversized length that exceeds the file bounds. Option D specifies a size of 1000 bytes, which would attempt to lock beyond the file’s end. Hence B is correct.

Q4. What is the main distinction between mandatory and advisory file locking?

A.Mandatory locks are enforced by the OS, advisory locks rely on cooperation
B.Advisory locks are faster than mandatory locks ✅
C.Mandatory locks allow shared access, advisory locks do not
D.Advisory locks work only on Windows
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Mandatory locking means the operating system prevents any other process from accessing the locked region, regardless of the program’s intent. Advisory locking, by contrast, depends on all processes voluntarily checking for locks before accessing the file. Option B is a performance claim not inherent to the definitions. Option C reverses the access rights: mandatory locks can be exclusive or shared, and advisory locks do not inherently restrict sharing. Option D incorrectly limits advisory locks to a specific OS. Therefore A correctly captures the core difference.

Q5. Why should a program release its exclusive lock before acquiring a shared lock on the same file region?

A.To avoid deadlock caused by overlapping lock types
B.Shared locks automatically upgrade exclusive locks ✅
C.Exclusive locks cannot coexist with any other locks
D.Releasing is unnecessary; the OS handles it
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Holding an exclusive lock and then trying to obtain a shared lock on the same range creates a conflict: the exclusive lock already blocks any other lock, including shared ones, leading to a deadlock if the same thread waits on itself. Releasing the exclusive lock first clears the conflict, allowing the shared lock to be granted. Option B is false because Java does not automatically upgrade locks. Option C is partly true but does not explain the need to release first. Option D is wrong; the OS does not automatically release locks on behalf of the program. Thus A is the most accurate explanation.

Q6. In a system that implements advisory locking, what happens when a process opens a file that another process has exclusively locked without explicitly requesting the lock?

A.The OS blocks the open operation ✅
B.The file is opened normally, but the process must check for locks itself
C.The file is opened in read‑only mode
D.The exclusive lock is ignored
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Advisory locks rely on cooperative behavior: the operating system does not enforce the lock, so the file can still be opened. However, well‑behaved programs are expected to query the lock status and respect it, meaning the second process must manually check for the lock and decide whether to proceed. Option A describes mandatory locking behavior. Option C is not a standard outcome of advisory locks. Option D is incorrect because the lock remains in effect; it is simply not enforced by the OS. Hence B correctly reflects advisory lock semantics.

Q7. Which constant in the provided Java code represents a shared lock mode?

A.EXCLUSIVE
B.SHARED ✅
C.READ
D.WRITE
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The code defines two boolean constants: EXCLUSIVE is set to false and SHARED is set to true. The SHARED constant is used when calling ch.lock for a shared lock, indicating that other processes may also acquire shared access. EXCLUSIVE represents an exclusive lock, while READ and WRITE are not defined in the snippet. Therefore, SHARED is the correct answer.

Q8. You need to prevent any other thread from reading or writing any part of a file while your thread updates the first half. Which lock strategy ensures this?

A.Acquire an exclusive lock on the first half only ✅
B.Acquire a shared lock on the whole file
C.Acquire an exclusive lock on the whole file
D.Acquire a shared lock on the first half and exclusive on the second
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Locking the entire file exclusively guarantees that no other thread can obtain any lock—shared or exclusive—on any portion of the file, thus protecting both the first half being updated and the rest of the file. Option A leaves the second half unlocked, allowing other threads to access it. Option B only provides shared access, which does not block writes. Option D splits the locks, but the shared lock on the first half would still permit other readers, violating the requirement. Hence C is the safest approach.

Q9. If an exception occurs after acquiring an exclusive lock and before releasing it, why is a finally block that calls release() essential?

A.It guarantees the lock is released even if the method returns early
B.It prevents the lock from being reacquired automatically ✅
C.It logs the lock status for debugging
D.It converts a mandatory lock into an advisory lock
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The finally block executes regardless of whether the try block completes normally or an exception is thrown, ensuring that the release() method runs and the lock is freed. Without this safeguard, the lock could remain held, causing deadlocks or resource starvation for other processes. Option B is incorrect because locks are not automatically reacquired. Option C, while useful, is not the primary purpose of finally. Option D misstates the effect of release(). Therefore A accurately explains the necessity of the finally block.

🔗 Related Topics (MCQs)