0

Consider I have a process called a.out , In that process, i have opened the 10 files using a open() function.

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.

For that I am using a getrlimit like this

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

But when I am priting this I get 4096 in my system. SO Is there is any way to do this?

Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25
  • What is meant by "Consider that time I get the last descriptor is 13"? – Rubix Rechvin Jan 21 '15 at 13:58
  • @RubixRechvin - After opening a 10 files, the last opened file got the file descriptor as 13. – Bhuvanesh Jan 21 '15 at 14:00
  • 2
    4096 means the maximum file descriptor number that can be opened by this process + 1. – Iharob Al Asimi Jan 21 '15 at 14:00
  • @iharob - so how to i know the last opened files descriptor?? – Bhuvanesh Jan 21 '15 at 14:01
  • 3
    Keep track of the information if you need it. – Iharob Al Asimi Jan 21 '15 at 14:01
  • I hate it when devs try to come up with cute solutions like this instead just properly tracking their file descriptors. The most straightforward solution is almost always the best. – Jonathon Reinhart Jan 21 '15 at 14:22
  • possible duplicate of [Getting the highest allocated file descriptor](http://stackoverflow.com/questions/899038/getting-the-highest-allocated-file-descriptor) – Kenster Jan 21 '15 at 14:40
  • What specifically do you want to know? The number of the most recently opened file? Or the largest descriptor number? What about files opened by library functions that your program calls? What do you need the value for? – Kenster Jan 21 '15 at 14:50
  • You may want to try reading /proc//fd directory in your code to see how many fds were opened. Not sure if that is what you are looking for. – vtha Jan 22 '15 at 08:52
  • @vtha this would work in a sense, but seems an unnecessarily complicated hack considering the simplicity of the task. Remembering an fd is as simple as storing it in a variable. – davmac Jan 23 '15 at 11:09

1 Answers1

1

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).

davmac
  • 20,150
  • 1
  • 40
  • 68