proc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2013
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _PROC_H_
  30. #define _PROC_H_
  31. /*
  32. * Definition of a process.
  33. *
  34. * Note: curproc is defined by <current.h>.
  35. */
  36. #include <spinlock.h>
  37. #include <thread.h> /* required for struct threadarray */
  38. #include <limits.h> // using to restrict array size of procs
  39. #include <list.h>
  40. struct addrspace;
  41. struct vnode;
  42. #ifdef UW
  43. struct semaphore;
  44. #endif // UW
  45. /*
  46. * Process structure.
  47. */
  48. struct proc
  49. {
  50. char * p_name; /* Name of this process */
  51. int pid; // pretty self-explanatory
  52. struct proc * parent; // again, pretty damn obvious
  53. struct list * kids; // list of kid processes (by pid)
  54. int exitcode; // exitcode if exited, -1 if running
  55. struct spinlock p_lock; /* Lock for this structure */
  56. struct threadarray p_threads; /* Threads in this process */
  57. /* VM */
  58. struct addrspace * p_addrspace; /* virtual address space */
  59. /* VFS */
  60. struct vnode * p_cwd; /* current working directory */
  61. #ifdef UW
  62. /* a vnode to refer to the console device */
  63. /* this is a quick-and-dirty way to get console writes working */
  64. /* you will probably need to change this when implementing file-related
  65. system calls, since each process will need to keep track of all files
  66. it has opened, not just the console. */
  67. struct vnode * console; /* a vnode for the console device */
  68. #endif
  69. /* add more material here as needed */
  70. };
  71. // struct that contains all the processes in the system
  72. struct procs
  73. {
  74. struct proc * pids[PID_MAX - PID_MIN + 1]; // total # of valid PIDs
  75. int lastpid; // last pid issued
  76. };
  77. /* This is the process structure for the kernel and for kernel-only threads. */
  78. extern struct proc * kproc;
  79. // The process array
  80. extern struct procs * processes;
  81. // The lock for the above array
  82. extern struct lock * proclock;
  83. /* Semaphore used to signal when there are no more processes */
  84. #ifdef UW
  85. extern struct semaphore * no_proc_sem;
  86. #endif // UW
  87. /* Call once during system startup to allocate data structures. */
  88. void proc_bootstrap(void);
  89. /* Create a fresh process for use by runprogram(). */
  90. struct proc *proc_create_runprogram(const char * name);// get and return a pid for the process
  91. /* Destroy a process. */
  92. void proc_destroy(struct proc * proc);
  93. /* Attach a thread to a process. Must not already have a process. */
  94. int proc_addthread(struct proc * proc, struct thread * t);
  95. /* Detach a thread from its process. */
  96. void proc_remthread(struct thread * t);
  97. /* Fetch the address space of the current process. */
  98. struct addrspace *curproc_getas(void);
  99. /* Change the address space of the current process, and return the old one. */
  100. struct addrspace *curproc_setas(struct addrspace *);
  101. // get and return a pid for the process
  102. int assignpid(struct proc * proc);
  103. // returns the child process if it is a child, otherwise null
  104. struct proc * getChild(struct proc * p, int pid);
  105. #endif /* _PROC_H_ */