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);
37 sigalrm_handler(int signum)
40 kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
51 kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
56 signal(_SIGCHLD, sigchild_handler);
57 signal(_SIGSEGV, sigsegv_handler);
58 signal(_SIGALRM, sigalrm_handler);
65 kprintf("I am parent, going to fork my child and wait.\n");
67 kprintf("I am child, going to sleep for 2 seconds\n");
69 kprintf("I am child, I am about to terminated\n");
73 pid_t child = wait(&status);
74 kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
82 kprintf("Test no hang!\n");
87 waitpid(-1, &status, WNOHANG);
89 for (size_t i = 0; i < 5; i++) {
91 if (!(pid = fork())) {
92 signal(_SIGSEGV, sigsegv_handler);
95 i = *(int*)0xdeadc0de; // seg fault!
97 tty_put_char('0' + i);
101 kprintf(KINFO "Forked %d\n", pid);
104 while ((p = wait(&status)) >= 0) {
105 short code = WEXITSTATUS(status);
106 if (WIFSIGNALED(status)) {
107 kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
110 } else if (WIFEXITED(status)) {
111 kprintf(KINFO "Process %d exited with code %d\n", p, code);
113 kprintf(KWARN "Process %d aborted with code %d\n", p, code);
119 kprintf(KINFO "Hello processes!\n");
122 kprintf("CPU: %s\n\n", buf);
124 // no lxmalloc here! This can only be used within kernel, but here, we are
125 // in a dedicated process! any access to kernel method must be done via
128 struct kdb_keyinfo_pkt keyevent;
130 if (!kbd_recv_key(&keyevent)) {
134 if ((keyevent.state & KBD_KEY_FPRESSED) &&
135 (keyevent.keycode & 0xff00) <= KEYPAD) {
136 tty_put_char((char)(keyevent.keycode & 0x00ff));
137 // FIXME: io to vga port is privileged and cause #GP in user mode
138 // tty_sync_cursor();