Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. /*
  2.  * Process ID management
  3.  * $Rev: 9 $$Date: 2009-09-30 20:09:09 +0200 (Mi, 30 Sep 2009) $
  4.  */
  5.  
  6. #include <types.h>
  7. #include <kern/errno.h>
  8. #include <kern/wait.h>
  9. #include <limits.h>
  10. #include <lib.h>
  11. #include <array.h>
  12. #include <clock.h>
  13. #include <thread.h>
  14. #include <current.h>
  15. #include <synch.h>
  16. #include <pid.h>
  17.  
  18.  
  19. static struct pidinfo *p_table[PROC_MAX];
  20. static struct lock *testlock;
  21. static struct cv *cv;
  22. volatile int counter;       // counts the amount of given threads
  23. volatile int thread_count;  // Counts the amount of threads in the table
  24.  
  25. /*
  26.  * pid_disown - disown any interest in waiting for a child's exit
  27.  * status.
  28.  */
  29. void
  30. pid_disown(pid_t theirpid)
  31. {
  32.     lock_acquire(testlock);
  33.     p_table[pid_hash(theirpid)]->interested = false;
  34.     lock_release(testlock);
  35. }
  36.  
  37. /*
  38.  * Waits on a pid, returning the exit status when it's available.
  39.  *
  40.  * status may be null, in which case the status is thrown away.
  41.  */
  42. int
  43. pid_wait(pid_t theirpid, int *status, int flags, pid_t *ret)
  44. {
  45.     (void) flags;
  46.     (void) ret;
  47.    
  48.    
  49.     lock_acquire(testlock);
  50.  
  51.     if(p_table[pid_hash(theirpid)] != NULL){ // Existiert das Kind noch?
  52.         if(p_table[pid_hash(theirpid)]->interested==true ){ //Besteht Interesse?
  53.             if(p_table[pid_hash(curthread->th_pid)]->children == true){ // Hat der Thread ueberhaupt ein kind?
  54.                 while(p_table[pid_hash(theirpid)]->exitstatus == 1){
  55.                     cv_wait(cv, testlock);
  56.                 }
  57.                 /*Exitstatus == 0 bzw. obrige Bedingung ( exitstatus = 1 wird zu 0 )*/
  58.                 *status = p_table[pid_hash(theirpid)]->exitstatus;
  59.                 pid_unalloc(theirpid); // Child PID wird gelöscht
  60.             }
  61.             else {
  62.                 lock_release(testlock);
  63.                 return ECHILD; /* No child processes */
  64.             }
  65.         }
  66.  
  67.     }
  68.     else{
  69.         lock_release(testlock);
  70.         return -1;
  71.     }
  72.  
  73.     lock_release(testlock);
  74.     return 0;
  75. }
  76.  
  77. void
  78. pid_bootstrap(void)
  79. {
  80.     struct pidinfo *pidinfo;
  81.     counter = 2;
  82.     thread_count = 1;
  83.  
  84.     if (cv==NULL){
  85.         cv = cv_create("testlock");
  86.         if (cv == NULL) {
  87.             panic("pid: cv_create failed\n");
  88.         }
  89.     }
  90.  
  91.     if (testlock==NULL){
  92.         testlock = lock_create("testlock");
  93.         if (testlock == NULL) {
  94.             panic("pid: lock_create failed\n");
  95.         }
  96.     }
  97.  
  98.         pidinfo = kmalloc(sizeof(struct pidinfo));
  99.         if (pidinfo == NULL){
  100.                 return;
  101.         }
  102.  
  103.     for(int i = 0; i < PROC_MAX; i++){
  104.         p_table[i] = NULL;
  105.     }
  106.    
  107.     curthread->th_pid = 1;
  108.     p_table[1] = pidinfo;
  109.     p_table[1]->pid = BOOTUP_PID;
  110.     p_table[1]->ppid = INVALID_PID;
  111.     p_table[1]->children = false;
  112.     p_table[1]->exitstatus = 0;
  113. }
  114.  
  115. int
  116. pid_alloc(pid_t *retval)
  117. {
  118.    
  119.    
  120.     if(thread_count == PROC_MAX){
  121.         return ENPROC; /* Too many processes in system */
  122.     }
  123.  
  124.     struct pidinfo *pidinfo;
  125.  
  126.         pidinfo = kmalloc(sizeof(struct pidinfo));
  127.         if (pidinfo == NULL) {
  128.                 return 0;
  129.         }
  130.     lock_acquire(testlock); // Da der counter abgefragt wird, muss gelockt werden
  131.     while(p_table[pid_hash(counter)] != NULL){
  132.         if(counter == PID_MAX){
  133.             counter = PID_MIN;
  134.         }
  135.         else{
  136.             counter++;
  137.         }
  138.     }
  139.     ++thread_count;
  140.     int value = pid_hash(counter);
  141.     p_table[value] = pidinfo;
  142.     p_table[value]->pid = counter;
  143.     p_table[value]->ppid = curthread->th_pid;
  144.     p_table[value]->children = false;
  145.     p_table[pid_hash(curthread->th_pid)]->children = true;
  146.     if(retval == NULL){
  147.         p_table[value]->interested = false;
  148.         p_table[value]->exitstatus = 1;
  149.     }
  150.     else{
  151.         p_table[value]->interested = true;
  152.         p_table[value]->exitstatus = 1;
  153.     }
  154.     *retval = p_table[value]->pid;
  155.     lock_release(testlock);
  156.     return 0;
  157. }
  158.  
  159. void
  160. pid_unalloc(pid_t targetpid)
  161. {
  162.  
  163.         if(p_table[pid_hash(p_table[pid_hash(curthread->th_pid)]->ppid)] == NULL &&
  164.     p_table[curthread->th_pid]->interested == false){
  165.                 int index = pid_hash(pid_hash(p_table[pid_hash(curthread->th_pid)]->ppid));
  166.                 kfree(p_table[pid_hash(p_table[pid_hash(curthread->th_pid)]->ppid)]);
  167.                 p_table[index] = NULL;
  168.                 --thread_count;
  169.         }
  170.         if(targetpid != 0){
  171.                 int index = pid_hash(targetpid);
  172.                 kfree(p_table[pid_hash(targetpid)]);
  173.                 p_table[index] = NULL;
  174.                 --thread_count;
  175.         }
  176. //  if(p_table[pid_hash(p_table[pid_hash(curthread->th_pid)]->ppid)] == NULL ){
  177. //      if((p_table[curthread->th_pid]->interested == false) || targetpid != 0){
  178. //          --thread_count;
  179. //          kfree(p_table[pid_hash(targetpid)]);
  180. //          p_table[pid_hash(targetpid)] = NULL;
  181. //      }
  182. //  }
  183. }
  184.  
  185. void
  186. pid_setexitstatus(int status)
  187. {
  188.  
  189.     lock_acquire(testlock);
  190.     if(status == 0){
  191.         p_table[pid_hash(curthread->th_pid)]->exitstatus = status;
  192.         cv_broadcast(cv, testlock);
  193.     }
  194.     else{
  195.         p_table[pid_hash(curthread->th_pid)]->exitstatus = status;
  196.     }
  197.     lock_release(testlock);
  198. }
  199.  
  200. int pid_hash(pid_t pid)
  201. {  
  202.     if(pid > PID_MAX){
  203.         panic("Error: PID is invalid! It's bigger than PID_MAX");
  204.     }
  205.  
  206.     pid=pid%PROC_MAX;
  207.     return pid;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement