The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with exec command and file descriptors?? rfourn Shell Programming and Scripting 1 07-18-2007 03:05 PM
File Descriptors + cron matrixmadhan UNIX for Advanced & Expert Users 7 05-23-2007 10:53 AM
Sockets and File descriptors gstlouis High Level Programming 3 12-12-2005 04:36 AM
file descriptors a25khan UNIX for Dummies Questions & Answers 3 01-27-2004 04:46 PM
File Descriptors shibz UNIX for Advanced & Expert Users 3 12-18-2002 07:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 12-24-2001
Registered User
 

Join Date: Dec 2001
Posts: 2
Question File Descriptors

Hi,
I have written a daemon process, to perform certain operations in the background.
For this I have to close, the open file descriptors,

Does anybody know how to find out the number of open file descriptors ?


Thanks in Advance,
Sheetal
Reply With Quote
Forum Sponsor
  #2  
Old 12-24-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
I don't know any portable way for a process to find out the number of files it has open.

But consider this case...I open 16 files and allocate fd 0 through fd 15. Then I close fd 0 through fd 14. This leaves only fd 15 open. If you could magically find out that only one file is open, how would that help you? You still don't know which fd to close.

The shell will allocate fd 0, 1, and 2 and pass these to you. Your program should keep track of which files it opens.

If you have lost track of want files are open, the only thing I can suggest to do a getrlimit() to obtain the highest possible fd that could ever be allocated. Then close all possible fd's. The close call will fail if the file is not open, so you just ignore that error.
Reply With Quote
  #3  
Old 12-24-2001
rwb1959's Avatar
Registered User
 

Join Date: Aug 2001
Location: Virginia, USA
Posts: 438
In general when you write a daemon process you...

Become a session leader - fork()
Become a process-group leader - setsid()
Dissociate from controlling terminal - setsid() normally does this too.
chdir to '/' - chdir()
Set file creation mask to 0 - umask(0)

You still need to close unneeded descriptors. Assuming you
haven't opened anything, you can simply do...

for (i = 0; i < 10; i++)
close(i);

As Perderabo says, "The close call will fail if the file is not open,
so you just ignore that error."

At this point, you have initalized your daemon process.
A great reference book with examples of this is...
Advanced Programming in the UNIX Environment
by W. Richard Stevens
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0