Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Disk I/O Operations and Data Transfer Mechanisms

Tech May 12 2

I/O Data Transfer Lifecycle

When an application initiates a file read request, it interacts through standard library functions such as read() or ReadFile(). This operation triggers a system call, serving as the transition mechanism from user mode to kernel mode. The kernel assumes control to handle the request, validating file descriptors and determining the data location on the storage device.

The operating system communicates with device drivers to initiate the retrieval of data blocks from the disk. The hardware controller transfers the retrieved data into a kernel buffer (often a PageCache), a reserved memory region managed by the kernel. Subsequently, the CPU executes a copy operation to move the data from the kernel buffer to the user space buffer. Once the transfer is complete, the system call returns, allowing the application to process the data in memory.

Kernel and User Space Interaction during read()

During a read() execution, the user process requests data from the kernel via a software interrupt or trap instruction, causing the CPU privilege level to switch from user mode to kernel mode. The kernel allocates a kernel buffer to house the incoming data. The CPU issues a command to the DMA (Direct Memory Access) controller. The DMA controller then transfers the data block from the disk drive to the kernel buffer independently of the CPU.

Once the transfer is complete, the DMA controller signals an interrupt. The CPU responds by copying the data from the kernel buffer to the application's user space buffer. The operation concludes as the system restores the execution context to user mode to resume the interrupted process.

write() to Network Device

For a write() operation involving network transmission, the process initiates a system call to send data to a network interface card (NIC). This requires a context switch from user mode to kernel mode. The kernel prepares a socket buffer. The CPU executes a memory copy from the user buffer to the socket buffer. The DMA controller is then instructed to transfer the data from the socket buffer to the NIC hardware. The DMA handles the hardware transfer asynchronously. Upon completion, the DMA signals that the write operation is finished, allowing the kernel to switch the CPU back to user mode to continue process execution.

Data Transfer Pseudo-Code Simulation

CPU-Driven Data Transfer

In Programmed I/O, the CPU is responsible for moving data between hardware and memory. This approach requires active polling or interrupt handling.

# Simulation: Transferring 512 bytes from an I/O port to RAM
# Data Path: Peripheral Device -> CPU Register -> Main Memory

IO_DATA_REG = 0x1F0       # Data register address
IO_STATUS_REG = 0x1F7     # Status register address
TARGET_RAM_ADDR = 0x1000  # Destination memory address
BUFFER_SIZE = 512         # Bytes to transfer
READY_FLAG = 0x08         # Bit mask for data ready status

def check_device_readiness():
    """Busy-wait loop checking the status register."""
    while True:
        status = read_port(IO_STATUS_REG)
        if (status & READY_FLAG) != 0:
            break

def perform_cpu_copy():
    """CPU manually moves each byte."""
    bytes_transferred = 0
    while bytes_transferred < BUFFER_SIZE:
        # Step A: Fetch data byte from device to CPU register
        data = read_port(IO_DATA_REG)
        
        # Step B: Store data byte from CPU register to RAM
        write_memory(TARGET_RAM_ADDR + bytes_transferred, data)
        
        bytes_transferred += 1

# Execution Flow
check_device_readiness()
perform_cpu_copy()
generate_interrupt(IRQ_DISK)

DMA-Driven Data Transfer

DMA offloads the transfer burden from the CPU to a dedicated hardware controller, allowing the CPU to execute other tasks during the I/O operation.

def execute_dma_transfer(src, dest, size):
    # Step 1: Program the DMA Controller registers
    configure_dma_engine(
        source=src,
        destination=dest,
        length=size
    )
    
    # Step 2: Activate the DMA channel
    start_dma_transfer()
    
    # Step 3: CPU can perform other work while DMA runs
    while not is_transfer_complete():
        run_background_tasks()

    # Step 4: Clear interrupt and finalize
    clear_dma_interrupt()

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.