Beispiel #1
0
/* 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);
    
    if(islarger(request, s->need[i], n)) {
        printf("¡¡¡ERROR!!!\n");
        printf("Process %i's request is greather than its need.\n", i);
        killProcesses();
    }
    
    subArr(s->available, request, n);
    addArr(s->allocation[i], request, n);
    subArr(s->need[i], request, n);
    
    if (safe_state(s)) {
        pthread_mutex_unlock(&state_mutex);
        return 1;
    } else {
        addArr(s->available, request, n);
        subArr(s->allocation[i], request, n);
        addArr(s->need[i], request, n);
        pthread_mutex_unlock(&state_mutex);
        return 0;
    }
}
Beispiel #2
0
/* Release the resources in request for process i */
void resource_release(int i, int *request)
{
    pthread_mutex_lock(&state_mutex);
    
    if(islarger(request, s->allocation[i], n)) {
        printf("¡¡¡ERROR!!!\n");
        printf("Process %i tries to release more resources than it has allocated.\n", i);
        killProcesses();
    }
    
    addArr(s->available, request, n);
    subArr(s->allocation[i], request, n);
    addArr(s->need[i], request, n);

    pthread_mutex_unlock(&state_mutex);
}
Beispiel #3
0
/*Checks if a given state is safe.*/
int safe_state(State *s) {
    int count = m, i, isSafe;
    int *finish = (int *)malloc(n * sizeof(int));
    int *work = (int *)malloc(m * sizeof(int));
    for (i = 0; i < m; i++) {
        finish[i] = 0;
        work[i] = s->available[i];
    }
    
    while (count != 0) {
        isSafe = 0;
        for (i = 0; i < m; i++) {
            if (!finish[i]) {
                if (islarger(work, s->need[i], n)) {
                    finish[i] = 1;
                    count--;
                    isSafe = 1;
                    addArr(work, s->allocation[i],n);
                    break;
                }
            }
        }
        if (!isSafe) {
            printf("The requested state is unsafe.\n");
            return 0;
        }
    }
    printf("The requested state is safe.\n");
    return 1;
}
Beispiel #4
0
int *algorithm_1(int *arr, int *outputArr, int size, int value) {
    
    int i, k, *tempArr1, *tempArr2, *tempArr3;
    k = value;
    
    if (k == 1) {
        outputArr[0]++;
        return outputArr;
    }
    else if ((i = isCoin(arr, size, k))) {
        outputArr[i]++;
        return outputArr;
    }
    else {
        //loop through every integer from 1 to "value"
        for (i = 1; i < k; i++) {
            
            //create and initilize three temprorary arrays of size "size" to all 0's
            tempArr1 = createArr(size);
            tempArr2 = createArr(size);
            tempArr3 = createArr(size);
            initArr(tempArr1, size);
            initArr(tempArr2, size);
            initArr(tempArr3, size);
            
            //recursively call algorithm_1 on array from 1 -> i and i + 1 -> "value"
            tempArr1 = algorithm_1(arr, tempArr1, size, i);
            tempArr2 = algorithm_1(arr, tempArr2, size, k - i);
            
            //add the sum of all elements at each index of tempArr1 and temArr2 together, assign to tempArr3
            addArr(tempArr1, tempArr2, tempArr3, size);
            
            //if this new sum of elements array (tempArr3) is less than the running min (outputArr) or the current output array is not a solution, then replace outputArr with tempArr3
            if ((sumArray(tempArr3, size) < sumArray(outputArr, size)) || !coinCheck(outputArr, arr, size, k)) {
                copyArr(tempArr3, outputArr, size);
            }
            
            free(tempArr1);
            free(tempArr2);
            free(tempArr3);
        }
    }
    
    return outputArr;
    
}