From 639c6575d45423f85211f238753ad08978c6e649 Mon Sep 17 00:00:00 2001 From: Advaith Menon Date: Thu, 15 Jan 2026 19:03:10 +0530 Subject: [PATCH] Finish part 2 * Write forkexec program --- user/include/user.h | 2 +- user/src/lab0/forkexec.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/user/include/user.h b/user/include/user.h index 4f99c52..5e2f488 100644 --- a/user/include/user.h +++ b/user/include/user.h @@ -34,6 +34,6 @@ void printf(int, const char*, ...); char* gets(char*, int max); uint strlen(const char*); void* memset(void*, int, uint); -void* malloc(uint); +void* malloc(uint); // umalloc.c void free(void*); int atoi(const char*); diff --git a/user/src/lab0/forkexec.c b/user/src/lab0/forkexec.c index d98ecf9..c762343 100644 --- a/user/src/lab0/forkexec.c +++ b/user/src/lab0/forkexec.c @@ -2,10 +2,34 @@ #include "stat.h" #include "user.h" +#define _NULL 0 +#define STDOUT_FD 1 + + int main(int argc, char *argv[]) { - // Student code goes here + // int i; + + int pid = fork(); + if (pid == 0) { + /* argv ends with a null ptr so no need for any malloc drama */ + //char **child_argv = malloc((argc + 1) * sizeof(char *)); + + + //for (i = 0; i < argc; ++i) + // child_argv[i] = argv[i]; /* point to same thing */ + + //child_argv[argc] = _NULL; + + /* let's exec */ + exec("echo", argv); + /* for old approach: argv memory should anyway be freed when process + * dies */ + } else { + wait(); + // printf(STDOUT_FD, "The child PID was: %d\n", pid); + } - exit(); -} \ No newline at end of file + exit(); +} -- 2.47.3