فهرست منبع

added logic to get/remove PIDs on process creation and destruction

tarfeef101 7 سال پیش
والد
کامیت
70ed7f87b4
2فایلهای تغییر یافته به همراه34 افزوده شده و 2 حذف شده
  1. 34 1
      kern/proc/proc.c
  2. 0 1
      kern/syscall/proc_syscalls.c

+ 34 - 1
kern/proc/proc.c

@@ -143,6 +143,34 @@ proc * getChild(struct proc * p, int pid)
   return procs->pids[pid];
 }
 
+int assignpid(struct proc * proc)
+{
+  lock_acquire(proclock);
+  
+  for (int i = procs->lastpid + 1; i <= PID_MAX; i++)
+  {
+    if (procs->pids[i] == 0)
+    {
+      proc->pid = i;
+      procs->lastpid = i;
+      lock_release(proclock);
+      return i;
+    }
+    
+    if (i == PID_MAX)
+    {
+      i = PID_MIN - 1;
+      continue;
+    }
+    
+    if (i == procs->lastpid)
+    {
+      lock_release(proclock);
+      return ENPROC;
+    }
+  }
+}
+
 void delete_procs(procs * procs)
 {
   kfree(procs->pids);
@@ -211,17 +239,21 @@ void proc_destroy(struct proc * proc)
 	threadarray_cleanup(&proc->p_threads);
 	spinlock_cleanup(&proc->p_lock);
 
+  lock_acquire(proclock);
+  procs->pids[proc->pid] = 0; // update available PIDs
+  lock_release(proclock);
 	kfree(proc->p_name);
 	kfree(proc);
 
 #ifdef UW
 	/* decrement the process count */
-        /* note: kproc is not included in the process count, but proc_destroy
+        /* note: kproc is not included as_copy in the process count, but proc_destroy
 	   is never called on kproc (see KASSERT above), so we're OK to decrement
 	   the proc_count unconditionally here */
 	P(proc_count_mutex);
 	KASSERT(proc_count > 0);
 	proc_count--;
+
 	/* signal the kernel menu thread if the process count has reached zero */
 	if (proc_count == 0)
 	{
@@ -320,6 +352,7 @@ struct proc * proc_create_runprogram(const char * name)
            are created using a call to proc_create_runprogram  */
 	P(proc_count_mutex);
 	proc_count++;
+	assignpid(struct proc * proc);
 	V(proc_count_mutex);
 #endif // UW
 

+ 0 - 1
kern/syscall/proc_syscalls.c

@@ -59,7 +59,6 @@ int sys_getpid(pid_t * retval)
 }
 
 /* stub handler for waitpid() system call                */
-
 int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval)
 {
   int exitstatus;