OneCompiler

os15

117

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

#define MAX_BLOCKS 1000

void show_bit_vector(int disk[], int n);
void create_new_file(int disk[], int n);
void show_directory(int disk[], int n);

int main() {
int disk[MAX_BLOCKS];
int n;

printf("Enter the number of blocks on the disk: ");
scanf("%d", &n);

// Randomly mark some blocks as allocated
srand(time(NULL));
for (int i = 0; i < n; i++) {
    disk[i] = rand() % 2; // Randomly mark blocks as allocated (1) or free (0)
}

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':
            show_bit_vector(disk, n);
            break;
        case '2':
            create_new_file(disk, n);
            break;
        case '3':
            show_directory(disk, n);
            break;
        case '4':
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice! Please try again.\n");
    }
} while (choice != '4');

return 0;

}

void show_bit_vector(int disk[], int n) {
printf("Bit Vector:\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}

void create_new_file(int disk[], int n) {
int start, end;
printf("Enter the starting block for the new file: ");
scanf("%d", &start);
printf("Enter the ending block for the new file: ");
scanf("%d", &end);

if (start < 0 || start >= n || end < start || end >= n) {
    printf("Invalid range! Please enter valid start and end blocks.\n");
    return;
}

for (int i = start; i <= end; i++) {
    if (disk[i] == 1) {
        printf("Block %d is already allocated!\n", i);
        return;
    }
}

for (int i = start; i <= end; i++) {
    disk[i] = 1; // Mark blocks as allocated
}
printf("New file created successfully.\n");

}

void show_directory(int disk[], int n) {
printf("Directory:\n");
printf("File Name\tStart Block\tEnd Block\n");
int start_block = -1;
for (int i = 0; i < n; i++) {
if (disk[i] == 1 && start_block == -1) {
start_block = i;
} else if ((disk[i] == 0 || i == n - 1) && start_block != -1) {
printf("File%d\t\t%d\t\t%d\n", start_block, start_block, i - 1);
start_block = -1;
}
}
}
Q2)
#include <stdio.h>
#include <stdlib.h>

void c_scan(int requests[], int n_requests, int start_position, int total_blocks) {
int total_head_movements = 0;
int current_position = start_position;
int direction = 1; // 1 for right, -1 for left

printf("Order of request servicing:\n");

// Sorting requests in ascending order
for (int i = 0; i < n_requests - 1; i++) {
    for (int j = 0; j < n_requests - i - 1; j++) {
        if (requests[j] > requests[j + 1]) {
            int temp = requests[j];
            requests[j] = requests[j + 1];
            requests[j + 1] = temp;
        }
    }
}

// Scanning right
int i = 0;
while (i < n_requests && requests[i] < start_position) {
    i++;
}
if (i < n_requests) {
    for (; i < n_requests; i++) {
        printf("%d ", requests[i]);
        total_head_movements += abs(requests[i] - current_position);
        current_position = requests[i];
    }
    total_head_movements += abs(total_blocks - current_position);
    current_position = 0;
}

// Scanning left
for (int j = 0; j < i; j++) {
    printf("%d ", requests[j]);
    total_head_movements += abs(requests[j] - current_position);
    current_position = requests[j];
}

printf("\nTotal head movements: %d\n", total_head_movements);

}

int main() {
int n_requests, start_position, total_blocks;

printf("Enter the total number of disk blocks: ");
scanf("%d", &total_blocks);

printf("Enter the number of disk requests: ");
scanf("%d", &n_requests);

int *requests = (int *)malloc(n_requests * sizeof(int));

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

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

c_scan(requests, n_requests, start_position, total_blocks);

free(requests);

return 0;

}