Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct schedulerResult process_SSTF_request(struct schedulerInput request){
- struct schedulerResult results;
- int size = request.requests.elements;
- results.requests.data = (int *)malloc(sizeof(int)*size);
- results.requests.elements = size;
- int head = request.startTrack;
- int seek_count = 0;
- int distance, cur_track;
- int location;
- for (int i = 0; i < size; i++) {
- location = nextTrack(request.requests.data, head, request.requests.elements);
- cur_track = request.requests.data[location];
- results.requests.data[i] = cur_track;
- // delete element at location
- request.requests.elements--;
- for (int j = location; j < request.requests.elements; j++){
- //printf("%d ", request.requests.data[j]);
- request.requests.data[j] = request.requests.data[j+1];
- }
- int* temp = realloc(request.requests.data, request.requests.elements * sizeof(int));
- request.requests.data = temp;
- //printf("Current Track: %d\n", cur_track);
- distance = abs(cur_track - head);
- seek_count += distance;
- head = cur_track;
- }
- printf("SSTF ->");
- results.totalHeadMovement = seek_count;
- return results;
- }
- struct schedulerResult process_SCAN_request(struct schedulerInput request){
- struct schedulerResult results;
- int seek_count = 0;
- int distance, cur_track;
- int size = request.requests.elements;
- int capacity = size;
- int* left = malloc(capacity * sizeof(int));
- int* right = malloc(capacity * sizeof(int));
- results.requests.data = (int *)malloc(sizeof(int)*size);
- results.requests.elements = size;
- int direction = request.direction;
- int head = request.startTrack;
- int lcount = 0;
- int rcount = 0;
- // appending end values
- // which has to be visited
- // before reversing the direction
- if (direction == -1)
- push(left, lcount, 0, &lcount, &capacity);
- else if (direction == 1)
- push(right, rcount, disk_size, &rcount, &capacity);
- // push the values to the right of the head to right arry and same for left
- for (int i = 0; i < size; i++) {
- if (request.requests.data[i] < head)
- push(left, lcount, request.requests.data[i], &lcount, &capacity);
- if (request.requests.data[i] > head)
- push(right, rcount, request.requests.data[i], &rcount, &capacity);
- }
- // sorting left and right vectors
- for (int i = 0; i < lcount; i++) {
- for (int j = 0; j < lcount; j++) {
- if (left[j] > left[i]) {
- int tmp = left[i];
- left[i] = left[j];
- left[j] = tmp;
- }
- }
- }
- for (int i = 0; i < rcount; i++) {
- for (int j = 0; j < rcount; j++) {
- if (right[j] > right[i]) {
- int tmp = right[i];
- right[i] = right[j];
- right[j] = tmp;
- }
- }
- }
- /*
- printf("Left: %d\n", lcount);
- for (int i =0; i < lcount; i++)
- printf("%d ", left[i]);
- printf("Right: %d\n", rcount);
- for (int i =0; i < rcount; i++)
- printf("%d ", right[i]);
- */
- // run the while loop two times.
- // one by one scanning right
- // and left of the head
- int run = 2;
- int count = 0;
- while (run--) {
- if (direction == -1) {
- for (int i = lcount - 1; i >= 0; i--) {
- cur_track = left[i];
- if (cur_track != 0){
- results.requests.data[count] = cur_track;
- count++;
- }
- else{
- if (head == 1)
- seek_count--;
- }
- // calculate absolute distance // seek time
- distance = abs(cur_track - head);
- seek_count += distance;
- head = cur_track;
- }
- direction = 1;
- }
- else if (direction == 1) {
- for (int i = 0; i < rcount; i++) {
- cur_track = right[i];
- if (cur_track != disk_size){
- results.requests.data[count] = cur_track;
- count++;
- }
- distance = abs(cur_track - head);
- seek_count += distance;
- head = cur_track;
- }
- direction = -1;
- }
- }
- free(left);
- free(right);
- printf("SCAN ->");
- results.totalHeadMovement = seek_count;
- return results;
- }
- struct schedulerResult process_C_SCAN_request(struct schedulerInput request){
- struct schedulerResult results;
- int seek_count = 0;
- int distance, cur_track;
- int size = request.requests.elements;
- int capacity = size;
- int* left = malloc(capacity * sizeof(int));
- int* right = malloc(capacity * sizeof(int));
- results.requests.data = (int *)malloc(sizeof(int)*size);
- results.requests.elements = size;
- int head = request.startTrack;
- int lcount = 0;
- int rcount = 0;
- // appending end values
- // which has to be visited
- // before reversing the direction
- // push the values to the right of the head to right arry and same for left
- for (int i = 0; i < size; i++) {
- if (request.requests.data[i] < head)
- push(left, lcount, request.requests.data[i], &lcount, &capacity);
- if (request.requests.data[i] > head)
- push(right, rcount, request.requests.data[i], &rcount, &capacity);
- }
- push(left, lcount, 0, &lcount, &capacity);
- push(right, rcount, disk_size, &rcount, &capacity);
- // sorting left and right vectors
- for (int i = 0; i < lcount; i++) {
- for (int j = 0; j < lcount; j++) {
- if (left[j] > left[i]) {
- int tmp = left[i];
- left[i] = left[j];
- left[j] = tmp;
- }
- }
- }
- for (int i = 0; i < rcount; i++) {
- for (int j = 0; j < rcount; j++) {
- if (right[j] > right[i]) {
- int tmp = right[i];
- right[i] = right[j];
- right[j] = tmp;
- }
- }
- }
- int count = 0;
- for (int i = 0; i < rcount; i++) {
- cur_track = right[i];
- if (cur_track != disk_size){
- results.requests.data[count] = cur_track;
- count++;
- }
- distance = abs(cur_track - head);
- seek_count += distance;
- head = cur_track;
- }
- head = 0;
- seek_count += disk_size;
- for (int i = 0; i < lcount; i++) {
- cur_track = left[i];
- if (cur_track != 0){
- results.requests.data[count] = cur_track;
- count++;
- }
- distance = abs(cur_track - head);
- seek_count += distance;
- head = cur_track;
- }
- free(left);
- free(right);
- printf("C-SCAN ->");
- results.totalHeadMovement = seek_count;
- return results;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement