user2882307
user2882307

Reputation:

fileno() and files larger than 2gb

I am working with huge files. (>>>2gb). the question I have is it safe to use fileno() on the file descriptor if the file is larger than sizeof(int) is?

Here a quick code snippet:

#define  _FILE_OFFSET_BITS  64
#include <stdio.h>
#include <inttypes.h>


int readstuff(FILE *fp,uint64_t seekpoint, uint64_t seekwidth) {
    int j;
    char buf[seekwidth];

    if (pread(fileno(fp),buf,seekwidth,seekpoint)!=-1) {
        /* do stuf */
        return 1;
    }
    else {
        return 2;
    }
}

int main() {
    FILE *FP;

    FP=fopen("/testfile","r");
    readstuff(FP,0,10000);
}

Upvotes: 1

Views: 202

Answers (3)

fileno(3) gives a file descriptor -which is a fixed small integer once you fopen-ed a stdio FILE stream. A process usually has only dozens of file descriptors, and occasionally (for some servers, see C10K problem) a few dozen of thousands of them.

The kernel is allocating file descriptors (with e.g. open(2) etc...), and gives "small" (generally contiguous) integers

Typically, a file descriptor is a non-negative integer often less than 100, and generally less than 100000. Of course the file descriptor is unrelated to the file size.

Try ls /proc/self/fd/ to list the file descriptors of the process running that ls command and ls /proc/1234/fd/ to list the file descriptors of process of pid 1234.

The command cat /proc/sys/fs/file-max gives the total cumulated maximum number of file descriptors on your system (on mine, it is 1629935 now), and each process has a fraction of that.

You may limit the number of file descriptors a process (and its children) can have with setrlimit(2) using RLIMIT_NOFILE. The bash builtin ulimit calls that syscall (on my system, the descriptors limit is 1024 by default).

Read Advanced Linux Programming.

Upvotes: 0

Lee Duhem
Lee Duhem

Reputation: 15121

Yes, you can. The value of a file descriptor is unrelated to the size of that file.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753655

The file descriptor returned by fileno() is an int, regardless of the size of the file that it is used to open.

Upvotes: 1

Related Questions