File Descriptor:
When a Unix/Linux program wants to use a file, it must first open that
file. When it does so, Unix/Linux 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 file descriptor is nothing but a data structure used by a program to get
a handle on a file, the most well know being 0,1,2 for standard in, standard
out, and standard error.
when you login the system ,system default creates three 3 file descriptor
for you
the are
1.stdin 0
2.stdout 1
3.stderr 2
If you are C programmer ,you can close this default file descriptor in the
particular program using "close() system call".
ex:
/*file name p1.c*/
#include<stdio.h>
int main()
{
int a;
close(0);
scanf("%d",&a);
printf("%d",a);
}
compile this using
#gcc -Wall p1.c
run it
#./a.out
it will print garbage value ,because in this program we
closed file descriptor 0 (stdin) using close() system call .So this program
can't get input from keyboard.
In the same way we will close the stdout,stderr.
After you login you open a file for r/w system may assign no 4 for this new
open file.
On Unix/Linux systems, there is a limit set in the kernel on how many open
file descriptors are allowed on the system.we can see it by using
#cat /proc/sys/fs/file-max
49053 (i am using ubuntu see your
distro)
On my ubuntu system 49053 open file descriptors are permitted. We are
unlikely to run out. If we wanted to change it, we'd do something like this:
root@mohan-desktop:~# echo "104853" > /proc/sys/fs/file-max
root@mohan-desktop:~# cat /proc/sys/fs/file-max
104853
(Warning, change kernel parameters on the fly only with great caution and
good cause)
But how do we know how many file descriptors are being used?
root@mohan-desktop:~# cat /proc/sys/fs/file-nr
4608 211 104853
| | |
(1) (2) (3)
1.total allocated file descriptor(the number of file descriptor allocated
since boot)
2.total free allocated file descriptors
3.total file descriptor
L.mohan