/* 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) { printf("Process %d: ",i); printRequest(request); /*if (stateSafeCheck() == 0) { return 0; }*/ int j; for (j = 0; j < col; j++) { // Step 1. if (request[j] > s->need[i][j]) { //ERROR printf("Request can't be granted, because the process has exceeded its maximum claim\n"); return 0; } } for (j = 0; j < col; j++) { // Step 2. while (request[j] > s->available[j]) { //ERROR printf("No resources are available, therefore the process must wait!\n"); sleep(100); } } pthread_mutex_lock(&lock); // Lock the mutex. for (j = 0; j < col; j += 1) { // Step 3. s->available[j] -= request[j]; s->allocation[i][j] += request[j]; s->need[i][j] -= request[j]; } if (stateSafeCheck() == 0) { for (j = 0; j < col; j += 1) { s->available[j] += request[j]; s->allocation[i][j] -= request[j]; s->need[i][j] += request[j]; } pthread_mutex_unlock(&lock); // Unlock the mutex. return 0; } pthread_mutex_unlock(&lock); // Unlock the mutex. printAvailable(); // Check if the resulting state is safe, return 1 if true, else 0. return 1; }
int requestResource(int processID,int requestVector[]) { //whether request number of resources is greater than needed if (ifGreaterThanNeed(processID,requestVector) == -1) { printf("requested resources is bigger than needed.\n"); return -1; } printf("Requested resources are not more than needed.\nPretend to allocate...\n"); //whether request number of resources is greater than needed if(ifEnoughToAlloc(requestVector) == -1) { printf("There is not enough resources for this process.\n"); return -1; } //pretend allocated for (i = 0; i < resourceTypeQuan; ++i) { needMatrix[processID][i] -= requestVector[i]; allocMatrix[processID][i] += requestVector[i]; availResourceVector[i] -= requestVector[i]; } printf("Checking if it is still safe...\n"); //check if still in safe status if (ifInSafeMode() == 0) { printf("Safe. Allocated successfully.\nNow available resources vector is:\n"); printAvailable(); printf("Now allocated matrix is:\n"); printAllocMatrix(); printf("Now need matrix is:\n"); printNeedMatrix(); return 0; } else { printf("It is not safe. Rolling back.\n"); for (i = 0; i < resourceTypeQuan; ++i) { needMatrix[processID][i] += requestVector[i]; allocMatrix[processID][i] -= requestVector[i]; availResourceVector[i] += requestVector[i]; } printf("Rolled back successfully.\n"); return -1; } }
int main(int argc, char const *argv[]) { if(argc != resourceTypeQuan + 1) { printf("Quantity of parameter is not correct.\n"); return -1; } for(i = 0; i < resourceTypeQuan; i++) { initResourceVector[i] = atoi(argv[i+1]);//argv[0] is name of program availResourceVector[i] = initResourceVector[i]; } //initialize needMatrix for (i = 0; i < processQuan; ++i) { for (j = 0; j < resourceTypeQuan; ++j) { needMatrix[i][j] = maxMatrix[i][j] - allocMatrix[i][j]; } } printf("Available resources vector is:\n"); printAvailable(); printf("Initial allocation matrix is:\n"); printAllocMatrix(); printf("Initial need matrix is:\n"); printNeedMatrix(); pthread_mutex_init(&mutex,NULL);//declared at head of code pthread_attr_t attrDefault; pthread_attr_init(&attrDefault); pthread_t *tid = malloc(sizeof(pthread_t) * processQuan); int *pid = malloc(sizeof(int) * processQuan);//customer's ID, for banker's algorithm, not pthread //initialize pid and create threads for(i = 0; i < processQuan; i++) { *(pid + i) = i; pthread_create((tid+i), &attrDefault, customer, (pid+i)); } //join threads for(i = 0; i < processQuan; i++) { pthread_join(*(tid+i),NULL); } return 0; }
/* Release the resources in request for process i */ void resource_release(int i, int *request) { printf("RELEASING RESOURCES!\n"); printRequest(request); int j; pthread_mutex_lock(&lock); // Lock the mutex. for (j = 0; j < col; j++) { s->available[j] += request[j]; s->allocation[i][j] -= request[j]; s->need[i][j] += request[j]; } pthread_mutex_unlock(&lock); // Unlock the mutex. printAvailable(); }
void clone(){ int ii=0, jj=0, kk=0, mm=0; printAvailable(); printf("Clone from detector number ? e.g. 89 ( < 0 to quit)\n"); ii = scan2int (); if (ii < 0) return; printInactive(); printf("to slot ? e.g. 2 ( < 0 to quit) \n"); jj = scan2int (); if (jj < 0) return; printf("to channel ? e.g. 21 ( < 0 to quit) \n"); kk = scan2int (); if (kk < 0) return; printf("Will you want detector %i removed from the set-up (HV off) ? 1 = yes 0 = no \n",ii); mm = scan2int (); if (mm == 1) { printf("Changing slot and channel assignment to detector %i \n",ii); // hvOff(ii); //hvptr->caenCrate[hvptr->xx[ii].slot] -= pow(2,hvptr->xx[ii].chan); // remove old occupied channel bit hvptr->xx[ii].slot = jj; // set new slot hvptr->xx[ii].chan = kk; // set new channel //hvptr->caenCrate[jj] += pow(2,kk); // add new occupied channel bit printf("Go to channel mode to turn on HV \n"); // maxchan does not change } else if (mm == 0) { hvptr->xx[hvptr->maxchan] = hvptr->xx[ii]; // transfer the data hvptr->xx[hvptr->maxchan].slot = jj; // correct to new slot hvptr->xx[hvptr->maxchan].chan = kk; // correct new channel //hvptr->caenCrate[jj] += pow(2,kk); // add new occupied channel bit hvptr->maxchan++; // increment maxchan printf("Go to channel mode to turn on HV and change appropriate values such as detector name \n"); } else printf("Neither 0 nor 1...doing nothing.. \n"); return; }
int releaseResource(int processID,int releaseVector[]) { if(ifEnoughToRelease(processID,releaseVector) == -1) { printf("The process do not own enough resources to release.\n"); return -1; } //enough to release for(i = 0; i < resourceTypeQuan; i++) { allocMatrix[processID][i] -= releaseVector[i]; needMatrix[processID][i] += releaseVector[i]; availResourceVector[i] += releaseVector[i]; } printf("Release successfully.\nNow available resources vector is:\n"); printAvailable(); printf("Now allocated matrix is:\n"); printAllocMatrix(); printf("Now need matrix is:\n"); printNeedMatrix(); return 0; }
int main(int argc, char **argv){ /* */ //int ii=0;// jj=0; int ans=0; int mapHVmon; // pid_t pid; /* printf("Working directory: %s\n",getcwd(path,PATH_MAX+1)); strcpy(mpod_mib,path); // copy path to mpod mib file strcat(mpod_mib,"/"); // add back the ending directory slash strcat(mpod_mib,MPOD_MIB); // tack on the mib file location printf(" mpod_mib file: %s\n",mpod_mib); */ /* Memory map creation and attachment the segment number is stored in hvptr->pid */ mapHVmon = mmapSetup(); if (mapHVmon == -1) return 0; /* Scroll through options */ while (ans != 100){ menu1(); // scanf ("%i", &ans); // read in ans (pointer is indicated) ans = scan2int (); switch (ans){ case 0: // end program but not kelvin ans = 100; break; case 1: // display temps and limits do not communicate to kelvin printOut(); break; case 2: // display temps and limits printOut(); hvptr->com0 = 2; // command (3) stored in SHM so hvmon can do something printOut(); break; case 3: // display temps and limits printOut(); hvptr->com0 = 3; // command (3) stored in SHM so hvmon can do something printOut(); break; case 4: // display temps and limits printf("HV on...\n"); hvptr->com0 = 4; kill(hvptr->pid,SIGALRM); //send to hvmon for action /*char cmdResult[140]="\0"; int onOff=100; snmp(0,0,"outputSwitch.u300",cmdResult); onOff = readOnOff(cmdResult); printf("%s :cmd, %i : onOFF", cmdResult, onOff); */ //mpodOnOff(1); break; case 5: // display temps and limits printf("HV off...\n"); //mpodOnOff(0); hvptr->com0 = 5; kill(hvptr->pid,SIGALRM); //send to hvmon for action break; case 6: // Save Config File printf("Saving file...\n"); openSaveFile(); fprintOut(); fclose(fileSave); printf("file is saved ...\n"); break; case 7: printf("N/A\n"); //hvptr->com0 = 7; // command (3) stored in SHM so kelvin can do something //kill(hvptr->pid,SIGALRM); // send an alarm to let kelvin know it needs to do command (1) //printf("Recover setup from hardware ...\n"); //sleep(1); break; case 8: // display temps and limits printActive(); break; case 9: printInactive(); break; case 11: // display temps and limits printf("Clone channel parameters...\n"); clone(); break; /* case 14: // display temps and limits printf("Channel HV on...\n"); printInactive(); printf("which channel to turn on?"); ii = scan2int(); hvptr->xx[ii].onoff = 1; hvptr->com0=14; kill(hvptr->pid,SIGALRM); //send to hvmon for action break; case 15: // display temps and limits printf("Channel HV off...\n"); printActive(); printf("which channel to turn off?"); ii = scan2int(); hvptr->xx[ii].onoff = 0; hvptr->com0=15; kill(hvptr->pid,SIGALRM); //send to hvmon for action break; */ case 16: // display temps and limits printf ("Alter parameters.\n"); printAvailable(); detParam(); hvptr->com0=16; kill(hvptr->pid,SIGALRM); // send an alarm to let hvmon know it needs to do command (1) break; case 17: printf("N/A Load config from file...\n"); break; case 18: printf("Reseting.....\n"); ResetAllChan(); break; case 19: printf("Configuration:\n"); printOut(); break; case 100: // End ALL kelvin programs by breaking out of while statement hvptr->com0=-1; // send command to hvmon to break out of its while command hvptr->com1=-1; // send command to hvmon-log to break out of its while command hvptr->com2=-1; // send command to hvmon-daq to break out of its while command kill(hvptr->pid,SIGALRM); break; default: // Do nothing and go back to the list of options ans = 0; break; } } /* Release the shared memory and close the U3 */ if (munmap(hvptr, sizeof (struct hvmon*)) == -1) { perror("Error un-mmapping the file"); /* Decide here whether to close(fd) and exit() or not. Depends... */ } close(mapHVmon); return 0; }
int main(int argc, char* argv[]) { // Initialize the mutex lock. pthread_mutex_init(&lock,NULL); /* Get size of current state as input */ int i, j; printf("Number of processes: \n"); scanf("%d", &row); printf("Number of resources: \n"); scanf("%d", &col); printf("\tFINISHED LOADING IN NB PROCESSES AND RESOURCES\n"); /* Allocate memory for state */ s = (State *) malloc(sizeof(State)); s->resource = (int *) malloc(col*sizeof(int)); s->available = (int *) malloc(col*sizeof(int)); s->max = (int **) malloc(row*sizeof(int)); s->allocation = (int **) malloc(row*sizeof(int)); s->need = (int **) malloc(row*sizeof(int)); if (s == NULL) { printf("\nYou need to allocate memory for the state!\n"); exit(0); }; printf("\tFINISHED ALLOCATING SPACE FOR STATE\n"); /* Get current state as input */ printf("Resource vector: \n"); for(i = 0; i < col; i++) scanf("%d", &s->resource[i]); printf("Enter max matrix: \n"); for(i = 0;i < row; i++) { // Allocate the row s->max[i] = (int *) malloc(col*sizeof(int)); for(j = 0;j < col; j++) { scanf("%d", &s->max[i][j]); } } printf("Enter allocation matrix: \n"); for(i = 0; i < row; i++) { // Allocate the row s->allocation[i] = (int *) malloc(col*sizeof(int)); for(j = 0; j < col; j++) { scanf("%d", &s->allocation[i][j]); } } printf("\n"); printf("\tFINISHED LOADING VECTORS AND MATRIXES\n"); /* Calcuate the need matrix */ for(i = 0; i < row; i++) { // Allocate the row s->need[i] = (int *) malloc(col*sizeof(int)); for(j = 0; j < col; j++) { s->need[i][j] = s->max[i][j] - s->allocation[i][j]; } } /* Calcuate the availability vector */ for(j = 0; j < col; j++) { int sum = 0; for(i = 0; i < row; i++) sum += s->allocation[i][j]; s->available[j] = s->resource[j] - sum; } /* Output need matrix and availability vector */ printf("Need matrix:\n"); for(i = 0; i < col; i++) printf("R%d ", i+1); printf("\n"); for(i = 0; i < row; i++) { for(j = 0; j < col; j++) printf("%d ",s->need[i][j]); printf("\n"); } printAvailable(); /* If initial state is unsafe then terminate with error */ int safe = stateSafeCheck(); if (safe == 0) { return 1; } /* Seed the random number generator */ struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_usec); /* Create row threads */ pthread_t *tid = malloc(row*sizeof(pthread_t)); for (i = 0; i < row; i++) pthread_create(&tid[i], NULL, process_thread, (void *) (long) i); /* Wait for threads to finish */ pthread_exit(0); free(tid); /* Free state memory */ free(s->resource); free(s->available); free(s->max); free(s->allocation); free(s->need); free(s); }