From dbba25aa24f86c1f8c3a0589c82985840adc1b6b Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Wed, 31 Dec 2025 18:00:55 +0800 Subject: dev:Refactor Memory Management and Process Management --- src/user/init.cc | 104 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 10 deletions(-) (limited to 'src/user/init.cc') diff --git a/src/user/init.cc b/src/user/init.cc index 3c42adf..85252be 100644 --- a/src/user/init.cc +++ b/src/user/init.cc @@ -1,34 +1,118 @@ #include "user/user.h" -int main(); +int strlen(const char *s) +{ + int n = 0; + while (s[n]) + n++; + return n; +} -extern "C" void _start() +void print(const char *s) { - exit(main()); + write(1, s, strlen(s)); +} + +void print_int(int val) +{ + char buf[16]; + int i = 0; + if (val == 0) + { + print("0"); + return; + } + if (val < 0) + { + print("-"); + val = -val; + } + while (val > 0) + { + buf[i++] = (val % 10) + '0'; + val /= 10; + } + while (i > 0) + { + char c = buf[--i]; + write(1, &c, 1); + } +} + +void pass() { print(" -> [PASS]\n"); } +void fail() +{ + print(" -> [FAIL]\n"); + exit(1); } int main() { + print("\n=== Lume OS Full Validation ===\n"); + + print("[TEST 1] Write\n"); + print(" System call write is working."); + pass(); + + print("[TEST 2] GetPID\n"); + int parent_pid = getpid(); + print(" Parent PID: "); + print_int(parent_pid); + if (parent_pid > 0) + pass(); + else + fail(); + + print("[TEST 3] Fork, Wait & COW\n"); + int val = 100; int pid = fork(); if (pid < 0) { - write(1, "Fork failed\n", 12); - exit(1); + print(" Fork Failed\n"); + fail(); } if (pid == 0) { - write(1, "Hello from Child!\n", 18); + print(" [Child] PID: "); + print_int(getpid()); + print("\n"); + + if (val != 100) + { + print(" [Child] Data corruption!\n"); + exit(1); + } + + val = 200; + print(" [Child] Modified val to 200. Exiting.\n"); exit(0); } else { - write(1, "Parent waiting...\n", 18); - wait(0); - write(1, "Parent: Child exited.\n", 22); + print(" [Parent] Waiting for child...\n"); + int child_pid = wait(0); + + print(" [Parent] Child "); + print_int(child_pid); + print(" exited.\n"); + + print(" [Parent] Check val (Expect 100): "); + print_int(val); + + if (val == 100) + pass(); + else + { + print(" [Parent] Memory Isolation Failed! Val="); + print_int(val); + fail(); + } } - while (1); + print("\n=== All Tests Passed ===\n"); + while (1) + ; return 0; } \ No newline at end of file -- cgit v1.2.3