os25
Q1)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to sort the disk requests in ascending order
void sortRequests(int *requests, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
}
// Function to find the index of the first request greater than or equal to head position
int findStartIndex(int *requests, int n, int head) {
int start = 0;
for (int i = 0; i < n; i++) {
if (requests[i] >= head) {
start = i;
break;
}
}
return start;
}
// Function to calculate total head movements for LOOK algorithm
int LOOK(int *requests, int n, int head, bool direction) {
sortRequests(requests, n);
int start = findStartIndex(requests, n, head);
int totalHeadMovement = 0;
int current = head;
int index = start;
while (index < n) {
    totalHeadMovement += abs(requests[index] - current);
    current = requests[index];
    index++;
}
if (direction) {
    totalHeadMovement += abs(current - requests[start - 1]);
    current = requests[start - 1];
    for (int i = start - 2; i >= 0; i--) {
        totalHeadMovement += abs(current - requests[i]);
        current = requests[i];
    }
}
return totalHeadMovement;
}
int main() {
int n;
printf("Enter the total number of disk blocks: ");
scanf("%d", &n);
int *requests = (int *)malloc(n * sizeof(int));
if (requests == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}
printf("Enter the disk request string: ");
for (int i = 0; i < n; i++) {
    scanf("%d", &requests[i]);
}
int head;
printf("Enter the starting head position: ");
scanf("%d", &head);
printf("Enter the direction (0 for left, 1 for right): ");
bool direction;
scanf("%d", &direction);
int totalHeadMovement = LOOK(requests, n, head, direction);
printf("Total head movements: %d\n", totalHeadMovement);
free(requests);
return 0;
}
Q2)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to initialize the disk blocks as free
void initializeDiskBlocks(bool *diskBlocks, int n) {
for (int i = 0; i < n; i++) {
diskBlocks[i] = false;
}
}
// Function to display the bit vector representing the disk blocks
void showBitVector(bool *diskBlocks, int n) {
printf("Bit Vector: ");
for (int i = 0; i < n; i++) {
printf("%d ", diskBlocks[i] ? 1 : 0);
}
printf("\n");
}
// Function to create a new file and allocate blocks
void createNewFile(bool *diskBlocks, int n, int fileBlocks) {
printf("Creating new file...\n");
int count = 0;
for (int i = 0; i < n && count < fileBlocks; i++) {
if (!diskBlocks[i]) {
diskBlocks[i] = true;
count++;
}
}
printf("File created successfully.\n");
}
// Function to show the directory containing allocated files
void showDirectory(bool *diskBlocks, int n) {
printf("Directory:\n");
for (int i = 0; i < n; i++) {
if (diskBlocks[i]) {
printf("Block %d: Allocated\n", i);
} else {
printf("Block %d: Free\n", i);
}
}
}
int main() {
int n;
printf("Enter the total number of disk blocks: ");
scanf("%d", &n);
bool *diskBlocks = (bool *)malloc(n * sizeof(bool));
if (diskBlocks == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}
initializeDiskBlocks(diskBlocks, n);
char choice;
do {
    printf("\nMenu:\n");
    printf("1. Show Bit Vector\n");
    printf("2. Create New File\n");
    printf("3. Show Directory\n");
    printf("4. Exit\n");
    printf("Enter your choice: ");
    scanf(" %c", &choice);
    switch (choice) {
        case '1':
            showBitVector(diskBlocks, n);
            break;
        case '2':
            {
                int fileBlocks;
                printf("Enter the number of blocks for the new file: ");
                scanf("%d", &fileBlocks);
                createNewFile(diskBlocks, n, fileBlocks);
            }
            break;
        case '3':
            showDirectory(diskBlocks, n);
            break;
        case '4':
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice. Please try again.\n");
    }
} while (choice != '4');
free(diskBlocks);
return 0;
}