What are file descriptors in linux

What is a file descriptor in linux

In 7th Edition Unix, the file descriptor is an index to a table in the user structure which is simply an array of pointers into the [system global] open file table . So the file descriptor itself is really an indirect reference, a ‘pointer to a pointer’ of sorts.

Solution 2: There are a couple of ways to the get the file descriptors: You can run something like: or File descriptors 0 (stdin), 1 (stdout), and 2 (stderr) are all standard FDs used by all programs. In traditional Unix, file descriptors reference entries in the file table , entries of which are referred to as files , or sometimes open files .

What is the referent of a file descriptor?

There is not really a more specific term. In traditional Unix, file descriptors reference entries in the file table , entries of which are referred to as files , or sometimes open files . This is in a specific context, so while obviously the term file is quite generic, in the context of the file table it specifically refers to open files.

Files on disk are often referred to as inodes , although technically the inode is the metadata portion of the file. However since the relationship between the inode and the data blocks is one-to-one, referring to an inode implicitly refers to the data to which it points. More modern filesystems may support features such as copy-on-write where data blocks may be shared, so this is not universally true, but is for traditional Unix. However, given the term filesystem , it is no small leap to consider the contents of that to be files .

inodes are also read into memory as in-core inodes when a file on disk is opened, and maintained in the inode table , but that is one level of indirection further than you are asking.

Читайте также:  Linux настройка http proxy

This leads to the conflict in terminology: files on disk (referenced by an inode), and open files (referenced by entries in the file table).

I would suggest that «open file» or «file table entry» are both adequate descriptions of what you are looking to describe.

One reasonably concise reference I found is at: http://www.hicom.net/~shchuang/Unix/unix4.html. References of the form (Bach nn ) are references to the book The Design Of The Unix Operating System by Maurice J. Bach.

In 7th Edition Unix, the file descriptor is an index to a table in the user structure which is simply an array of pointers into the [system global] open file table. So the file descriptor itself is really an indirect reference, a ‘pointer to a pointer’ of sorts. It also indexes an array of one-byte flag fields (which only included, at the time at least, the close-on-exec flag).

The open file table itself contains the read/write mode a file was opened with, a pointer to the inode, the position, and probably dozens of other things on modern systems. I have no idea if the file descriptor table contains anything more than this on modern systems.

  • http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/h/user.h (u_ofile)
  • http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/h/file.h
  • http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/sys/fio.c (getf)

C — What is file-descriptor?, File descriptor is an integer in your application that refers to the file description in the kernel. File description is the structure in the kernel that maintains the state of an open file (its current position, blocking/non-blocking, etc.). In Linux file descripion is struct file. POSIX open ():

File Descriptors Explained

File Descriptors (Unix)Note: pipefd[0] is 5 and pipefd[1] is 6 as 4 is the file descriptor for out.txt

What is the file descriptor of my terminal

In your program, by default file descriptors 0 (stdin), 1 (stdout), and 2 (stderr) will be associated with the terminal, unless you use redirection or pipes in the command you use to invoke the program.

Читайте также:  Get smart info linux

There are a couple of ways to the get the file descriptors:

File descriptors 0 (stdin), 1 (stdout), and 2 (stderr) are all standard FDs used by all programs.

What is the file descriptor of my terminal, 0. There are a couple of ways to the get the file descriptors: You can run something like: lsof -p $$ | grep /dev/pts or. ls /proc/$$/fd. File descriptors 0 (stdin), 1 (stdout), and 2 (stderr) are all standard FDs used by all programs. Share. Improve this answer.

What’s so special about file descriptor 3 on linux?

The special aspect of file descriptor 3 is that it will usually be the first file descriptor returned from a system call that allocates a new file descriptor, given that 0, 1 and 2 are usually set up for stdin, stdout and stderr.

This means that if any library function you have called allocates a file descriptor for its own internal purposes in order to perform its functions, it will get fd 3.

The openlog(3) library call will need to open /dev/log to communicate with the syslog daemon. If you subsequently close all file descriptors, you may break the syslog library functions if they are not written in a way to handle that.

The way to debug this on Linux is to use strace to trace the actual system calls that are being made; the use of a file descriptor for syslog then becomes obvious:

$ cat syslog_test.c #include #include int main(void) < openlog("test", LOG_PID, LOG_LOCAL0); syslog(LOG_ERR, "waaaaaah"); closelog(); return 0; >$ gcc -W -Wall -o syslog_test syslog_test.c $ strace ./syslog_test . socket(PF_FILE, SOCK_DGRAM, 0) = 3 fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 connect(3, , 16) = 0 send(3, "Aug 21 00:47:52 test[24264]". 42, MSG_NOSIGNAL) = 42 close(3) = 0 exit_group(0) = ? Process 24264 detached 

syslog(3) may keep a file descriptor to syslogd’s socket open; closing this under its feet is likely to cause problems. A closelog(3) call may help.

Читайте также:  Astra linux разрешение 1920 1080

What’s so special about file descriptor 3 on linux?, 4 Answers. Sorted by: 15. The special aspect of file descriptor 3 is that it will usually be the first file descriptor returned from a system call that allocates a new file descriptor, given that 0, 1 and 2 are usually set up for stdin, stdout and stderr. This means that if any library function you have called …

Sockets and File Descriptors

Opening a socket opens a socket , which is what you see listed as file descriptor 3 in your output ( socket:[5474494] ). The other three file descriptors are the standard input, output and error descriptors, which are opened by default for any process. In your case, these point to the terminal where the program is running, which is why “surni” shows up there when you write to the file descriptor.

To write to the socket, you need to use appropriate mechanisms, such as netcat in the other direction:

echo Hello | nc localhost 9999 
echo Hello > /dev/tcp/localhost/9999 

However it appears you already have a connection established to port 9999 using another netcat , so that won’t actually work in your case. You need to use the established connection.

If you’re expecting a long-running nc to provide a way for other processes to feed into the socket, you need to set that up e.g. using a FIFO:

mkfifo socket-input nc localhost 9999 < socket-input echo Hello >socket-input 

File descriptors & shell scripting, stderr is 2 For opening additional files, there remain descriptors 3 to 9. Take for example. exec 3>&1 4>&2 exec 1> /proc/1/fd/1 2>&1. 3>&1 creates a new file descriptor and redirect it to 1 which is STDOUT 4>&2 also new file descriptor and redirect it to 2 which is STDERR.

Источник

Оцените статью
Adblock
detector