Device Files in Linux

 In Linux/Unix systems, device files (also called special files) are interfaces to hardware devices and are found in the /dev directory. These files do not store data, but act as access points to device drivers in the kernel.


✅ Types of Device Files in Linux

There are two main types of device files:

Device Type

Description

Major Examples

Character Device

Transfers data one character at a time (unbuffered, byte-stream).

Keyboard, mouse, serial ports

Block Device

Transfers data in blocks (buffered, random access).

Hard drives, SSDs, USBs


🔹 1. Character Devices (c)

  • Represented with a c in file permissions.

  • Data is transferred byte-by-byte (like a stream).

  • Used for devices where direct sequential access is expected.

📌 Examples:

/dev/tty        # Terminal

/dev/console    # System console

/dev/null       # Discards input

/dev/random     # Random number generator


📌 List them:

ls -l /dev | grep '^c'



🔹 2. Block Devices (b)

  • Represented with a b in file permissions.

  • Data is transferred in blocks (e.g., 512 bytes, 4 KB).

  • Support random access, suitable for filesystems.

📌 Examples:

/dev/sda      # First SATA hard disk

/dev/sdb      # Second SATA disk

/dev/loop0    # Loop device (mounting ISO)


📌 List them:

ls -l /dev | grep '^b'



🔹 Major and Minor Numbers

Every device file has:

  • Major number: identifies the driver associated with the device.

  • Minor number: identifies a specific device (instance) the driver controls.

📌 View using:

ls -l /dev/sda

# brw-rw---- 1 root disk 8, 0 Jun 23 10:00 /dev/sda

# ↳ 8 = major, 0 = minor



🔹 3. Other Special Device Types

Type

Description

Command Symbol

Named Pipe (FIFO)

Used for inter-process communication.

p

Socket

Used for communication between processes (like network/Unix domain sockets).

s

Symbolic Link

A link pointing to another file.

l

Regular File

Standard file.

-

Directory

Folder.

d

📌 List all:

ls -l /dev | grep -E '^[cbpsld-]'



🔸 FAANG-Level 20 Questions and Answers

📌 Conceptual & System-Level

  1. Q: What’s the difference between block and character devices?
    A: Block devices allow random access in fixed-size blocks (like disks). Character devices are sequential and unbuffered (like serial ports).

  2. Q: What do major and minor numbers represent?
    A: Major = driver; Minor = device instance.

  3. Q: How can you list all block devices in the system?
    A: Use lsblk, ls -l /dev | grep '^b', or cat /proc/partitions.

  4. Q: What happens if you delete a device file (e.g., /dev/null)?
    A: The system won't work correctly. You can recreate using mknod.

  5. Q: How to recreate /dev/null?
    A: sudo mknod -m 666 /dev/null c 1 3

  6. Q: Which type of device file is /dev/sda?
    A: Block device.

  7. Q: Can block devices be used without mounting?
    A: Yes, using tools like dd, but mounting is needed for filesystem access.

  8. Q: What command shows all device drivers and their major numbers?
    A: cat /proc/devices

  9. Q: How do loop devices work?
    A: They map a file as a block device, used to mount disk images.

  10. Q: What is the difference between /dev/random and /dev/urandom?
    A: /dev/random is blocking (more secure); /dev/urandom is non-blocking (faster, uses PRNG).


📌 Advanced and Use-Case Based

  1. Q: How can a program interact with a device file?
    A: Via system calls like open(), read(), write() just like regular files.

  2. Q: Can a device have both character and block interfaces?
    A: Yes, some devices expose both (e.g., loop devices).

  3. Q: What is /dev/pts?
    A: Pseudo terminal slaves used in terminal emulators (like SSH).

  4. Q: How does udev relate to device files?
    A: udev dynamically manages /dev entries based on hardware events.

  5. Q: What’s /dev/shm?
    A: Temporary shared memory filesystem, often used for IPC.

  6. Q: How do sockets in /dev differ from regular files?
    A: They facilitate communication (e.g., /dev/log for system logs).

  7. Q: What are /dev/fb0, /dev/input/mice?
    A: Framebuffer and input devices for graphical and peripheral input.

  8. Q: Why does ls -l /dev/sda show b at the beginning?
    A: It is a block device.

  9. Q: How do I find the device file for a USB stick?
    A: Use dmesg | tail, lsblk, or udevadm info.

  10. Q: How does Linux know what driver to use for a device file?
    A: Based on the major number, which maps to a kernel driver.


✅ Summary Table

Type

Symbol

Example

Use Case

Character

c

/dev/tty, /dev/random

Terminals, serial ports

Block

b

/dev/sda, /dev/loop0

Hard drives, images

Named Pipe

p

/tmp/mypipe

IPC

Socket

s

/dev/log

Inter-process communication

Directory

d

/dev

File system tree

Symlink

l

/dev/cdrom → /dev/sr0

Redirection

Regular File

-

config.txt

Default data files




Distributed by Gooyaabi Templates | Designed by OddThemes