Advertisement
Guest User

C-thread allocate_PID 2

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