main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // console_put_str(" main_pid:0x");
  30. // console_put_int(sys_getpid());
  31. // console_put_str("\n");
  32. thread_start("kernel_thread_a_func", 31, kernel_thread_a_func, "argA ");
  33. thread_start("kernel_thread_a_func", 31, kernel_thread_b_func, "argB ");
  34. process_execute(user_prog_a, "user_prog_a");
  35. process_execute(user_prog_b, "user_prog_b");
  36. intr_enable(); // 打开中断,使中断起作用
  37. // intr_disable(); // 关闭中断,使中断不起作用
  38. // 主线程会一直执行, 直到被中断或被其他线程强制结束
  39. while (1)
  40. ;
  41. return 0;
  42. }
  43. /*在线程中运行的函数*/
  44. void kernel_thread_a_func(void *arg)
  45. {
  46. // 用 void 来表示通用类型,这样就可以传递任意类型的参数
  47. char *para = arg;
  48. while (1)
  49. {
  50. console_put_str(" v_a:0x");
  51. console_put_int(test_var_a);
  52. }
  53. }
  54. void kernel_thread_b_func(void *arg)
  55. {
  56. char *para = arg;
  57. while (1)
  58. {
  59. console_put_str(" v_b:0x");
  60. console_put_int(test_var_b);
  61. }
  62. }
  63. /*测试用户进程*/
  64. void user_prog_a(void *UNUSED(arg))
  65. {
  66. while (1)
  67. {
  68. test_var_a++;
  69. }
  70. }
  71. void user_prog_b(void *UNUSED(arg))
  72. {
  73. while (1)
  74. {
  75. test_var_b++;
  76. }
  77. }