LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-08-2012, 06:32 PM   #1
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
Closing file descriptors before fork


I need to close all file descriptors before forking, and I can't rely on FD_CLOEXEC as there may (will) be libraries I'm using that have opened some descriptors as well as me.

I've done a bit of Googling, and there doesn't seem to be a standard way, other than looping to a really high number and calling close. I'm in a highly threaded environment so can only make system calls (no library functions)

My intention is to call setrlimit at the start of the program, and setting the hard limit to something like 50. That way, after the fork I only have to loop to 50, and I can guarantee closing all the descriptors.

My question is whether there are any issues with this I'm not considering, or any reason why it won't work?
 
Old 12-08-2012, 08:19 PM   #2
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
This is just my doubt. But, if you do this, won't it again close the files opened by libraries?
 
Old 12-09-2012, 03:21 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by sarin View Post
This is just my doubt. But, if you do this, won't it again close the files opened by libraries?
Yes, but that's the point. I'm doing this before an exec and I need to close a couple of sockets and a sqlite database.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading and writing to pipes, file descriptors, and file streams cmartin0 Programming 1 02-13-2012 04:03 AM
Fork behavior while copying file descriptors greplinux Linux - Newbie 2 06-22-2010 11:34 PM
Closing file descriptors nikin Linux - Newbie 2 06-05-2008 05:16 AM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 08:02 AM


All times are GMT -5. The time now is 05:47 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration