ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If that's not what you're looking for then you're really going to need to be a little more specific with your questions. It's hard to answer a question "What does this code mean?" Say right off what's confusing you about it and what you'd like to know.
Perhaps NOFILE is defines in a header you're including. It's not, to my knowledge, a standard ANSI C or POSIX macro. There is a MAX_OPEN_FILES or something similar defined in POSIX, but I don't think there's a NOFILE. In any case, if the program compiles, it's being defined somewhere, so check the header files included in the program.
The close system call does not terminate your program. That is done by returning from main, calling exit or (sometimes) _exit. What makes you think the close call is causing your program to terminate?
From 'Advanced Programming in the Unix Environment' on page 42:
A common sequence of code in a daemon process is one that closes all open files.
Some programs have the code sequence
for (i = 0; i < NOFILE; i++)
assuming the constant NOFILE was defined in the <sys/param.h> header.
Other programs use the constant _NFILE that some versions of <stdio.h>
provide as the upper limit.
For your own programs, use the POSIX.1 value OPEN_MAX to determine the maximum open files value portably.
This value may be indeterminate at compile time. Using the function call sysconf(_SC_OPEN_MAX) can also determine this value. The value is referred to as 'run-time invariant'. This means that it won't change during the lifetime of the process.