proc.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // struct that contains all the processes in the system
  46. struct procs
  47. {
  48. struct proc * pids[PID_MAX - PID_MIN + 1]; // total # of valid PIDs
  49. int lastpid; // last pid issued
  50. }
  51. /*
  52. * Process structure.
  53. */
  54. struct proc
  55. {
  56. char * p_name; /* Name of this process */
  57. int pid; // pretty self-explanatory
  58. struct proc * parent; // again, pretty damn obvious
  59. struct list * kids; // list of kid processes (by pid)
  60. int exitcode; // exitcode if exited, -1 if running
  61. struct spinlock p_lock; /* Lock for this structure */
  62. struct threadarray p_threads; /* Threads in this process */
  63. /* VM */
  64. struct addrspace * p_addrspace; /* virtual address space */
  65. /* VFS */
  66. struct vnode * p_cwd; /* current working directory */
  67. #ifdef UW
  68. /* a vnode to refer to the console device */
  69. /* this is a quick-and-dirty way to get console writes working */
  70. /* you will probably need to change this when implementing file-related
  71. system calls, since each process will need to keep track of all files
  72. it has opened, not just the console. */
  73. struct vnode * console; /* a vnode for the console device */
  74. #endif
  75. /* add more material here as needed */
  76. };
  77. /* This is the process structure for the kernel and for kernel-only threads. */
  78. extern struct proc * kproc;
  79. /* Semaphore used to signal when there are no more processes */
  80. #ifdef UW
  81. extern struct semaphore * no_proc_sem;
  82. #endif // UW
  83. /* Call once during system startup to allocate data structures. */
  84. void proc_bootstrap(void);
  85. /* Create a fresh process for use by runprogram(). */
  86. struct proc *proc_create_runprogram(const char * name);
  87. /* Destroy a process. */
  88. void proc_destroy(struct proc * proc);
  89. /* Attach a thread to a process. Must not already have a process. */
  90. int proc_addthread(struct proc * proc, struct thread * t);
  91. /* Detach a thread from its process. */
  92. void proc_remthread(struct thread * t);
  93. /* Fetch the address space of the current process. */
  94. struct addrspace *curproc_getas(void);
  95. /* Change the address space of the current process, and return the old one. */
  96. struct addrspace *curproc_setas(struct addrspace *);
  97. #endif /* _PROC_H_ */