<p>When sockets are created or files are opened/created in C, is the file descriptor that's assigned to the socket/file guaranteed to be the lowest-valued descriptor available?  What does the C spec say about file descriptor assignment in this regard, if anything?</p>

[{'answer_id': 51630, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think you'll find it in the C spec, more likely the spec for your OS. My experience in Linux has been that it's always the lowest.</p>\n"}, {'answer_id': 51640, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': "<p>I'll counter this with another question - why does this matter? You shouldn't be comparing the file descriptor with anything (unless checking for stdin/stdout/stderr) or doing math with it. As long as it fits in an int (and its guaranteed to) that's all you really need to know.</p>\n"}, {'answer_id': 51643, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 0, 'selected': False, 'text': '<p>Steve M is right; C has no notion of sockets, and its file I/O functions use a [pointer to a] FILE object, not a descriptor.</p>\n'}, {'answer_id': 51671, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 4, 'selected': True, 'text': "<p>It's not guaranteed to be the lowest, and is implementation dependent <em>(1)</em>.  In general, however, the routine that assigns open file descriptors uses a method that gives you the first open on.  It could be that immediately after several lower ones free, leaving you with a higher descriptor than you might expect though.</p>\n\n<p>The only reason I can think of to know this, though, is for the select function, which is sped up if you pass it the highest file descriptor you need to check for.</p>\n\n<p><em>(1)</em> Note that those implementations that follow the IEEE standard do guarantee the lowest unused descriptor for files, but this may not apply to sockets.  Not every implementation follows the IEEE standard for open(), so if you're writing portable software it is best not to depend on it.</p>\n"}, {'answer_id': 51774, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>@aib the open(), close(), lseek(), read(), write() all make use of file descriptors.  I hardly ever use streams for I/O.</p>\n\n<p>@Kyle it matters because of statements like select().  Knowing the highest descriptor can improve performance.</p>\n'}, {'answer_id': 596671, 'author': 'mpez0', 'author_id': 27898, 'author_profile': 'https://Stackoverflow.com/users/27898', 'pm_score': 0, 'selected': False, 'text': '<p>The C spec says that it\'s implementation dependent.  If you\'re looking at a Unix implementation, the man page for open(2) says "The file descriptor returned by a successful call will be the lowest-numbered file  descriptor  not  currently open for the process."</p>\n\n<p>This helps if you\'re trying to attach a specific file to a specific descriptor.  Say you want to redirect stderr to /dev/null.  Something like</p>\n\n<p>close(2); open("/dev/null", O_WRONLY);</p>\n\n<p>ought to do it.  You should, of course, capture the fd returned by open and ensure that it\'s 2.</p>\n'}]
['https://Stackoverflow.com/questions/51627', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']