I/O Device Classification and Characteristics

There are many kinds of I/O device, and they behave very differently from one another. Managing them well starts with putting them into sensible groups. This lesson walks through the ways devices are classified, the characteristics that distinguish them, and the management strategies that follow.

How Devices Are Classified

1. By the Mode of Information Exchange

Block Devices

  • Characteristic: data is transferred in blocks
  • Typical devices: hard disks, floppy disks, optical disks, USB flash drives
  • Properties:
    • Support random access
    • Move large amounts of data
    • Have a caching mechanism
    • Support DMA transfers

A block device is addressed by the block number rather than by position, so the operating system can jump straight to the data it needs instead of reading everything in between.

// The basic structure of a block device
typedef struct {
    int block_size;          // block size
    int total_blocks;        // total number of blocks
    int current_position;    // current position
    void (*read_block)(int block_num, void *buffer);
    void (*write_block)(int block_num, void *buffer);
} block_device_t;

Character Devices

  • Characteristic: data is transferred one character at a time
  • Typical devices: keyboards, mice, serial ports, printers
  • Properties:
    • Primarily accessed sequentially
    • Move small amounts of data
    • Place high demands on responsiveness
    • Usually do not support caching

A character device behaves like a stream: bytes arrive in order, and there is no useful notion of “block number” to jump to.

// The basic structure of a character device
typedef struct {
    int baud_rate;           // baud rate
    int data_bits;           // data bits
    int stop_bits;           // stop bits
    int parity;              // parity bit
    void (*read_char)(char *ch);
    void (*write_char)(char ch);
} char_device_t;

2. By Transfer Speed

High-Speed Devices

  • Speed range: > 1MB/s
  • Typical devices: hard disks, SSDs, network devices
  • Management characteristics:
    • Need efficient buffer management
    • Support concurrent access
    • Use DMA transfers

Medium-Speed Devices

  • Speed range: 1KB/s - 1MB/s
  • Typical devices: optical disks, tapes, some network devices
  • Management characteristics:
    • Need a moderate amount of buffering
    • Support interrupt-driven operation
    • Balance the load between the CPU and the I/O path

Low-Speed Devices

  • Speed range: < 1KB/s
  • Typical devices: keyboards, mice, printers
  • Management characteristics:
    • Need simple buffer management
    • Are driven mainly by interrupts
    • Keep the CPU fairly involved

3. By Function

Dedicated Devices

  • Characteristic: usable by one process at a time
  • Typical devices: printers, plotters
  • Management strategies:
    • A device allocation mechanism
    • A queueing mechanism for waiting
    • A device release mechanism

Because the device cannot serve anyone else while it is busy, the operating system has to own the queue and decide who gets it next.

// The structure used to manage a dedicated device
typedef struct {
    int device_id;
    int current_owner;       // PID of the current owner
    int queue_head;          // head of the waiting queue
    int queue_tail;          // tail of the waiting queue
    int queue[MAX_WAIT];     // the waiting queue itself
    void (*allocate)(int pid);
    void (*release)(void);
} dedicated_device_t;

Shared Devices

  • Characteristic: usable by several processes at the same time
  • Typical devices: hard disks, network devices
  • Management strategies:
    • A concurrency control mechanism
    • Protection of data consistency
    • Management of access permissions

Here the hard part is not handing the device out but keeping concurrent users from interfering with one another.

// The structure used to manage a shared device
typedef struct {
    int device_id;
    int user_count;          // current number of users
    int max_users;           // maximum number of users
    semaphore_t access_sem;  // access semaphore
    void (*request_access)(int pid);
    void (*release_access)(int pid);
} shared_device_t;

Analyzing Device Characteristics

1. Transfer Characteristics

Direction of Transfer

  • Unidirectional: input only, or output only
  • Bidirectional: both input and output
  • Full duplex: input and output at the same time

Mode of Transfer

  • Synchronous: the sender and the receiver share a clock
  • Asynchronous: start bits and stop bits are used to stay in step
  • Isochronous: a fixed transfer rate is guaranteed

2. Control Characteristics

Control Complexity

How much the operating system has to know in order to drive a device varies enormously, from a single LED to a hard disk with its own queue of commands.

// A classification of devices by control complexity
typedef enum {
    DEVICE_SIMPLE,           // simple device (e.g. an LED)
    DEVICE_STANDARD,         // standard device (e.g. a keyboard)
    DEVICE_COMPLEX           // complex device (e.g. a hard disk)
} device_complexity_t;

Control Methods

  • Programmed control: the CPU controls the device directly
  • Interrupt control: the device notifies the CPU through an interrupt
  • DMA control: the device accesses memory directly

3. Reliability Characteristics

Error-Handling Capability

A device that can detect and describe its own failures saves the operating system a great deal of guesswork, and a device that can retry safely can hide most transient faults from the application entirely.

// The structure used for device error handling
typedef struct {
    int error_code;          // error code
    int retry_count;         // number of retries
    int timeout_value;       // timeout value
    void (*error_handler)(int error_code);
    void (*recovery_procedure)(void);
} device_error_handling_t;

Failure-Recovery Mechanisms

  • Automatic recovery: the device recovers from a fault on its own
  • Manual recovery: a human has to intervene to restore service
  • Replacement recovery: the device has to be replaced before service returns

Device Management Strategies

1. Device Allocation Strategies

Static Allocation

  • The device stays assigned to the process for as long as that process runs
  • Advantage: simple, with no contention to resolve
  • Disadvantage: the device is used inefficiently

Dynamic Allocation

  • The device is assigned to a process only when it is actually needed
  • Advantage: the device is used efficiently
  • Disadvantage: complex synchronization mechanisms are needed

2. Device Scheduling Strategies

First-Come, First-Served (FCFS)

The simplest fair rule: the request that has been waiting longest is served first, whatever its size.

// FCFS device scheduling
typedef struct {
    int queue[MAX_QUEUE];    // the waiting queue
    int head, tail;          // head and tail pointers of the queue
    void (*enqueue)(int pid);
    int (*dequeue)(void);
} fcfs_scheduler_t;

Priority Scheduling

  • The device is allocated according to the priority of the process
  • High-priority processes get to use the device first

Shortest Seek Time First (SSTF)

  • The request that can be served in the least time is served first
  • Suits the scheduling of block devices

3. Device Buffering Strategies

Single Buffering

  • There is only one buffer
  • Simple, but of limited efficiency

Double Buffering

With two buffers, one can be filled while the other is being consumed, so the device and the CPU no longer have to wait for each other.

// The structure of a double buffer
typedef struct {
    void *buffer1;           // buffer 1
    void *buffer2;           // buffer 2
    int current_buffer;      // the buffer currently in use
    void (*switch_buffer)(void);
} double_buffer_t;

Multiple Buffering

  • Several buffers are used in rotation
  • Improves concurrency and efficiency

Device Drivers

1. The Structure of a Driver

Every device driver presents the same kind of interface to the operating system, so that the layers above it never have to know which physical device is on the other end.

// The interface of a device driver
typedef struct {
    char driver_name[64];    // name of the driver
    int device_type;         // device type
    void (*init)(void);      // initialization function
    void (*open)(void);      // open the device
    void (*close)(void);     // close the device
    int (*read)(void *buffer, int size);
    int (*write)(void *buffer, int size);
    void (*ioctl)(int cmd, void *arg);
} device_driver_t;

2. What a Driver Does

  • Device initialization: sets up the parameters of the device
  • Data transfer: carries out read and write operations
  • Error handling: deals with errors raised by the device
  • State management: keeps track of the state of the device

Glossary

TermAbbreviationExplanation
Block Device-A device that transfers data in blocks
Character Device-A device that transfers data one character at a time
High-Speed Device-A device with a high transfer speed
Medium-Speed Device-A device with a moderate transfer speed
Low-Speed Device-A device with a low transfer speed
Dedicated Device-A device that only one process may use
Shared Device-A device that several processes may use at once
Virtual Device-A device simulated through SPOOLing technology
Storage Device-A device that stores data
Input Device-A device that feeds data into the computer
Output Device-A device that carries data out of the computer
Human-Computer Interaction Device-A device through which a person works with the computer
Terminal-A device through which a user interacts with the computer
Keyboard-A character input device
Mouse-A pointing input device
Display-A graphics output device
Printer-A paper output device
Disk-A block storage device
Tape-A sequential storage device
Optical Disk-An optical storage device
Solid State DriveSSDA flash storage device
Network Device-A device used for network communication
Network Interface CardNICThe network interface card
Device Characteristic-The technical characteristics of a device

Found an issue with this content?

Tell us what could be improved.