Advertisement
Guest User

C-thread allocate_PID

a guest
Apr 19th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. int
  2. allocate_PID(int t_id)
  3. {
  4.     int next_pid;
  5.     int i;
  6.     int pid = -1;
  7.  
  8.     pthread_mutex_lock(&mutex);
  9.     //Test for available resources
  10.     if (S<=0) {
  11.         goto end;
  12.     }
  13.    
  14.     for (i =0;i<PID_MAX-PID_MIN+1;i++) {
  15.         if (pid_map[i] == 0){
  16.             S--;
  17.             printf("Thread number %d got PID %d S=%d\n", t_id, i+PID_MIN, S);
  18.             pid_map[i] = 1;
  19.             next_pid   = i;
  20.             pid        = next_pid + PID_MIN;
  21.             goto end;
  22.         }
  23.     }
  24.     //for debug purposes:
  25.     //if a thread will pass mutex and S-semaphore
  26.     //but won't find a PID then -999 will be returned
  27.     pid = -999;
  28. end:
  29.     pthread_mutex_unlock(&mutex);
  30.     return pid;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement