OS2021_Project1.Shell
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
3.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #define CMDLINE_MAX_SIZE 1024 /* max length of a single command line */
  3. #define ARGS_MAX_QUANTITY 128 /* max args on a command line */
  4. #define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */
  5. #define CMDLINE_DIV ' \t\r\n\a'
  6. #define CMDLINE_HISTORY_MAX_QUANTITY 256
  7. #define JOBS_MAX_QUANTITY 16
  8. #define PATH_MAX_SIZE 256
  9. #define LS_BUF_SIZE 1024
  10. #define REDIRECT_FILENAME_MAX_SIZE 64 /* redirection filename */
  11. #define REDIRECT_ARG_MAX_SIZE 16 /* redirection argument */
  12. #define REDIRECT_NO 0 /* no redirection */
  13. #define REDIRECT_OUT 1 /* redirect output */
  14. #define REDIRECT_IN 2 /* redirect input */
  15. typedef int endpoint_t;
  16. typedef uint64_t u64_t;
  17. typedef long unsigned int vir_bytes;
  18. #define USED 0x1
  19. #define IS_TASK 0x2
  20. #define IS_SYSTEM 0x4
  21. #define BLOCKED 0x8
  22. #define TYPE_TASK 'T'
  23. #define TYPE_SYSTEM 'S'
  24. #define STATE_RUN 'R'
  25. #define MAX_NR_TASKS 1023
  26. #define SELF ((endpoint_t)0x8ace)
  27. #define _MAX_MAGIC_PROC (SELF)
  28. #define _ENDPOINT_GENERATION_SIZE (MAX_NR_TASKS + _MAX_MAGIC_PROC + 1)
  29. #define _ENDPOINT_P(e) \
  30. ((((e) + MAX_NR_TASKS) % _ENDPOINT_GENERATION_SIZE) - MAX_NR_TASKS)
  31. #define SLOT_NR(e) (_ENDPOINT_P(e) + 5)
  32. #define _PATH_PROC "/proc"
  33. #define CPUTIME(m, i) (m & (1L << (i)))
  34. const char *cputimenames[] = {"user", "ipc", "kernelcall"};
  35. #define CPUTIMENAMES (sizeof(cputimenames) / sizeof(cputimenames[0]))
  36. struct proc
  37. {
  38. int p_flags;
  39. endpoint_t p_endpoint;
  40. pid_t p_pid;
  41. u64_t p_cpucycles[CPUTIMENAMES];
  42. int p_priority;
  43. endpoint_t p_blocked;
  44. time_t p_user_time;
  45. vir_bytes p_memory;
  46. uid_t p_effuid;
  47. int p_nice;
  48. char p_name[PROC_NAME_LEN + 1];
  49. };
  50. struct tp
  51. {
  52. struct proc *p;
  53. u64_t ticks;
  54. };
  55. /* readline - Get the command line */
  56. char *readline();
  57. /* parseline - Evaluate the command line that the user has just typed in */
  58. int parseline(char *cmdline, char **args);
  59. /* check_redirect - check if the command contains redirection */
  60. int check_redirect(char **args, char *redirect_filename, char **redirect_args);
  61. /* do_redirect - execute redirection command */
  62. int do_redirect(int redirect_flag, char *redirect_filename, char **redirect_args);
  63. /* check_redirect - check if the command contains pipe */
  64. int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2);
  65. /* do_pipe - execute pipe command */
  66. int do_pipe(char **pipe_arg_1, char **pipe_arg_2);
  67. /* do_bgfg - fork and execute background/foreground tasks */
  68. int do_bg_fg(char **args, int bg);
  69. /* execute - Execute the command line */
  70. int execute(char *cmdline, char **args);
  71. /* builtin functions - Handle built-in command */
  72. int built_in(char **args);
  73. int builtin_cd(char **args);
  74. int builtin_history(char **args);
  75. int builtin_mytop();
  76. /* mytop routine */
  77. void mytop_memory();
  78. void mytop_CPU();
  79. void get_procs();
  80. void parse_dir();
  81. void parse_file(pid_t pid);
  82. void getkinfo();
  83. void print_procs(struct proc *proc1, struct proc *proc2, int cputimemode);
  84. u64_t cputicks(struct proc *p1, struct proc *p2, int timemode);
  85. static inline u64_t make64(unsigned long lo, unsigned long hi);