file descriptors | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

file descriptors

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-26-2004
a25khan's Avatar
a25khan a25khan is offline
Registered User
 
Join Date: Dec 2003
Last Activity: 2 March 2004, 11:56 PM EST
Location: My home :D
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may contain 20 entries called file pointers which point to an area of memory containing info about the opening of the file.
can someone comment on that, please!
Sponsored Links
    #2  
Old 01-27-2004
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 5 Times in 4 Posts
Short File Descriptors Tutorial

Here's a nice little (basic) tutorial: http://www.livefirelabs.com/unix_tip...3/06092003.htm.

This explains what "info" about a file they hold ... as for the more technical side, you'll have to dig deeper.
Quote:
The Standard UNIX File Descriptors - Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr)

If you are familiar with UNIX I/O redirection, syntax similar to the following should not be new to you:
  • command > file 2>&1
Briefly, when command runs it sends "normal" output to file, and any error messages generated by command are also written to file. "2>&1" handles the latter.

Have you ever wondered where the numbers 2 and 1 come from? This may be common knowledge to our more experienced readers, buy may need some explaining to those who are relatively new to the UNIX operating system environment. The 2 and 1 are file descriptors. Okay, so what's a file descriptor?

When a UNIX program wants to use a file, it must first open that file. When it does so, UNIX will associate a number with the file. This number, which is used by the program when reading from and writing to the file, is the file descriptor.

A typical UNIX program will open three files when it starts. These files are:
  • standard input (also known as stdin)
  • standard output (also known as stdout)
  • standard error (also known as stderr)
Standard input has a file descriptor of 0, standard output uses 1, and the number 2 is used by standard error. Are you starting to see where this is headed?

Looking at our command again,
  • command > file 2>&1
you should now recognize that 2>&1 instructs the shell to send messages headed to stderr (2) to the same place messages to stdout (1) are sent. In our example, that place is file.

If you are wondering, > is equivalent to 1>, and < is short for <0.
Sponsored Links
    #3  
Old 01-27-2004
a25khan's Avatar
a25khan a25khan is offline
Registered User
 
Join Date: Dec 2003
Last Activity: 2 March 2004, 11:56 PM EST
Location: My home :D
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
user oombera,

thanks for your answer,
yeah i see where are we heading from here. when i read it in my text, didn't make any sense. but when you said that "unix reads the files by using their respective numbers(file descriptors)" then i really understood the idea behind this. i have one more question though, what does this have to do with processes!?
a25khan
    #4  
Old 01-27-2004
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 31 August 2014, 11:02 AM EDT
Location: /dev/ph
Posts: 4,787
Thanks: 63
Thanked 424 Times in 393 Posts
Note that processes do not have a limit of 20 file descriptors
as suggested in your mail - rather the limit is OPEN_MAX.

Here is the formal POSIX definition of a file descriptor:

A per-process unique, non-negative integer used to identify an open file for the purpose of file access. The value of a file descriptor is from zero to OPEN_MAX. A process can have no more than OPEN_MAX file descriptors open simultaneously.

Note also the difference between file DESCRIPTOR and file DESCRIPTION. From SUSv3 ....

(Open) File Description

A record of how a process or group of processes is accessing a file. Each file descriptor refers to exactly one open file description, but an open file description can be referred to by more than one file descriptor. The file offset, file status, and file access modes are attributes of an open file description.

- Finnbarr
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Semaphores and File Descriptors Mr_Webster UNIX for Dummies Questions & Answers 1 04-20-2009 05:24 AM
File Descriptors + cron matrixmadhan UNIX for Advanced & Expert Users 7 05-23-2007 01:53 PM
Sockets and File descriptors gstlouis Programming 3 12-12-2005 06:36 AM
File Descriptors shibz UNIX for Advanced & Expert Users 3 12-18-2002 09:12 AM
File Descriptors s_chordia Programming 2 12-24-2001 10:45 AM



All times are GMT -4. The time now is 12:18 AM.