bool inside_bounding_box(bounding_box bb, point p) { if (greaterOrEqual(p.x, bb.left.x) && lessOrEqual(p.x, bb.right.x) && greaterOrEqual(p.y, bb.left.y) && lessOrEqual(p.y, bb.right.y) && greaterOrEqual(p.z, bb.left.z) && lessOrEqual(p.z, bb.right.z)) return true; else return false; }
/* Allocate resources in request for process i, only if it results in a safe state and return 1, else return 0 */ int resource_request(int i, int *request) { pthread_mutex_lock(&state_mutex); int ret = 0; //Step 1 if(!lessOrEqual(request, s->need[i], n)){ exit (-1); } int pq = lessOrEqual(request, s->available, n); if(pq){ int j; for(j = 0; j < n; j++){ s->available[j] += -request[j]; s->allocation[i][j] += request[j]; s->need[i][j] += -request[j]; } int safe = safeState(); if(!safe){ //reverting for(j = 0; j < n; j++){ s->available[j] += request[j]; s->allocation[i][j] += -request[j]; s->need[i][j] += request[j]; } }else{ ret = 1; } } printf("%d\n", ret); print_available(); pthread_mutex_unlock(&state_mutex); return ret; }
int safeState(){ int work[n]; int finish[m]; //Step 1 int i, j; for(i = 0; i < n; i++){ work[i] = s->available[i]; } for(i = 0; i < m; i++){ finish[i] = 0; } int umod = 0; for(i = 0; umod < m; i = (i + 1) % m){ //Step 2 if(!finish[i] && lessOrEqual(s->need[i], work, n)) { //step 3 for(j = 0; j < n; j++){ work[j] += s->allocation[i][j]; } finish[i] = 1; umod = 0; } umod++; } //step 4 for(i = 0; i < m; i++) if(!finish[i]){ return 0; } return 1; }
bool greaterOrEqual(long double a, long double b) { return lessOrEqual(b, a); }