Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int
- allocate_PID(int t_id)
- {
- int i;
- int pid = -1;
- pthread_mutex_lock(&mutex);
- //Test for available resources
- if (S<=0) {
- goto end;
- }
- for (i =0;i<PID_MAX-PID_MIN+1;i++) {
- if (pid_map[i] == 0){
- S--;
- printf("Thread number %d got PID %d S=%d\n", t_id, i+PID_MIN, S);
- pid_map[i] = 1;
- pid = i + PID_MIN;
- goto end;
- }
- }
- //for debug purposes:
- //if a thread will pass mutex and S-semaphore
- //but won't find a PID then -999 will be returned
- pid = -999;
- end:
- pthread_mutex_unlock(&mutex);
- return pid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement