Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Life is too short to have a dog

Start with I/O

I/O, also known as input/output, refers to the transfer of data between any operation, program or device and the computer.

For a computer, I/O operations and computational processing are two major tasks throughout its life, and most of the time is spent performing I/O operations. The I/O operations mentioned here are actually divided into hardware and software components, namely THE I/O device and the I/O subsystem.

I/O devices

Today, there are more and more TYPES of I/O devices, which can be divided as follows in terms of function (or working mode) :

  • The storage device: Also called block device, this type of device stores information in fixed size blocks, each block has its own address, and each block can read and write independently of other blocks. Common storage devices include disks and tapes.
  • Transmission equipment: Also called network communication devices, these devices are mainly used to communicate with remote devices. Common transmission devices include network cards and modems.
  • Human-computer interaction equipment: Also called character device, this type of device transmits information in the unit of character during I/O transmission. The more common character devices are screen, keyboard and mouse. Because character devices are directly facing users and users can interact with computers through these devices, they are also called human-computer interaction devices.
  • Special equipment: All devices that can transmit I/O except the above three can be classified as special devices.

As you can see, there are many different types of I/O devices. In order to shield the differences and details of the underlying devices from the upper applications, and to standardize the hardware and software interfaces, the operating system kernel is designed to use device driver modules. Device drivers provide a unified device access interface for the I/O subsystem, just as system calls provide a unified standard interface between applications and the operating system. At the same time, it is important to note that the communication between the I/O device and the device driver (computer) is actually done through hardware interfaces (ports), and this process requires hardware and software cooperation to complete.

I/O subsystem

No matter how gorgeous the hardware is, if there is no corresponding software to provide control and management capabilities, the services that the hardware is supposed to provide will eventually not be used. As mentioned above, the control of the devices connected to the computer (I/O devices) is one of the main tasks of the operating system, and the collection of methods used to control the devices constitutes the I/O subsystem.

As mentioned above, with the continuous development of I/O devices, there are more and more TYPES of I/O devices. Without a standard and abstract I/O operation interface, it will be difficult for new types of I/O to access the operating system. In order to shield the differences and details of underlying hardware devices, the operating system uses device drivers to abstract and unify the access interfaces of devices. The specific implementation is implemented by the hardware manufacturers themselves. (As you can see, the idea of abstract programming arose early on.)

Having covered some of the basic CONCEPTS of I/O, let’s take a look at two concepts that are commonly encountered in daily development: file I/O and network I/O.

File I/O

The concept of file I/O that we will discuss is more at the software level, the operating system level. In the operating system, to facilitate users and developers to use data information (provided by peripherals), the operating system abstracts various attributes of storage devices, defines logical storage units (files), and maps the files to physical devices. It is important to note that the concept of files applies not only to storage devices but also to other I/O devices. The difference between the two is the file type and how the operating system operates on the two different types of files. This also validates the Linux adage “Everything is a file.” (Again, object-oriented design concepts come into play.) Here we will only discuss the concept of file I/O for storage devices.

As you can see from the figure above, if an application wants to initiate a file I/O request, it needs to complete the corresponding I/O operation through the file system of the operating system. As mentioned above, the operating system provides the user with the logical storage unit that can be manipulated by the file (in the UNIX environment, the concept of file descriptor is used). This operable file object contains the basic properties of the actual manipulated object, which is abstracted by the operating system as file properties. Information such as name, identifier, type, location, size, protection, user id, and time. For an abstract data type like a file, the file system provides system calls to create, write, degree, locate, delete, and truncate.

If we further restrict the concept of file I/O to a Linux environment and call it low-level disk I/O, any POSIX-compliant operating system can support file I/O. In addition, file I/O can access different types of files on the storage device. However, file I/O accesses resources directly through system calls rather than buffers.

Network I/O

The network communication we talk about everyday is essentially network I/O, through which we can communicate with remote devices (data exchange). Because network I/O is different from normal disk I/O in performance and access mode, the read and write methods for disk I/O cannot be applied to network I/O. Most operating systems abstract a special set of interfaces for network I/O — Socket interfaces for network I/O operations. Of course, in Linux, “everything is a file”. In Linux, Socket is also described by a file descriptor, but this file descriptor does not describe the local file, but the corresponding file of the remote device.

The unpredictable nature of network communication has led to many I/O models, which are essentially a way for clients (or service consumers) to handle network I/O requests. Because there is a lot of this kind of information on the Internet, I will not describe it here.

Four,

In the grand scheme of things, file I/O and network I/O are essentially access to resources, but one is a local device and the other is a remote device. But in terms of access and performance, file I/O and network I/O are very different in terms of BOTH I/O interfaces and system calls.