Any program that runs on a Linux machine has access to something
called a File Descriptor Table.This table acts as a map
providing the process access to "files, directories, unnamed pipes, named
pipes, sockets and kernel-level data structures" . This table exists for
each process. Inside the bash shell you have access to the three
standard file descriptors: standard input, standard output and standard
error.
Standard Input, Standard Output, and Standard Error are all character devices. Character devices provide a mechanism to send a stream
of characters or bytes. A stream provides sequential access รข that
is, it provides output in the order in which it was received, this is
also known as a FIFO pipe, which stands for First-In First-Out
In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry number is the file descriptor. So it is just an integer number that uniquely represents an opened file in operating system. If your process opens 10 files then your Process table will have 10 entries for file descriptors.
Similarly when you open a network socket, it is also represented by an integer and it is called Socket Descriptor.
Why is number of open files limited in Linux?
The reason is that the operating system needs memory to manage each open file, and memory is a limited resource - especially on embedded systems.
The reason is that the operating system needs memory to manage each open file, and memory is a limited resource - especially on embedded systems.
As root user you can change the maximum of the open files count per process (via ulimit -n) and per system (e.g echo 800000 > /proc/sys/fs/file-max).
note that lsof | wc -l sums up a lot of duplicated entries (forked processes can share file handles etc). That number could be much higher than the limit set in /proc/sys/fs/file-max.
To get the current number of open files from the Linux kernel point of view, do this: cat /proc/sys/file/file-nr
Example: This server has 40096 out of max 65536 open files, although lsof reports a much larger number:
# cat /proc/sys/fs/file-max
65536
# cat /proc/sys/fs/file-nr
40096 0 65536
# lsof | wc -l
521504
Standard Input
Standard Input is a file descriptor that a program is given for input sent to it. On Linux machines the file descriptor can be accessed under the path /proc/PID/fd/0, where PID is the process id of the program. When the output of one command is piped into the input of another command, the input is taken from Standard Input. It is often referred to by the token STDIN or the file descriptor 0.
Standard Output
Standard Output is a file descriptor that a program is given to send output data to. When a command is run and output goes to a terminal, the output is usually being sent to Standard Output. It is worth nothing that there are exceptions where the output is being sent directly to the terminal. This is the case with anything that uses the curses library for rendering, such as irssi or top. On Linux machines the file descriptor can be accessed under the path /proc/PID/fd/1 where PID is the process id of the command. When the output of a command is piped to another command it is sending the contents of Standard Output. This is often referred to as STDOUT or the file descriptor 1.
Standard Error
Standard Error is a file descriptor that a program is given to send error output data to. When a command reports an error to the terminal it is most often sent through Standard Error. On Linux machines the file descriptor can be accessed under the path /proc/PID/fd/2 where PID is the process id of the command. When displayed in a terminal and not using any forms of redirection there appears to be no difference to the user between Standard Error and Standard Output. It is often referred to by STDERR and the file descriptor 2.
No comments:
Post a Comment