简体   繁体   English

是否有系统调用或某种方式来了解 Linux 中文件描述符的类型(例如常规文件 fd、套接字 fd、信号 fd、计时器 fd)?

[英]Is there a system call or some way to know the type of file descriptor in Linux (e.g. regular file fd, socket fd, signal fd, timer fd)?

As I keep discovering, there are a variety of File Descriptors - Almost every thing is abstracted around a file descriptor: regular files, sockets, signals and timers (for example).正如我不断发现的那样,有各种各样的文件描述符 - 几乎所有东西都围绕文件描述符抽象:常规文件、套接字、信号和计时器(例如)。 All file descriptors are merely integers.所有文件描述符都只是整数。

Given a file descriptor, is it possible to know what type it is?给定一个文件描述符,是否有可能知道它是什么类型? For example, it would be nice to have a system call such as getFdType(fd).例如,有一个像 getFdType(fd) 这样的系统调用会很好。

If an epoll_wait is awakened due to multiple file descriptors getting ready, the processing of each file descriptor will be based upon its type.如果由于多个文件描述符准备就绪而唤醒 epoll_wait,则每个文件描述符的处理将基于其类型。 That is the reason I need the type.这就是我需要类型的原因。

Of course, I can maintain this info separately myself but it would be more convenient to have the system support it.当然,我可以自己单独维护这些信息,但让系统支持它会更方便。

Also, are all file descriptors, irrespective of the type, sequential.此外,无论类型如何,所有文件描述符都是连续的。 I mean if you open a regular data file, then create a timer file descriptor, then a signal file descriptor, are they all guaranteed to be numbered sequentially?我的意思是,如果您打开一个常规数据文件,然后创建一个计时器文件描述符,然后创建一个信号文件描述符,它们是否都保证按顺序编号?

As "that other guy" mentioned, the most obvious such call is fstat .正如“那个其他人”所提到的,最明显的此类调用是fstat The st_mode member contains bits to distinguish between regular files, devices, sockets, pipes, etc. st_mode成员包含用于区分常规文件、设备、套接字、管道等的位。

But in practice, you will almost certainly need to keep track yourself of which fd is which.但在实践中,您几乎肯定需要自己跟踪哪个 fd 是哪个。 Knowing it's a regular file doesn't help too much when you have several different regular files open.当您打开多个不同的常规文件时,知道它是一个常规文件并没有太大帮助。 So since you have to maintain this information somewhere in your code anyway, then referring back to that record would seem to be the most robust way to go.因此,既然您无论如何都必须在代码中的某处维护此信息,那么回溯该记录似乎是最可靠的方法。

(It's also going to be much faster to check some variables within your program than to make one or several additional system calls.) (检查程序中的某些变量也比进行一个或几个额外的系统调用要快得多。)

Also, are all file descriptors, irrespective of the type, sequential.此外,无论类型如何,所有文件描述符都是连续的。 I mean if you open a regular data file, then create a timer file descriptor, then a signal file descriptor, are they all guaranteed to be numbered sequentially?我的意思是,如果您打开一个常规数据文件,然后创建一个计时器文件描述符,然后创建一个信号文件描述符,它们是否都保证按顺序编号?

Not really.并不真地。

As far as I know, calls that create a new fd will always return the lowest-numbered available fd.据我所知,创建新 fd 的调用将始终返回编号最低的可用 fd。 There are old programs that rely on this behavior;有一些旧程序依赖于这种行为; before dup2 existed, I believe the accepted way to move standard input to a new file was was close(0); open("myfile", ...);dup2存在之前,我相信将标准输入移动到新文件的公认方法是close(0); open("myfile", ...); close(0); open("myfile", ...); . .

However, it's hard to really be sure what fds are available.然而,很难真正确定哪些 fds 可用。 For example, the user may have run your program as /usr/bin/prog 5>/some/file/somewhere and then it will appear that fd 5 gets skipped, because /some/file/somewhere is already open on fd 5. As such, if you open a bunch of files in succession, you cannot really be sure that you will get sequential fds, unless you have just closed all those fds yourself and are sure that all lower-numbered fds are already in use.例如,用户可能以/usr/bin/prog 5>/some/file/somewhere运行你的程序,然后看起来 fd 5 被跳过了,因为/some/file/somewhere已经在 fd 5 上打开了。因此,如果您连续打开一堆文件,则无法真正确定您将获得连续的 fd,除非您自己刚刚关闭了所有这些 fd 并且确定所有编号较低的 fd 都已在使用中。 And doing that seems much more of a hassle (and a source of potential problems) than just keeping track of the fds in the first place.这样做似乎比首先跟踪 fds 更麻烦(也是潜在问题的根源)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当fd是常规文件时,linux系统调用read(fd,buf,count)返回的结果是否小于count? - Does the linux system call read(fd, buf, count) return less than count when fd is a regular file? 来自文件 fd 的文件系统 fd - filesystem fd from file fd 从套接字返回的文件描述符大于FD_SETSIZE - File descriptor returned from socket is larger than FD_SETSIZE 如何更改与stdin文件描述符关联的fd? - How to change the fd associated with the stdin file descriptor? select()/ FD _ *()导致文件描述符错误 - select()/FD_*() cause Bad file descriptor 系统将日志 fd 刷新写入 mq socket fd - the log fd was flush to write to mq socket fd by system 尝试通过拦截文件系统调用在Linux中使用readlink从fd获取文件名,但是它不起作用 - Trying to get file name from fd using readlink in linux by intercepting file system call, but it is not working 通过文件描述符(fd)而不是文件名来比较/比较两个文件 - Diff/compare two files by file descriptor (fd) instead of file name 使用 open("/dev/fd/n", mode) 重新打开现有的文件描述符 - Reopen an existing file descriptor with open("/dev/fd/n", mode) FD_ISSET()是否返回接收到数据的文件描述符 - Does FD_ISSET() return the file descriptor which received data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM