日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转]文件IO详解(二)---文件描述符(fd)和inode号的关系

發布時間:2025/7/25 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]文件IO详解(二)---文件描述符(fd)和inode号的关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:https://www.cnblogs.com/frank-yxs/p/5925563.html

文件IO詳解(二)---文件描述符(fd)和inode號的關系

----------------------------------------------------------------- 1、文件描述符和inode號碼是不同的兩個東西。 2、對于每個進程,系統會建立一個進程控制塊(PCB)來保存相關的信息,而這個PCB在內核中的表現其實就是一個稱為task_struct的結構體,這個結構體的成員用來保存與此進程有關的相關信息,其中有個成員是struct file_struct ?*files,它是用來找到此進程所有打開的文件列表的,files變量指向的是struct file_struct類型的結構體,這個結構體中有一個成員是一個指針數組struct file *fd_array[],這個數組中的每個元素會指向一個struct file的結構體,這個結構體是用來描述一個打開的文件的,有很多的成員,其中有個成員是指向struct file_operation的指針變量,而struct file_operation結構體中包括了很多的函數指針,也就是open,write,realease,read等等對文件進行操作的函數,具體這些函數的實現就是驅動開發需要做的事情。如下圖所示。 3、在對文件操作前,必須要先打開文件,打開文件過程其實就是讓程序能通過某個路徑能夠找到此文件,關閉文件就是將此路徑斷開,找不到文件。 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

13.13 Duplicating Descriptors

You can?duplicate?a file descriptor, or allocate another file descriptor that refers to the same open file as the original. Duplicate descriptors share one file position and one set of file status flags (see?File Status Flags), but each has its own set of file descriptor flags (see?Descriptor Flags).

The major use of duplicating a file descriptor is to implement?redirection?of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.

You can perform this operation using the?fcntl?function with the?F_DUPFD?command, but there are also convenient functions?dup?and?dup2?for duplicating descriptors.

The?fcntl?function and flags are declared in?fcntl.h, while prototypes for?dup?and?dup2?are in the header file?unistd.h.

Function:?int?dup?(int?old)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See?POSIX Safety Concepts.

This function copies descriptor?old?to the first available descriptor number (the first number not currently open). It is equivalent to?fcntl (old, F_DUPFD, 0).

Function:?int?dup2?(int?old, int?new)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See?POSIX Safety Concepts.

This function copies the descriptor?old?to descriptor number?new.

If?old?is an invalid descriptor, then?dup2?does nothing; it does not close?new. Otherwise, the new duplicate of?old?replaces any previous meaning of descriptor?new, as if?new?were closed first.

If?old?and?new?are different numbers, and?old?is a valid descriptor number, then?dup2?is equivalent to:

close (new); fcntl (old, F_DUPFD, new)

However,?dup2?does this atomically; there is no instant in the middle of calling?dup2?at which?new?is closed and not yet a duplicate of?old.

Macro:?int?F_DUPFD

This macro is used as the?command?argument to?fcntl, to copy the file descriptor given as the first argument.

The form of the call in this case is:

fcntl (old, F_DUPFD, next-filedes)

The?next-filedes?argument is of type?int?and specifies that the file descriptor returned should be the next available one greater than or equal to this value.

The return value from?fcntl?with this command is normally the value of the new file descriptor. A return value of?-1?indicates an error. The following?errno?error conditions are defined for this command:

EBADF

The?old?argument is invalid.

EINVAL

The?next-filedes?argument is invalid.

EMFILE

There are no more file descriptors available—your program is already using the maximum. In BSD and GNU, the maximum is controlled by a resource limit that can be changed; see?Limits on Resources, for more information about the?RLIMIT_NOFILE?limit.

ENFILE?is not a possible error code for?dup2?because?dup2?does not create a new opening of a file; duplicate descriptors do not count toward the limit which?ENFILE?indicates.?EMFILE?is possible because it refers to the limit on distinct descriptor numbers in use in one process.

Here is an example showing how to use?dup2?to do redirection. Typically, redirection of the standard streams (like?stdin) is done by a shell or shell-like program before calling one of the?exec?functions (see?Executing a File) to execute a new program in a child process. When the new program is executed, it creates and initializes the standard streams to point to the corresponding file descriptors, before its?main?function is invoked.

So, to redirect standard input to a file, the shell could do something like:

pid = fork (); if (pid == 0){char *filename;char *program;int file;…file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));dup2 (file, STDIN_FILENO);TEMP_FAILURE_RETRY (close (file));execv (program, NULL);}

There is also a more detailed example showing how to implement redirection in the context of a pipeline of processes in?Launching Jobs.

轉載于:https://www.cnblogs.com/oxspirt/p/8734100.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的[转]文件IO详解(二)---文件描述符(fd)和inode号的关系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。