what is linux handle

Feb 25, 2022 pm 01:39 PM
linux handle

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to learn Linux basics? How to learn Linux basics? Apr 10, 2025 am 09:32 AM

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

The 5 Pillars of Linux: Understanding Their Roles The 5 Pillars of Linux: Understanding Their Roles Apr 11, 2025 am 12:07 AM

The five pillars of the Linux system are: 1. Kernel, 2. System library, 3. Shell, 4. File system, 5. System tools. The kernel manages hardware resources and provides basic services; the system library provides precompiled functions for applications; the shell is the interface for users to interact with the system; the file system organizes and stores data; and system tools are used for system management and maintenance.

Where is the Redis restart service Where is the Redis restart service Apr 10, 2025 pm 02:36 PM

How to restart the Redis service in different operating systems: Linux/macOS: Use the systemctl command (systemctl restart redis-server) or the service command (service redis-server restart). Windows: Use the services.msc tool (enter "services.msc" in the Run dialog box and press Enter) and right-click the "Redis" service and select "Restart".

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

What is Linux actually good for? What is Linux actually good for? Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

See all articles