《操作系统》的实验代码。
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

104 рядки
4.5 KiB

10 роки тому
10 роки тому
10 роки тому
10 роки тому
  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 50
  33. #define MAX_PROCESS 4096
  34. #define MAX_PID (MAX_PROCESS * 2)
  35. extern list_entry_t proc_list;
  36. struct inode;
  37. struct proc_struct {
  38. enum proc_state state; // Process state
  39. int pid; // Process ID
  40. int runs; // the running times of Proces
  41. uintptr_t kstack; // Process kernel stack
  42. volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  43. struct proc_struct *parent; // the parent process
  44. struct mm_struct *mm; // Process's memory management field
  45. struct context context; // Switch here to run process
  46. struct trapframe *tf; // Trap frame for current interrupt
  47. uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  48. uint32_t flags; // Process flag
  49. char name[PROC_NAME_LEN + 1]; // Process name
  50. list_entry_t list_link; // Process link list
  51. list_entry_t hash_link; // Process hash list
  52. int exit_code; // exit code (be sent to parent proc)
  53. uint32_t wait_state; // waiting state
  54. struct proc_struct *cptr, *yptr, *optr; // relations between processes
  55. struct run_queue *rq; // running queue contains Process
  56. list_entry_t run_link; // the entry linked in run queue
  57. int time_slice; // time slice for occupying the CPU
  58. skew_heap_entry_t lab6_run_pool; // FOR LAB6 ONLY: the entry in the run pool
  59. uint32_t lab6_stride; // FOR LAB6 ONLY: the current stride of the process
  60. uint32_t lab6_priority; // FOR LAB6 ONLY: the priority of process, set by lab6_set_priority(uint32_t)
  61. struct files_struct *filesp; // the file related info(pwd, files_count, files_array, fs_semaphore) of process
  62. };
  63. #define PF_EXITING 0x00000001 // getting shutdown
  64. #define WT_INTERRUPTED 0x80000000 // the wait state could be interrupted
  65. #define WT_CHILD (0x00000001 | WT_INTERRUPTED) // wait child process
  66. #define WT_KSEM 0x00000100 // wait kernel semaphore
  67. #define WT_TIMER (0x00000002 | WT_INTERRUPTED) // wait timer
  68. #define WT_KBD (0x00000004 | WT_INTERRUPTED) // wait the input of keyboard
  69. #define le2proc(le, member) \
  70. to_struct((le), struct proc_struct, member)
  71. extern struct proc_struct *idleproc, *initproc, *current;
  72. void proc_init(void);
  73. void proc_run(struct proc_struct *proc);
  74. int kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags);
  75. char *set_proc_name(struct proc_struct *proc, const char *name);
  76. char *get_proc_name(struct proc_struct *proc);
  77. void cpu_idle(void) __attribute__((noreturn));
  78. struct proc_struct *find_proc(int pid);
  79. int do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf);
  80. int do_exit(int error_code);
  81. int do_yield(void);
  82. int do_execve(const char *name, int argc, const char **argv);
  83. int do_wait(int pid, int *code_store);
  84. int do_kill(int pid);
  85. //FOR LAB6, set the process's priority (bigger value will get more CPU time)
  86. void lab6_set_priority(uint32_t priority);
  87. int do_sleep(unsigned int time);
  88. #endif /* !__KERN_PROCESS_PROC_H__ */