Questions tagged [file-descriptor]

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. In Microsoft Windows terminology and in the context of the C standard I/O library, "file handle" is preferred.

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table.

1444 questions
591
votes
13 answers

What are file descriptors, explained in simple terms?

What would be a more simplified description of file descriptors compared to Wikipedia's? Why are they required? Say, take shell processes as an example and how does it apply for it? Does a process table contain more than one file descriptor. If…
Nishant
  • 20,354
  • 18
  • 69
  • 101
292
votes
3 answers

What is the theoretical maximum number of open TCP connections that a modern Linux box can have

Assuming infinite performance from hardware, can a Linux box support >65536 open TCP connections? I understand that the number of ephemeral ports (<65536) limits the number of connections from one local IP to one port on one remote IP. The tuple…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
152
votes
9 answers

What's the difference between a file descriptor and a file pointer?

How are file descriptors and file pointers related? When is it appropriate to use each?
karthi_ms
  • 5,568
  • 11
  • 38
  • 39
129
votes
8 answers

Retrieve filename from file descriptor in C

Is it possible to get the filename of a file descriptor (Linux) in C?
adk
  • 4,479
  • 9
  • 36
  • 38
118
votes
3 answers

What does >& mean?

I was a little confused by this expression: gcc -c -g program.c >& compiler.txt I know &>filename will redirect both stdout and stderr to file filename. But in this case the ampersand is after the greater than sign. It looks like it's of the form…
contrapositive
  • 1,381
  • 2
  • 10
  • 11
112
votes
8 answers

How to construct a c++ fstream from a POSIX file descriptor?

I'm basically looking for a C++ version of fdopen(). I did a bit of research on this and it is one of those things that seems like it should be easy, but turns out to be very complicated. Am I missing something in this belief (i.e. it really is…
BD at Rivenhill
  • 12,395
  • 10
  • 46
  • 49
90
votes
4 answers

How do file descriptors work?

Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost. #!/bin/bash echo "This" echo "is" >&2 echo "a" >&3 echo "test." >&4 The first three lines run fine, but the last two error out. Why?
Trcx
  • 4,164
  • 6
  • 30
  • 30
88
votes
9 answers

Get list of open files (descriptors) in OS X

I would like to get a list of open files in a process on os x (10.9.1). In Linux I was able to get this from /proc/PID/fd. However I'm not sure how to get the same on OS X. I found that the procfs is not present on the OS X (by default. possible…
user3169543
  • 1,499
  • 1
  • 10
  • 16
85
votes
3 answers

What can lead to "IOError: [Errno 9] Bad file descriptor" during os.system()?

I am using a scientific software including a Python script that is calling os.system() which is used to run another scientific program. While the subprocess is running, Python at some point prints the following: close failed in file object…
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
66
votes
7 answers

Check the open FD limit for a given process in Linux

I recently had a Linux process which “leaked” file descriptors: It opened them and didn't properly close some of them. If I had monitored this, I could tell – in advance – that the process was reaching its limit. Is there a nice, Bash or Python way…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
63
votes
6 answers

How to check if a given file descriptor stored in a variable is still valid?

I have a file descriptor stored in a variable say var. How can I check whether that descriptor is valid at a later stage? fdvar1= open(.....); fdvar2 = fdvar1; // Please ignore the bad design .... // lots of loops , conditionals and…
Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88
49
votes
6 answers

Getting the highest allocated file descriptor

Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process? I know that there's a nice way to get the number on AIX, for example, but I'm looking for a portable method. The reason I'm asking is that I…
Ville Laurikari
  • 28,380
  • 7
  • 60
  • 55
49
votes
3 answers

Golang bad file descriptor

I am getting a bad file descriptor when trying to append to a logging file within my go routine. write ./log.log: bad file descriptor The file exists and has 666 for permissions. At first I thought well maybe it is because each one of them is…
Jared Mackey
  • 3,998
  • 4
  • 31
  • 50
46
votes
1 answer

ftell on a file descriptor?

Is there a way to do what ftell() does (return the current position in the file) on a raw file descriptor instead of a FILE*? I think there ought to be, since you can seek on a raw file descriptor using lseek(). I know I could use fdopen() to create…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
43
votes
3 answers

Why is select used in Linux

I was going through a serial program and I observed that they use select() before using read(). Why exactly is this required. Why cant we just directly call read() and check if it fails or not ? Also why do we have to increment the file descriptor…
user1667307
  • 2,041
  • 6
  • 27
  • 32
1
2 3
96 97