LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-04-2007, 01:13 AM   #1
walkinmud
LQ Newbie
 
Registered: Jun 2003
Posts: 16

Rep: Reputation: 0
Question How can I get available file descriptor number?


I can only get the max file descriptor number by command ulimit. But I need get the available number. Anyone can help me? Thank you very much advance.
 
Old 04-04-2007, 07:50 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Can you give us more details of the problem?

Do you want the number of file descriptors which are not used by a particular process? Is this process running a C program which you are writing? What?

Please give details.
 
Old 04-04-2007, 08:06 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Google search has quite a few article on how file descriptors work.

In /proc, each process has some file descriptor info in the directory for each specific process.
 
Old 04-04-2007, 08:11 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This gives some clues:

Open a file in some application
Do ps -e to get the PID
Do "ls -l /proc/<PID>/fd" This shows all the file descriptors associated with that process
 
Old 04-04-2007, 09:29 PM   #5
walkinmud
LQ Newbie
 
Registered: Jun 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by wjevans_7d1@yahoo.co
Can you give us more details of the problem?

Do you want the number of file descriptors which are not used by a particular process? Is this process running a C program which you are writing? What?

Please give details.
I'm going to write a program which will fork numbers of processes. Each process will use 2 file descriptors(redirecting STDOUT and STDERR to 2 pipes). Before forking them, I need to detect how many available file descriptors in system for the current user, so I can know how many processes I can fork.

Thank you for your reply.

Last edited by walkinmud; 04-04-2007 at 09:57 PM.
 
Old 04-04-2007, 10:03 PM   #6
walkinmud
LQ Newbie
 
Registered: Jun 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany
This gives some clues:

Open a file in some application
Do ps -e to get the PID
Do "ls -l /proc/<PID>/fd" This shows all the file descriptors associated with that process
Yes, this will give all file descriptors opened in system. But how can I know how many file descriptors a user can use, for example, how many fds root can use?
 
Old 04-05-2007, 07:59 AM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Your redirection of output will be done in the child processes, yes? In that case, the limit problem is not the one you think it is.

There is no maximum number of file descriptors per "user" (i.e., root), or even per process group. So that's not your limit.

The limit problem is the number of processes you can fork. This is not a bug, it's a feature. It's meant to eliminate the possibility that a buggy program can get stuck in an infinite loop containing a fork() spree.

To examine this more closely, try this at the shell prompt:

Code:
ulimit -a
ulimit -u
On my system, this shows the max user processes to be 1024. I don't know whether this is the maximum processes a given user may have (no matter how spread out among parent processes), or how many fork()s may be done, or how many forks() may be done, minus one for the parent process of those fork()s. Unless someone can jump in here with the answer, you get to experiment.

I'm thinking that if you're going to use anywhere near, say, 1024 processes, you may wish to try something other than figuring out how many processes you can have, taking a deep breath, and just doing 'em all. You may wish to check each process to see whether it starts successfully before starting the next one. This provides for more robust code. It will run a little slower, but the payoff is tremendous in isolating problems that can arise either in initial debugging or in sudden changes in availability of system resources.

You might want to consider a sort of handshake system, using file record locking or something, for each process to tell the parent that it has started ok and has succeeded in getting the resources it needs.

Hope this helps a little.
 
Old 04-05-2007, 09:51 PM   #8
walkinmud
LQ Newbie
 
Registered: Jun 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wjevans_7d1@yahoo.co
Your redirection of output will be done in the child processes, yes? In that case, the limit problem is not the one you think it is.

There is no maximum number of file descriptors per "user" (i.e., root), or even per process group. So that's not your limit.
I really appreciate your answer. I'm sorry I did not explain the whole things very clear. But from your answer, I can assume what's the problem in my code.

My situation is the parent process need to fork N child processes. Each children will redirect its STDOUT and STDERR to 2 pipes, while parent process will also create 2*N pipes to get the output of each child process. So from your reply, it seems the limitation is in my parent process, because each process will have no more than 1024 file descriptors. No matter how many child processes(less than 1024 by default) I forked, but the max pipe/fd number of parent process will be limited to 1024 by default. So in my program with a default ulimit config(fd number limited to 1024), actually I can only fork 511 child (because parent process has STDOUT and STDERR too. ), or maybe 510 (because parent process has STDIN.)? Is my understanding correct?

Thank again.
 
Old 04-06-2007, 11:55 AM   #9
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
It looks like that will be your limit, yes.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
File Descriptor 63 kshkid Programming 3 03-02-2006 11:41 AM
Bad File Descriptor tech_user Linux - Hardware 0 10-14-2005 03:21 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration