《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.1 KiB

10 years ago
  1. #ifndef __KERN_PROCESS_PROC_H__
  2. #define __KERN_PROCESS_PROC_H__
  3. #include <defs.h>
  4. #include <list.h>
  5. #include <trap.h>
  6. #include <memlayout.h>
  7. #include <skew_heap.h>
  8. // process's state in his life cycle
  9. enum proc_state {
  10. PROC_UNINIT = 0, // uninitialized
  11. PROC_SLEEPING, // sleeping
  12. PROC_RUNNABLE, // runnable(maybe running)
  13. PROC_ZOMBIE, // almost dead, and wait parent proc to reclaim his resource
  14. };
  15. // Saved registers for kernel context switches.
  16. // Don't need to save all the %fs etc. segment registers,
  17. // because they are constant across kernel contexts.
  18. // Save all the regular registers so we don't need to care
  19. // which are caller save, but not the return register %eax.
  20. // (Not saving %eax just simplifies the switching code.)
  21. // The layout of context must match code in switch.S.
  22. struct context {
  23. uint32_t eip;
  24. uint32_t esp;
  25. uint32_t ebx;
  26. uint32_t ecx;
  27. uint32_t edx;
  28. uint32_t esi;
  29. uint32_t edi;
  30. uint32_t ebp;
  31. };
  32. #define PROC_NAME_LEN 15
  33. #define MAX_PROCESS 4096
  34. #define MAX_PID (MAX_PROCESS * 2)
  35. extern list_entry_t proc_list;
  36. struct proc_struct {
  37. enum proc_state state; // Process state
  38. int pid; // Process ID
  39. int runs; // the running times of Proces
  40. uintptr_t kstack; // Process kernel stack
  41. volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  42. struct proc_struct *parent; // the parent process
  43. struct mm_struct *mm; // Process's memory management field
  44. struct context context; // Switch here to run process
  45. struct trapframe *tf; // Trap frame for current interrupt
  46. uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  47. uint32_t flags; // Process flag
  48. char name[PROC_NAME_LEN + 1]; // Process name
  49. list_entry_t list_link; // Process link list
  50. list_entry_t hash_link; // Process hash list
  51. int exit_code; // exit code (be sent to parent proc)
  52. uint32_t wait_state; // waiting state
  53. struct proc_struct *cptr, *yptr, *optr; // relations between processes
  54. struct run_queue *rq; // running queue contains Process
  55. list_entry_t run_link; // the entry linked in run queue
  56. int time_slice; // time slice for occupying the CPU
  57. skew_heap_entry_t lab6_run_pool; // FOR LAB6 ONLY: the entry in the run pool
  58. uint32_t lab6_stride; // FOR LAB6 ONLY: the current stride of the process
  59. uint32_t lab6_priority; // FOR LAB6 ONLY: the priority of process, set by lab6_set_priority(uint32_t)
  60. };
  61. #define PF_EXITING 0x00000001 // getting shutdown
  62. #define WT_CHILD (0x00000001 | WT_INTERRUPTED)
  63. #define WT_INTERRUPTED 0x80000000 // the wait state could be interrupted
  64. #define le2proc(le, member) \
  65. to_struct((le), struct proc_struct, member)
  66. extern struct proc_struct *idleproc, *initproc, *current;
  67. void proc_init(void);
  68. void proc_run(struct proc_struct *proc);
  69. int kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags);
  70. char *set_proc_name(struct proc_struct *proc, const char *name);
  71. char *get_proc_name(struct proc_struct *proc);
  72. void cpu_idle(void) __attribute__((noreturn));
  73. struct proc_struct *find_proc(int pid);
  74. int do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf);
  75. int do_exit(int error_code);
  76. int do_yield(void);
  77. int do_execve(const char *name, size_t len, unsigned char *binary, size_t size);
  78. int do_wait(int pid, int *code_store);
  79. int do_kill(int pid);
  80. //FOR LAB6, set the process's priority (bigger value will get more CPU time)
  81. void lab6_set_priority(uint32_t priority);
  82. #endif /* !__KERN_PROCESS_PROC_H__ */