LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-22-2004, 12:13 AM   #1
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Rep: Reputation: 15
wht do these codes mean?


for(int i=0;i< NOFILE;++i)
close(i);

I found these from a daemon example.
 
Old 12-22-2004, 12:36 AM   #2
RandomLinuxNewb
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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.
 
Old 12-22-2004, 12:38 AM   #3
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-23-2004, 07:17 AM   #4
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
after testing, I found:
NOFILE=256

and when running to these codes , program exit normally.
 
Old 12-23-2004, 07:57 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
We're assuming you understand the basic syntax/sematics of a for( ; ; ) loop... is that you're also asking about?
 
Old 12-23-2004, 08:05 AM   #6
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
of course not,

mainly, there are two points confusing me:
NOFILE refers to what?
close(i) operate on what ?
 
Old 12-23-2004, 09:38 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Quote:
NOFILE refers to what?
The number of files used in the program.

Quote:
close(i) operate on what ?
A file descriptor.

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.

Last edited by itsme86; 12-23-2004 at 09:40 AM.
 
Old 12-23-2004, 07:18 PM   #8
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
I am sorry for my illegibility statement.

please allow me to try again:

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.
 
Old 12-23-2004, 07:58 PM   #9
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,060

Rep: Reputation: 295Reputation: 295Reputation: 295
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?
 
Old 12-23-2004, 08:23 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 12-25-2004, 11:14 AM   #11
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
ok, that's all, thank all of you peoples very much!
I think i should read more books and study harder.
 
  


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
Are the hex codes for colors in a jpg the same codes as used in html? abefroman Linux - Security 3 07-31-2005 03:21 PM
how to start and wht r d benifits harmandeep kaur Linux - Newbie 3 07-01-2005 06:06 PM
wht is mean by cups? ss100 Linux - Software 3 01-19-2005 06:04 AM
wht u miss in linux? frosty_kgp General 16 09-16-2004 01:33 AM
Problems wht tv card and webcam elekt Mandriva 0 09-11-2004 02:50 PM


All times are GMT -5. The time now is 02:05 PM.

Main Menu
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