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 standard IO and file descriptor in linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what are the standard IO and file descriptors in linux. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. File descriptor

The file descriptor is an integer value

A file descriptor marks a specific file that is opened

The Linux system allocation file descriptor has certain rules:

Assign file descriptors from small to large

After the file is closed, the previously assigned file descriptor will be reclaimed by the system

The file descriptor reclaimed by the system is re-allocated with the unoccupied file descriptor according to the rule from small to large.

2. After the Linux system starts, the returned file descriptor starts with fd = 3. Why?

After the Linux system starts, the returned file descriptor starts with fd = 3

After the Linux system starts, three file descriptors fd = 0,1,2 are assigned to stdin, stdout and stderr

Fd = 0,1,2 three file descriptors can be programmatically closed to release (the current stdout is output to the command line), and then reopen a file, so that the standard input and output will be relocated to the file we opened. In this way, the standard output can be relocated.

3. Copy of file descriptor 1. Dup function copy file descriptor

-dup copies the file descriptor, and the new file descriptor is the minimum unused value automatically assigned by the Linux system

2. Dup2 copy file descriptor

When dup2 copies a file descriptor, the programmer can specify the numeric value of the copied new file descriptor. The following example specifies that the file descriptor is 6.

3. Fcntl copy file descriptor

-fcntl can also be used to copy file descriptors

-the function prototype of fcntl is int fcntl (int fd, int cmd,... / * arg * /)

The meaning of each parameter is:

Fd: old file descriptor

Cmd: command parameters, different command parameters perform different tasks

. / * arg * /: variable parameter, which is closely related to the cmd parameter. For example, when cmd is F_DUPFD, the fcntl function can specify a new file description after copying.

The size of the predictor is greater than or equal to the parameter arg

4. Standard IO in Linux

Before the operation file has FileIO, why is there a standard IO here?

Standard IO is also some standard library functions for manipulating files.

The standard IO is encapsulated on the basis of a series of Linux kernel API of FileIO.

Standard IO provides buffering of file reading and writing in the application layer when encapsulating, which can cooperate with the buffering of Linux kernel in reading and writing files, thus improving the efficiency of file operation.

Standard IO library functions shield the differences between different operating systems, so they are used in almost exactly the same way on different operating systems (Linux, Windows)

What common library functions are included in the standard IO and how to use them?

The commonly used standard IO library functions are: fopen, fclose, fwrite, fread, fflush, fseek and so on.

The usage of the commonly used standard IO library functions is basically corresponding to and similar to that of FileIO.

Give an example to illustrate the usage

On the linux standard IO and file descriptors what is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 221

*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

Internet Technology

Wechat

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

12
Report