main.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "../lib/kernel/print.h"
  2. #include "debug.h"
  3. #include "init.h"
  4. #include "memory.h"
  5. #include "../thread/thread.h"
  6. #include "interrupt.h"
  7. #include "../device/console.h"
  8. #include "../device/ioqueue.h"
  9. #include "../device/keyboard.h"
  10. #include "../userprog/process.h"
  11. #include "../userprog/syscall-init.h"
  12. #include "../lib/user/syscall.h"
  13. void kernel_thread_a_func(void *);
  14. void kernel_thread_b_func(void *);
  15. void user_prog_a(void *);
  16. void user_prog_b(void *);
  17. int test_var_a = 0, test_var_b = 0;
  18. int prog_a_pid = 0, prog_b_pid = 0;
  19. int main(void)
  20. {
  21. put_str("I am kernel\n");
  22. init_all();
  23. // asm volatile("sti"); // 使能中断
  24. // ASSERT(1 == 2);
  25. // void *addr = get_kernel_pages(4);
  26. // put_str("\n get_kernel_page start vaddr is ");
  27. // put_int((uintptr_t)addr);
  28. // put_str("\n");
  29. process_execute(user_prog_a, "user_prog_a");
  30. // process_execute(user_prog_b, "user_prog_b");
  31. intr_enable(); // 打开中断,使中断起作用
  32. console_put_str(" main_pid:0x");
  33. console_put_int(sys_getpid());
  34. console_put_str("\n");
  35. thread_start("kernel_thread_a_func", 31, kernel_thread_a_func, "argA ");
  36. thread_start("kernel_thread_a_func", 31, kernel_thread_b_func, "argB ");
  37. // 主线程会一直执行, 直到被中断或被其他线程强制结束
  38. while (1)
  39. ;
  40. return 0;
  41. }
  42. /*在线程中运行的函数*/
  43. void kernel_thread_a_func(void *arg)
  44. {
  45. // 用 void 来表示通用类型,这样就可以传递任意类型的参数
  46. char *para = arg;
  47. console_put_str(" thread_a_pid:0x");
  48. console_put_int(sys_getpid());
  49. console_put_char('\n');
  50. console_put_str(" prog_a_pid:0x");
  51. console_put_int(prog_a_pid);
  52. console_put_char('\n');
  53. }
  54. void kernel_thread_b_func(void *arg)
  55. {
  56. char *para = arg;
  57. console_put_str(" thread_b_pid:0x");
  58. console_put_int(sys_getpid());
  59. console_put_char('\n');
  60. console_put_str(" prog_b_pid:0x");
  61. console_put_int(prog_b_pid);
  62. console_put_char('\n');
  63. }
  64. /*测试用户进程*/
  65. void user_prog_a(void *UNUSED(arg))
  66. {
  67. prog_a_pid = getpid();
  68. while (1)
  69. ;
  70. }
  71. void user_prog_b(void *UNUSED(arg))
  72. {
  73. prog_b_pid = getpid();
  74. while (1)
  75. ;
  76. }