Home  >  Article  >  Operation and Maintenance  >  what is linux handle

what is linux handle

青灯夜游
青灯夜游Original
2022-02-25 13:39:455719browse

In Linux, a handle is an identifier, a reference identifier managed by the system. The kernel can use the handle to calculate the address of the file object in the kernel; as long as the developer obtains the handle of the object, it can The object performs arbitrary operations.

what is linux handle

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is a handle

A handle is an identifier. As long as we get the handle of the object, we can perform any operation on the object.

The handle is not a pointer. The operating system can use the handle to find a piece of memory. This handle may be an identifier, a map key, or a pointer. It depends on how the operating system handles it. fd can be regarded as replacing handles to some extent; Linux has corresponding mechanisms, but there is no unified handle type. Various types of system resources are identified by their respective types and operated by their respective interfaces.

At the operating system level, file operations also have a concept similar to FILE. In Linux, this is called a file descriptor (File Descriptor), and in Windows, it is called a handle (Handle) (not mentioned below) When ambiguous, they are collectively referred to as handles). The user opens the file through a certain function to obtain a handle, and thereafter the user manipulates the file through the handle.

Rough explanation

windows is handle, liunx is similar to fd, in the earliest windows development book, handle is translated as " handle". Although it doesn't sound good, I personally think it is quite expressive.

Although you are only holding the handle, you can pull the entire door, and you don’t have to care about what the door looks like

If a door has multiple handles, they can be pulled by different people. (Process) Hold it, it’s hard to say where the door will go.

The reason for designing such a handle is that the handle can prevent users from reading and writing file objects in the operating system kernel at will. Whether it is Linux or Windows, the file handle is always associated with the kernel's file object, but the details of the association are not visible to the user. The kernel can calculate the address of the file object in the kernel through the handle, but this ability is not open to users.

Handle in liunx

In the Linux system design, we follow the principle that everything is a file, that is, disk files, directories, network sockets Connectors, disks, pipes, etc., all of which are files, will return an fd when we open it, which is a file handle.

If you open files frequently or open a network socket and forget to release it, the handle will be leaked.

In the Linux system, there is a limit on the number of file handles that a process can call. By default, the maximum number of file handles that each process can call is 1024. If this limit is exceeded, the process will not be able to obtain new files. handle, resulting in the inability to open new files or network sockets, and the online server will be denied service.

The following is a practical example. In Linux, fd with values ​​​​0, 1, and 2 represent standard input, standard output, and standard error output respectively. The fd obtained by opening the file in the program starts to grow from 3.

What exactly is fd?

In the kernel, each process has a private "open file table". This table is an array of pointers, and each element points to a kernel. The open file object.

And fd is the subscript of this table. When the user opens a file, the kernel will internally generate an open file object, find an empty item in this table, let this item point to the generated open file object, and return the subscript of this item as fd.

Since this table is in the kernel and cannot be accessed by users, even if the user owns fd, he cannot get the address of the open file object and can only operate it through the functions provided by the system.

In C language, the channel for manipulating files is the FILE structure. It is not difficult to imagine that the FILE structure in C language must have a one-to-one relationship with fd. Each FILE structure will record its own unique corresponding fd. .

In programming, a handle is a special smart pointer. When an application wants to reference memory blocks or objects managed by other systems (such as databases and operating systems), handles are used.

The difference between a handle and an ordinary pointer is that the pointer contains the memory address of the referenced object, while the handle is a reference identifier managed by the system, which can be relocated to a memory address by the system. This indirect object access mode enhances the system's control over the referenced object.

In the memory management of operating systems (such as Mac OS and Windows) in the 1980s, handles were widely used. File descriptors in Unix systems are basically handles. Like other desktop environments, the Windows API makes extensive use of handles to identify objects in the system and establish communication channels between the operating system and user space. For example, a form on the desktop is identified by a handle of type HWND.

Today, increases in memory capacity and virtual memory algorithms have made simpler pointers more popular, while handles that point to another pointer have fallen out of favor. Despite this, many operating systems still refer to pointers to private objects and internal array indices that a process passes to the client as handles.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of what is linux handle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is linux kvmNext article:what is linux kvm