OneCompiler

os8

106

Q1)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_BLOCKS 100

int disk[MAX_BLOCKS]; // Disk with maximum number of blocks
int freeBlocks[MAX_BLOCKS]; // Array to maintain the list of free blocks
int n; // Number of blocks in the disk

void initializeDisk() {
for (int i = 0; i < n; i++) {
disk[i] = -1; // Mark all blocks as free initially
freeBlocks[i] = i; // Maintain the list of free blocks
}
}

void showBitVector() {
printf("Bit Vector:\n");
for (int i = 0; i < n; i++) {
if (disk[i] == -1) {
printf("0 ");
} else {
printf("1 ");
}
}
printf("\n");
}

void createNewFile() {
srand(time(0)); // Seed for random number generation
int fileStart = rand() % n; // Randomly select a starting block for the new file
int fileSize = rand() % (n - fileStart) + 1; // Randomly select the file size

// Allocate blocks for the file
for (int i = fileStart; i < fileStart + fileSize; i++) {
    disk[i] = 1; // Mark the block as allocated
    for (int j = 0; j < n; j++) {
        if (freeBlocks[j] == i) {
            freeBlocks[j] = -1; // Remove the allocated block from the list of free blocks
            break;
        }
    }
}
printf("New file created starting from block %d with size %d\n", fileStart, fileSize);

}

void showDirectory() {
printf("Directory:\n");
printf("Start Block\tSize\n");
int fileStart = -1;
int fileSize = 0;
for (int i = 0; i < n; i++) {
if (disk[i] == 1 && fileStart == -1) {
fileStart = i;
fileSize++;
} else if (disk[i] == 1 && fileStart != -1) {
fileSize++;
} else if (disk[i] == -1 && fileStart != -1) {
printf("%d\t\t%d\n", fileStart, fileSize);
fileStart = -1;
fileSize = 0;
}
}
if (fileStart != -1) {
printf("%d\t\t%d\n", fileStart, fileSize);
}
}

int main() {
printf("Enter the number of blocks in the disk: ");
scanf("%d", &n);

initializeDisk();

char choice;
do {
    printf("\nMenu:\n");
    printf("a) Show Bit Vector\n");
    printf("b) Create New File\n");
    printf("c) Show Directory\n");
    printf("d) Exit\n");
    printf("Enter your choice: ");
    scanf(" %c", &choice);

    switch(choice) {
        case 'a':
            showBitVector();
            break;
        case 'b':
            createNewFile();
            break;
        case 'c':
            showDirectory();
            break;
        case 'd':
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice!\n");
    }
} while (choice != 'd');

return 0;

}
Q2)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void sstf(int requests[], int n, int start) {
int totalHeadMovements = 0;
int currentHead = start;
int minSeekTime, minIndex;

printf("Head Movement Sequence: %d", currentHead);

for (int i = 0; i < n; i++) {
    minSeekTime = INT_MAX;
    minIndex = -1;
    for (int j = 0; j < n; j++) {
        if (requests[j] != -1 && abs(requests[j] - currentHead) < minSeekTime) {
            minSeekTime = abs(requests[j] - currentHead);
            minIndex = j;
        }
    }
    if (minIndex != -1) {
        totalHeadMovements += minSeekTime;
        currentHead = requests[minIndex];
        printf(" -> %d", currentHead);
        requests[minIndex] = -1;
    }
}

printf("\nTotal Head Movements: %d\n", totalHeadMovements);

}

int main() {
int n;
printf("Enter the number of disk requests: ");
scanf("%d", &n);

int requests[n];
printf("Enter the disk request string: ");
for (int i = 0; i < n; i++) {
    scanf("%d", &requests[i]);
}

int start;
printf("Enter the starting head position: ");
scanf("%d", &start);

sstf(requests, n, start);

return 0;

}