🎓 BookMCQ
← Back to 18. The Linux System

📝 Character devices in The Linux System (8 MCQs)

Stream Oriented Device Interfaces in Linux

Linux character devices provide byte-oriented or stream-like access to hardware and kernel services. Terminals, serial ports, and some input devices use character device interfaces. Their drivers register operations through the kernel character-device framework, allowing applications to interact through device files and common system calls such as open, read, write, and ioctl.

Why Stream Devices Differ from Block Storage

Many devices produce or consume data sequentially rather than exposing addressable storage blocks. A terminal delivers input as it arrives, while a serial interface sends and receives byte streams. Applying block scheduling and random-access assumptions to these devices would not accurately represent their behaviour.

Major and Minor Numbers and File Operations

A character device is identified through major and minor device numbers. The major number identifies the driver or device class association, while the minor number distinguishes devices handled by it. A registered cdev connects device numbers to file operations that implement operations such as reading, writing, and control requests.

Example of Reading from a Serial Device

A program opens /dev/ttyS0 and requests 64 bytes. The serial driver receives data from the device and makes available bytes to the requesting process. The read may return fewer bytes if that is all currently available, or block until data arrives depending on the device settings and file mode.

Device Responsiveness and Access Control

Character interfaces support terminals, input devices, serial communication, and specialised hardware. Their stream semantics make them suitable for event-driven data but may involve buffering and blocking behaviour. Device-node permissions control which users can access them, while incorrect drivers or unsafe operations can disrupt the device or kernel.

Misconceptions About Character Devices

'A character device transfers exactly one character per operation' is false because reads and writes can use buffers. 'Character devices cannot block' ignores normal blocking read behaviour. 'Every character device provides random seeking' is incorrect because many represent sequential streams without meaningful seek positions.

Exam Focus: Device Numbers and Character Operations

Explain the roles of major and minor numbers and how cdev registration connects a device to file operations. Compare character and block devices by access model and examples. Questions may examine a read from a terminal, partial transfers, blocking behaviour, or device-node permissions.

Key Terms of Linux Character Devices

  • Character device - Device with stream-oriented access.
  • Major number - Identifies a driver association.
  • Minor number - Identifies a device instance.
  • cdev - Kernel character-device registration structure.
  • ioctl - Interface for device-specific control operations.
2
Easy
4
Medium
2
Hard

📝 All Character devices in The Linux System MCQs

Q1. What defines a character device in Linux?

A.It stores data in blocks
B.It provides a stream of bytes ✅
C.It caches data in the page cache
D.It represents a network socket
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A character device is defined by its ability to deliver a continuous stream of bytes to user space, unlike block devices that handle fixed-size blocks. Option B captures this definition. Option A describes block devices, Option C incorrectly suggests caching, and Option D refers to sockets, which are a different abstraction. Understanding this core definition helps when choosing the right interface for simple I/O tasks.

Q2. Why do character devices typically bypass the page cache?

A.Because they require immediate, unbuffered access ✅
B.Because they are block devices
C.Because they rely on ioctl
D.Because they are memory‑mapped
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: Character devices are intended for low‑latency, unbuffered I/O, so the kernel reads or writes data directly without involving the page cache. Option A states this reason. Option B is false because block devices use the cache. Option C mixes up control operations with data flow, and Option D describes a different mechanism (mmap) that is optional. Recognizing this behavior explains why drivers often implement direct read/write paths.

Q3. How does a vm_area_struct relate to mapping a character device?

A.A character device can be mapped, creating a vm_area_struct ✅
B.Character devices cannot be mapped
C.vm_area_struct only applies to block devices
D.Mapping a character device bypasses vm_area_struct
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: When a driver supports mmap, the kernel creates a vm_area_struct to represent the user‑space region that maps the device. Option A correctly links the two concepts. Option B is wrong because many character drivers do allow mmap. Option C limits vm_area_struct to block devices, which is inaccurate. Option D suggests mapping avoids vm_area_struct, contradicting the kernel’s design. This connection is crucial for implementing zero‑copy I/O.

Q4. A driver writes sensor data to a character device and must return data to a non‑blocking reader instantly. Which mechanism should be used?

A.Use copy_to_user with blocking flag
B.Register the device with poll/epoll ✅
C.Store data in the page cache
D.Flush the buffer after each write
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Non‑blocking readers rely on readiness notification, which poll/epoll provides. Option B is the correct choice because it informs the process when data is available without blocking. Option A defeats the non‑blocking requirement. Option C would introduce unnecessary caching and latency. Option D adds overhead without solving the readiness problem. Using poll/epoll aligns with typical character‑device designs for asynchronous data delivery.

Q5. How does the Linux virtual memory system treat reads from character devices compared with reads from regular files?

A.Both use the page cache
B.Character device reads bypass the page cache ✅
C.Both use swap
D.Character devices go through the network stack
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Reads from character devices are performed directly, bypassing the page cache, because they often represent streaming or hardware data. Option B states this behavior. Option A is false since regular files rely heavily on the cache. Option C misattributes swapping, which is unrelated to the read path. Option D incorrectly suggests network processing. Knowing this difference helps developers avoid unnecessary caching when designing high‑throughput drivers.

Q6. A character device supports mmap. What must the driver provide to allow user space to map the device memory?

A.Provide a vm_operations_struct with a .fault handler ✅
B.Implement an open method
C.Define an ioctl command
D.Implement a read method
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: To enable mmap, the driver must supply a vm_operations_struct that includes a .fault callback for handling page faults. Option A correctly describes this requirement. Option B is unrelated to memory mapping. Option C deals with control operations, not memory mapping. Option D handles data transfer but not the mapping mechanism. Supplying the proper vm_operations ensures the kernel can correctly associate user‑space pages with the device’s physical resources.

Q7. A developer needs a character device that streams sensor data to multiple concurrent readers without losing data. Which synchronization plus I/O model is most appropriate?

A.Spinlock with select
B.Mutex with poll
C.Semaphore with epoll ✅
D.RCU with async I/O
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Using a semaphore to control access combined with epoll for readiness notification lets many readers wait efficiently and be awakened when new data arrives. Option C captures this optimal pattern. Spinlocks (Option A) are unsuitable for sleeping I/O. Mutexes (Option B) can block but lack the event‑driven wake‑up that epoll provides. RCU (Option D) is designed for read‑mostly data structures, not for coordinating streaming writes. This combination balances safety and performance for high‑rate sensor streams.

Q8. In a system where a character device is accessed via both read() and mmap(), a bug causes the same memory page to be cached twice, leading to stale data. Which kernel change most effectively prevents this duplication?

A.Disable page cache for the device
B.Set vm_flags to VM_IO in .mmap ✅
C.Force read to use copy_from_user
D.Add a .readv implementation
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Marking the mapping with VM_IO tells the kernel that the pages are I/O memory and should not be entered into the page cache, preventing duplicate caching. Option B is the correct fix. Option A would overly restrict caching and hurt performance. Option C changes the read path but does not affect mmap duplication. Option D adds a vectored read interface without addressing the caching issue. Properly flagging the mapping resolves the stale‑page problem.

🔗 Related Topics (MCQs)