![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
||||
|
||||
|
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 |
||||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|