The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 06:05 PM
File Descriptors + cron matrixmadhan UNIX for Advanced & Expert Users 7 05-23-2007 01:53 PM
Sockets and File descriptors gstlouis High Level Programming 3 12-12-2005 07:36 AM
file descriptors a25khan UNIX for Dummies Questions & Answers 3 01-27-2004 07:46 PM
File Descriptors shibz UNIX for Advanced & Expert Users 3 12-18-2002 10:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-24-2001
s_chordia s_chordia is offline
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
  #2 (permalink)  
Old 12-24-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #3 (permalink)  
Old 12-24-2001
rwb1959's Avatar
rwb1959 rwb1959 is offline
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0