ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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 you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It looks like i would be the handle to a file, and NOFILE would be the number of files that are opened by the script. So I'm guessing that it's closing all files that have been opend by the script.
The daemon is shutting down any file descriptors that it has open. NOFILES is probably #defined to be the maximum number of simultaneous files/connections handled.
If you want to play at this level, buy a book on the C programming language.
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.
I don't define NOFILE in my program,so is it defined by system or compiler?
why executing close(i) exit my program?
what's more ,I have not opened any files before that codes.
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:
Code:
A common sequence of code in a daemon process is one that closes all open files.
Some programs have the code sequence
#include
for (i = 0; i < NOFILE; i++)
close(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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.