]> Devi Nivas Git - cs3210-lab0.git/commitdiff
Maximum Number of Processes
authorAdvaith Menon <noreply-git@bp4k.net>
Thu, 15 Jan 2026 15:50:30 +0000 (21:20 +0530)
committerAdvaith Menon <noreply-git@bp4k.net>
Thu, 15 Jan 2026 15:50:30 +0000 (21:20 +0530)
user/src/lab0/limits.c

index 837b00d1201016451a5f6156b94911b3d2e1aad2..02be4001c133e6bf0d7953d476f520ddd0294622 100644 (file)
@@ -4,12 +4,32 @@
 
 #define PGSIZE 4096  // 4K Page size
 #define CURRENTLY_OCCUPIED_MEM 0
+#define CURRENTLY_OPEN_PROCESSES 2
 #define STDOUT_FD 1
 
+static void test_maxmem(void);
+static void test_maxfd(void);
+static void test_maxproc(void);
+static void forkfun(void (*) (void));
+
+
 int
 main(int argc, char *argv[])
 {
     // Student code goes here
+    /* Global gameplan: Since this deals with exhaustion of resources and
+     * resources won't be freed till the process dies, we should do these in
+     * subprocesses. */
+
+    forkfun(&test_maxmem);
+    forkfun(&test_maxfd);
+    test_maxproc(); /* I deal with exiting */
+
+
+}
+
+
+void test_maxmem(void) {
     int memsz;
 
     /* initialize to current memory */
@@ -21,7 +41,10 @@ main(int argc, char *argv[])
         memsz += PGSIZE;
 
     printf(STDOUT_FD, "Maximum Memory Size: 0x%x\n", memsz);
+}
+
 
+void test_maxfd(void) {
     /* 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
@@ -47,5 +70,37 @@ main(int argc, char *argv[])
     // poc validated
     // printf(STDOUT_FD, "Maximum Number of Files Open at Once: %d\n",
     //         manual_tally);
+}
+
+void forkfun(void (*fun) (void)) {
+    /* fork function */
+
+    int pid = fork();
+
+    if (pid > 0) {
+        /* wait till child finishes */
+        wait();
+    } else {
+        /* "exec" the child */
+        fun();
+        exit();
+    }
+}
+
+void test_maxproc(void) {
+    /* Extra Credit: Max. no of processes */
+    /* This one is just creating a fork bomb. */
+    /* So PIDs don't seem to be reused hence TBC */
+    int max_procs, fpid;
+
+    max_procs = CURRENTLY_OPEN_PROCESSES; /* 1 for the shell and 1 for me */
+
+    while ((fpid = fork()) != -1)
+        if (fpid != 0) {wait(); exit(); } /* parents wait, then exit */
+        else ++max_procs;
+
+    /* these executed by the last child of the fork bomb */
+    printf(STDOUT_FD, "Maximum Number of Processes: %d\n", max_procs);
+    // printf(STDOUT_FD, "fpid is: %d\n", fpid);
     exit();
 }