Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of linux handle

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the concept of linux handle". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the concept of linux handle"?

In linux, the handle is an identifier, which is a reference ID managed by the system. The kernel can calculate the address of the file object in the kernel through the handle; as long as the developer gets the handle of the object, he can operate on the object arbitrarily.

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

What is a handle?

A handle is an identifier, and as long as we get a handle to the object, we can do anything with the object.

The handle is not a pointer, the operating system can find a piece of memory with the handle, this handle may be an identifier, the key of map, or it may be a pointer, depending on how the operating system handles it. Fd can be regarded as a substitute for handles to some extent; Linux has corresponding mechanisms, but there is no unified handle type, and various types of system resources are identified by their own types and operated by their respective interfaces.

At the operating system level, file manipulation also has a concept similar to FILE. In Linux, this is called a file descriptor (File Descriptor), while in Windows, it is called a Handle (hereinafter referred to as a handle when there is no ambiguity). The user opens the file through a function to get the handle, and then the user manipulates the file through the handle.

A rough explanation

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

Although you only hold the handle, you can pull the whole door, and you don't care what the door looks like.

If a door has multiple handles and is held by different people (processes), it is hard to say where the door is going.

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

Handle in liunx

In linux system design, we follow the principle that everything is a file, that is, disk files, directories, network sockets, disks, pipes, etc., all these are files, and when we open them, we will return a fd, that is, a file handle.

If you open the file frequently, or open the network socket and forget to release it, there will be handle leakage.

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 handles that can be called by each process is 1024. If this limit is exceeded, the process will not be able to obtain new handles, and the online server will be denied service due to the inability to open new files or network sockets.

Here's a practical example. In Linux, fd with values of 0, 1, and 2 represents 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", which is an array of pointers, and each element points to a kernel's open file object.

And fd is the subscript of this table. When a user opens a file, the kernel generates an open file object internally, finds an empty entry in the table, points to the generated open file object, and returns the subscript of the entry as fd.

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

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

In programming, a handle is a special intelligent pointer. Handles are used when an application wants to reference blocks of memory or objects managed by other systems (such as databases, operating systems).

A handle differs from a normal pointer in that the pointer contains the memory address of the reference object, while the handle is a reference identity managed by the system, which can be relocated to a memory address by the system. This mode of indirect access to objects strengthens the system's control over reference objects.

Handles are widely used in memory management in operating systems such as Mac OS and Windows in the 1980s. The file descriptor of the Unix system is basically also a handle. Like other desktop environments, Windows API makes extensive use of handles to identify objects in the system and to 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, increased memory capacity and virtual memory algorithms make simpler pointers more popular, while handles that point to another pointer are indifferent. However, many operating systems still refer to pointers to private objects and the internal array of processes passed to the client as handles.

At this point, I believe you have a deeper understanding of "what is the concept of linux handle". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report