davidbejenariu2

Untitled

Jan 8th, 2022
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <sys/queue.h>
  2.  
  3.  
  4. struct myprocess {
  5.     pid_t id;                       // process id
  6.     int waiting;                    // process status: 0 - not waiting, != 0 - waiting
  7.    
  8.     LIST_ENTRY(myprocess) link;     // link to next process in process list
  9. };
  10.  
  11. struct mymutex {
  12.     int d;                          // mutex descriptor
  13.     char *name;                     // mutex name
  14.     int is_locked;                  // mutex status: 0 - not waiting, != 0 - waiting
  15.     LIST_HEAD(, myprocess) plist;   // process list
  16.    
  17.     LIST_ENTRY(mymutex) link;       // link to next mutex in mutex list
  18. }
  19.  
  20.  
  21. LIST_HEAD(, mymutex) mtxlist;       // mutex list;
  22. int mtxlist_is_init;                // mutex list status: 0 - not initialized, != 0 - initialized
  23.  
  24. int t_pause;                        // wait channel (the resource for which the processes need to wait)
  25. pid_t wake_pid;                     // id of currently granted process
  26.  
  27.  
  28. int
  29. sys_mtxlock(struct proc *p, void *v, register_t *retval) {
  30.     int error = 0;
  31.    
  32.     if (mtxlist_is_init == 0) {
  33.         LIST_INIT(&mtxlist);
  34.         mtxlist_is_init = 1;
  35.     }
  36.    
  37.     struct sys_mtxlock_args *uap = v;
  38.     int d = SCARG(uap, d);
  39.    
  40.     struct mymutex *mtx_found = NULL;
  41.     struct mymutex *mtx_it;
  42.    
  43.     LIST_FOREACH(mtx_it, &mtxlist, link) {
  44.         if (mtx_it->d == d) {
  45.             mtx_found = it;
  46.             break;
  47.         }
  48.     }
  49.    
  50.     if (mtx_found == NULL) {
  51.         printf("LOCK: there is no mutex having descriptor %d\n", d);
  52.        
  53.         *retval = -1;
  54.         return 0;
  55.     }
  56.    
  57.     printf("LOCK: mutex to be locked name %s, descriptor %d\n", mtx_found->name, mtx_found->d);
  58.  
  59.     struct myprocess *proc_found = NULL;
  60.     struct myprocess *proc_it;
  61.    
  62.     LIST_FOREACH(proc_it, &(mtx_found->plist), link) {
  63.         if (proc_it->id == p->p_tid) {
  64.             proc_found = proc_it;
  65.             break;
  66.         }
  67.     }
  68.  
  69.     if (proc_found == NULL) {
  70.         printf("LOCK: proc %d has not opened mutex name %s\n", p->p_tid, mtx_found->name);
  71.        
  72.         *retval = -1;
  73.         return 0;
  74.     }
  75.  
  76.     printf("LOCK: proc %d start waiting mutex %s\n", proc_found->id, mtx_found->name);
  77.  
  78.     proc_found->waiting = 1;
  79.  
  80.     // should check mutex not in use (is_locked == 0) to skip sleep
  81.  
  82.     sleep:
  83.         error = tsleep(&t_pause, PSOCK | PCATCH, "awaiting lock", 0);
  84.  
  85.         if (error) {
  86.             proc_found->waiting = 0;
  87.            
  88.             *retval = -1;
  89.             return error;
  90.         }
  91.  
  92.         if (wake_pid != p->p_tid) {
  93.             goto sleep;
  94.         }
  95.  
  96.     wake_pid = 0;
  97.     proc_found->waiting = 0;
  98.  
  99.     *retval = 0;
  100.     return 0;
  101. }
  102.  
  103. int
  104. sys_mtxunlock(struct proc *p, void *v, register_t *retval)
  105. {
  106.     if (mtxlist_is_init == 0) {
  107.         LIST_INIT(&mtxlist);
  108.         mtxlist_is_init = 1;
  109.     }
  110.    
  111.     struct sys_mtxclose_args *uap = v;
  112.     int d = SCARG(uap, d);
  113.    
  114.     struct mymutex *mtx_it;
  115.     // struct myprocess *proc_it, *proc_found = NULL;
  116.    
  117.     LIST_FOREACH(mtx_it, &mtxlist, link) {
  118.         if (mtx_it->d == d) {
  119.             // should check mutex d actually opened by proc p?
  120.             /*LIST_FOREACH(proc_it, &(mtx_it->plist), link) {
  121.                 if (proc_it->id == p->p_tid) {
  122.                     proc_found = proc_it;
  123.                     break;
  124.                 }
  125.             }
  126.            
  127.             if (proc_found == NULL) {
  128.                 printf("UNLOCK: proc %d has not opened mutex name %s\n", p->p_tid, mtx_found->name");
  129.                
  130.                 *retval = -1;
  131.                 return 0;
  132.             }*/
  133.            
  134.             mtx_it->is_locked = 0;
  135.             break;
  136.         }
  137.     }
  138.  
  139.     *retval = 0;
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment