[cs631apue] Openmax.c fails earlier than expected

Charles Magyar IV cmagyar at stevens.edu
Mon Sep 14 19:45:38 EDT 2020


Hello all,

I changed openmax.c openFiles(int num) to print out what # iteration of the loop it is in when open fails (and NOT because of EMFILE limit).
The code is included at the end of this e-mail.

I change the limit to unlimited, and the program starts to throw issues at file # 3249.  I had 8 files already open.
With 8 files open, I would expect it to fail at 3396.

Also, the last line of output is File # 3405 which fails due to "too many files" rather than "due to limit."
The number 3405 makes sense because the function is called with the num parameter of 3404,
and the loop goes up until num+1, but it's interesting that it doesn't generate the EMFILE errno.

Attached is a short video illustrating my results.

void
openFiles(int num) {
        struct stat stats;
        int count = 0;
        int fd;

        for (int i = 0; i < num; i++) {
                if (fstat(i, &stats) == 0) {
                        printf("Fd #%d is open.\n", i);
                        count++;
                }
        }
        printf("Currently open files: %d\n", count);

        for (int i = count; i <= num + 1; i++) {
                if ((fd = open("/dev/null", O_RDONLY)) < 0) {
                        if (errno == EMFILE) {
                                printf("Opened %d additional files, then failed due to limit.\n", i - count);
                                break;
                        } else {
        fprintf(stdout, "File # %d err\n",i);
        fprintf(stderr, "Unable to open '/dev/null': %s\n",
                                                strerror(errno));
                        }
                }
        }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.stevens.edu/pipermail/cs631apue/attachments/20200914/e94b253e/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2020-09-14 19-24-32.mkv
Type: video/x-matroska
Size: 2877797 bytes
Desc: 2020-09-14 19-24-32.mkv
URL: <http://lists.stevens.edu/pipermail/cs631apue/attachments/20200914/e94b253e/attachment-0001.bin>


More information about the cs631apue mailing list