From 7562f8cffbde02c2d3aa9f68665ac151a7d39ffe Mon Sep 17 00:00:00 2001 From: Advaith Menon Date: Thu, 15 Jan 2026 21:20:30 +0530 Subject: [PATCH] Maximum Number of Processes --- user/src/lab0/limits.c | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/user/src/lab0/limits.c b/user/src/lab0/limits.c index 837b00d..02be400 100644 --- a/user/src/lab0/limits.c +++ b/user/src/lab0/limits.c @@ -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(); } -- 2.47.3