main.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. void thread_a_func(void *);
  11. void thread_b_func(void *);
  12. int main(void)
  13. {
  14. put_str("I am kernel\n");
  15. init_all();
  16. // asm volatile("sti"); // 使能中断
  17. // ASSERT(1 == 2);
  18. // void *addr = get_kernel_pages(4);
  19. // put_str("\n get_kernel_page start vaddr is ");
  20. // put_int((uintptr_t)addr);
  21. // put_str("\n");
  22. thread_start("consumer_a", 31, thread_a_func, " A_");
  23. thread_start("consumer_b", 31, thread_b_func, " B_");
  24. intr_enable(); // 打开中断,使中断起作用
  25. // 主线程会一直执行, 直到被中断或被其他线程强制结束
  26. while (1)
  27. ;
  28. // {
  29. // console_put_str("Main ");
  30. // }
  31. return 0;
  32. }
  33. void thread_a_func(void *arg)
  34. {
  35. // 用 void 来表示通用类型,这样就可以传递任意类型的参数
  36. while (1)
  37. {
  38. enum intr_status old_status = intr_disable();
  39. if (!ioq_empty(&keyboard_buf))
  40. {
  41. console_put_str(arg);
  42. char byte = ioq_getchar(&keyboard_buf);
  43. console_put_char(byte);
  44. }
  45. intr_set_status(old_status);
  46. }
  47. }
  48. void thread_b_func(void *arg)
  49. {
  50. while (1)
  51. {
  52. enum intr_status old_status = intr_disable();
  53. if (!ioq_empty(&keyboard_buf))
  54. {
  55. console_put_str(arg);
  56. char byte = ioq_getchar(&keyboard_buf);
  57. console_put_char(byte);
  58. }
  59. intr_set_status(old_status);
  60. }
  61. }