]> Devi Nivas Git - cs3210-lab0.git/commitdiff
EC part 5
authorAdvaith Menon <noreply-git@bp4k.net>
Thu, 15 Jan 2026 14:08:38 +0000 (19:38 +0530)
committerAdvaith Menon <noreply-git@bp4k.net>
Thu, 15 Jan 2026 14:08:38 +0000 (19:38 +0530)
user/src/lab0/limits.c

index 745f205b120d30547cc4c8dbe45b1ce2a6741fdf..837b00d1201016451a5f6156b94911b3d2e1aad2 100644 (file)
@@ -22,5 +22,30 @@ main(int argc, char *argv[])
 
     printf(STDOUT_FD, "Maximum Memory Size: 0x%x\n", memsz);
 
+    /* Extra Credit: Max no of file descriptors */
+    /* Game Plan: File descriptors are allocated sequentially, as we know this
+     * from the xv6 book's explanation on replacing stdin/stdout. We shall keep
+     * track of the current and last file descriptor and keep on duplicating an
+     * existing file until dup returns -1.
+     *
+     * To prove that this works, I'll also keep a manual file counter and ensure
+     * they match.
+     */
+    int curfd, prevfd;
+    // int manual_tally;
+
+    // POC verified
+    // manual_tally = 3;
+
+    curfd = 2; /* because stderr. Only used if first dup fails. */
+
+    while ((prevfd = curfd) && (curfd = dup(0)) >= 0);
+        // ++manual_tally;
+
+    printf(STDOUT_FD, "Maximum Number of Files Open at Once: %d\n",
+            prevfd + 1); // remember first valid descriptor is 0
+    // poc validated
+    // printf(STDOUT_FD, "Maximum Number of Files Open at Once: %d\n",
+    //         manual_tally);
     exit();
 }