2 #include <lunaix/clock.h>
3 #include <lunaix/keyboard.h>
4 #include <lunaix/lunistd.h>
5 #include <lunaix/mm/kalloc.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/proc.h>
8 #include <lunaix/signal.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/syslog.h>
11 #include <lunaix/timer.h>
12 #include <lunaix/tty/tty.h>
14 extern uint8_t __kernel_start;
18 // #define FORK_BOMB_DEMO
23 sigchild_handler(int signum)
25 kprintf(KINFO "SIGCHLD received\n");
29 sigsegv_handler(int signum)
32 kprintf(KWARN "SIGSEGV received on process %d\n", pid);
44 kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
49 signal(_SIGCHLD, sigchild_handler);
50 signal(_SIGSEGV, sigsegv_handler);
55 kprintf("I am parent, going to fork my child and wait.\n");
57 kprintf("I am child, going to sleep for 2 seconds\n");
59 kprintf("I am child, I am about to terminated\n");
63 pid_t child = wait(&status);
64 kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
72 kprintf("Test no hang!\n");
77 waitpid(-1, &status, WNOHANG);
79 for (size_t i = 0; i < 5; i++) {
81 if (!(pid = fork())) {
82 signal(_SIGSEGV, sigsegv_handler);
85 i = *(int*)0xdeadc0de; // seg fault!
87 tty_put_char('0' + i);
91 kprintf(KINFO "Forked %d\n", pid);
94 while ((p = wait(&status)) >= 0) {
95 short code = WEXITSTATUS(status);
96 if (WIFSIGNALED(status)) {
97 kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
100 } else if (WIFEXITED(status)) {
101 kprintf(KINFO "Process %d exited with code %d\n", p, code);
103 kprintf(KWARN "Process %d aborted with code %d\n", p, code);
109 kprintf(KINFO "Hello processes!\n");
112 kprintf("CPU: %s\n\n", buf);
114 // no lxmalloc here! This can only be used within kernel, but here, we are
115 // in a dedicated process! any access to kernel method must be done via
118 struct kdb_keyinfo_pkt keyevent;
120 if (!kbd_recv_key(&keyevent)) {
124 if ((keyevent.state & KBD_KEY_FPRESSED) &&
125 (keyevent.keycode & 0xff00) <= KEYPAD) {
126 tty_put_char((char)(keyevent.keycode & 0x00ff));
127 // FIXME: io to vga port is privileged and cause #GP in user mode
128 // tty_sync_cursor();