refactor: replace all stdint::uint32_t into short and more manageable u32_t
[lunaix-os.git] / lunaix-os / kernel / lxinit.c
index ec3425c1df6cd0fc0c141ff10238b223afb830a5..f53405ab051acc45beddbac5186351131b7e07a8 100644 (file)
@@ -1,81 +1,90 @@
 #include <hal/cpu.h>
-#include <lunaix/syslog.h>
+#include <lunaix/clock.h>
+#include <lunaix/lunaix.h>
+#include <lunaix/lunistd.h>
+#include <lunaix/lxconsole.h>
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/spike.h>
-#include <lunaix/clock.h>
+#include <lunaix/syslog.h>
 #include <lunaix/timer.h>
-#include <lunaix/keyboard.h>
-#include <lunaix/tty/tty.h>
-#include <lunaix/lunistd.h>
-#include <lunaix/proc.h>
 
 extern uint8_t __kernel_start;
 
 LOG_MODULE("INIT")
 
-void 
-test_timer(void* payload);
+// #define FORK_BOMB_DEMO
+#define WAIT_DEMO
+#define IN_USER_MODE
 
-void
+void __USER__
 _lxinit_main()
 {
-    // 这里是就是LunaixOS的第一个进程了!
-    for (size_t i = 0; i < 10; i++)
-    {
-        pid_t pid = 0;
-        if (!(pid = fork())) {
-            while (1)
-            {
-                // kprintf(KINFO "Process %d\n", i);
-                tty_put_char('0'+i);
-                yield();
-            }
+#ifdef FORK_BOMB_DEMO
+    // fork炸弹
+    for (;;) {
+        pid_t p;
+        if ((p = fork())) {
+            kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
         }
-        kprintf(KINFO "Forked %d\n", pid);
     }
+#endif
 
-    char buf[64];
+    int status;
+#ifdef WAIT_DEMO
+    // 测试wait
+    kprintf("I am parent, going to fork my child and wait.\n");
+    if (!fork()) {
+        kprintf("I am child, going to sleep for 2 seconds\n");
+        sleep(2);
+        kprintf("I am child, I am about to terminated\n");
+        _exit(1);
+    }
+    wait(&status);
+    pid_t child = wait(&status);
+    kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
+            child,
+            WEXITSTATUS(status));
+#endif
 
-    kprintf(KINFO "Hello higher half kernel world!\nWe are now running in virtual "
-           "address space!\n\n");
+    pid_t p = 0;
 
-    cpu_get_brand(buf);
-    kprintf("CPU: %s\n\n", buf);
-
-    void* k_start = vmm_v2p(&__kernel_start);
-    kprintf(KINFO "The kernel's base address mapping: %p->%p\n", &__kernel_start, k_start);
+    if (!fork()) {
+        kprintf("Test no hang!\n");
+        sleep(6);
+        _exit(0);
+    }
 
-    // no lxmalloc here! This can only be used within kernel, but here, we are in a dedicated process!
-    // any access to kernel method must be done via syscall
+    waitpid(-1, &status, WNOHANG);
 
-    struct kdb_keyinfo_pkt keyevent;
-    while (1)
-    {
-        if (!kbd_recv_key(&keyevent)) {
-            // yield();
-            continue;
+    for (size_t i = 0; i < 5; i++) {
+        pid_t pid = 0;
+        if (!(pid = fork())) {
+            sleep(i);
+            if (i == 3) {
+                i = *(int*)0xdeadc0de; // seg fault!
+            }
+            kprintf(KINFO "%d\n", i);
+            _exit(0);
         }
-        if ((keyevent.state & KBD_KEY_FPRESSED) && (keyevent.keycode & 0xff00) <= KEYPAD) {
-            tty_put_char((char)(keyevent.keycode & 0x00ff));
-            tty_sync_cursor();
+        kprintf(KINFO "Forked %d\n", pid);
+    }
+
+    while ((p = wait(&status)) >= 0) {
+        short code = WEXITSTATUS(status);
+        if (WIFEXITED(status)) {
+            kprintf(KINFO "Process %d exited with code %d\n", p, code);
+        } else {
+            kprintf(KWARN "Process %d aborted with code %d\n", p, code);
         }
     }
-    
 
-    spin();
-}
+    char buf[64];
 
-static datetime_t datetime;
+    kprintf(KINFO "Hello processes!\n");
 
-void test_timer(void* payload) {
-    clock_walltime(&datetime);
+    cpu_get_brand(buf);
+    kprintf("CPU: %s\n\n", buf);
 
-    kprintf(KWARN "%u/%02u/%02u %02u:%02u:%02u\r",
-           datetime.year,
-           datetime.month,
-           datetime.day,
-           datetime.hour,
-           datetime.minute,
-           datetime.second);
+    _exit(0);
 }
\ No newline at end of file