《操作系统》的实验代码。
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.

77 lines
2.8 KiB

12 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. // process's state in his life cycle
  8. enum proc_state {
  9. PROC_UNINIT = 0, // uninitialized
  10. PROC_SLEEPING, // sleeping
  11. PROC_RUNNABLE, // runnable(maybe running)
  12. PROC_ZOMBIE, // almost dead, and wait parent proc to reclaim his resource
  13. };
  14. // Saved registers for kernel context switches.
  15. // Don't need to save all the %fs etc. segment registers,
  16. // because they are constant across kernel contexts.
  17. // Save all the regular registers so we don't need to care
  18. // which are caller save, but not the return register %eax.
  19. // (Not saving %eax just simplifies the switching code.)
  20. // The layout of context must match code in switch.S.
  21. struct context {
  22. uint32_t eip;
  23. uint32_t esp;
  24. uint32_t ebx;
  25. uint32_t ecx;
  26. uint32_t edx;
  27. uint32_t esi;
  28. uint32_t edi;
  29. uint32_t ebp;
  30. };
  31. #define PROC_NAME_LEN 15
  32. #define MAX_PROCESS 4096
  33. #define MAX_PID (MAX_PROCESS * 2)
  34. extern list_entry_t proc_list;
  35. struct proc_struct {
  36. enum proc_state state; // Process state
  37. int pid; // Process ID
  38. int runs; // the running times of Proces
  39. uintptr_t kstack; // Process kernel stack
  40. volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  41. struct proc_struct *parent; // the parent process
  42. struct mm_struct *mm; // Process's memory management field
  43. struct context context; // Switch here to run process
  44. struct trapframe *tf; // Trap frame for current interrupt
  45. uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  46. uint32_t flags; // Process flag
  47. char name[PROC_NAME_LEN + 1]; // Process name
  48. list_entry_t list_link; // Process link list
  49. list_entry_t hash_link; // Process hash list
  50. };
  51. #define le2proc(le, member) \
  52. to_struct((le), struct proc_struct, member)
  53. extern struct proc_struct *idleproc, *initproc, *current;
  54. void proc_init(void);
  55. void proc_run(struct proc_struct *proc);
  56. int kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags);
  57. char *set_proc_name(struct proc_struct *proc, const char *name);
  58. char *get_proc_name(struct proc_struct *proc);
  59. void cpu_idle(void) __attribute__((noreturn));
  60. struct proc_struct *find_proc(int pid);
  61. int do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf);
  62. int do_exit(int error_code);
  63. #endif /* !__KERN_PROCESS_PROC_H__ */