proc.h 4.5 KB

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