《操作系统》的实验代码。
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

105 satır
4.6 KiB

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