简体   繁体   English

如何找到最后打开的文件描述符?

[英]How to find the last opened file descriptor?

Consider I have a process called a.out , In that process, i have opened the 10 files using a open() function. 考虑一下我有一个名为a.out的进程,在该进程中,我已经使用open()函数打开了10个文件。

Consider that time I get the last descriptor is 13. So I need to know is there is any way to find the last opened descriptor in the process. 考虑到我得到最后一个描述符的时间是13。所以我需要知道有什么方法可以找到进程中最后一个打开的描述符。

For that I am using a getrlimit like this 为此,我正在使用这样的getrlimit

   if(getrlimit(RLIMIT_NOFILE,&r1)<0)
              perror("error");

But when I am priting this I get 4096 in my system. 但是,当我对此进行自豪时,系统中会显示4096。 SO Is there is any way to do this? 所以有没有办法做到这一点?

There is no (sensible) magic way to do this. 没有(明智的)魔术方法可以做到这一点。 You must remember it in a variable somewhere. 您必须在某个地方的变量中记住它。 Each time you open a file, update this variable. 每次打开文件时,请更新此变量。

( getrlimit with RLIMIT_NOFILE tells you how many files you can open, not how many you have opened). getrlimitRLIMIT_NOFILE告诉你,你可以有多少个文件打开,你没有多少开)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM