os6
Q1)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define MAX_BLOCKS 100
bool disk[MAX_BLOCKS] = {false}; // Array to represent disk blocks
void showBitVector(int n) {
printf("Bit Vector:\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void createNewFile(int n) {
int block;
srand(time(0));
do {
block = rand() % n;
} while (disk[block]); // Keep generating random blocks until we find a free one
disk[block] = true;
printf("New file created at block %d\n", block);
}
void showDirectory(int n) {
printf("Allocated blocks:\n");
for (int i = 0; i < n; i++) {
if (disk[i]) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int n, choice;
printf("Enter the total number of disk blocks: ");
scanf("%d", &n);
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("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory(n);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
Q2)
#include <stdio.h>
#include <stdlib.h>
#define MAX_REQUESTS 100
void cScan(int requests[], int n, int start, int direction) {
int totalHeadMovements = 0;
int currentHead = start;
printf("Head Movement Sequence: ");
if (direction == 1) { // Right direction
// Move head to the end of the disk
for (int i = currentHead; i <= 199; i++) {
printf("%d ", i);
totalHeadMovements += abs(currentHead - i);
currentHead = i;
}
// Move head to the beginning of the disk
for (int i = 0; i < start; i++) {
printf("%d ", i);
totalHeadMovements += abs(currentHead - i);
currentHead = i;
}
} else { // Left direction
// Move head to the beginning of the disk
for (int i = currentHead; i >= 0; i--) {
printf("%d ", i);
totalHeadMovements += abs(currentHead - i);
currentHead = i;
}
// Move head to the end of the disk
for (int i = 199; i > start; i--) {
printf("%d ", i);
totalHeadMovements += abs(currentHead - i);
currentHead = i;
}
}
printf("\nTotal Head Movements: %d\n", totalHeadMovements);
}
int main() {
int requests[MAX_REQUESTS];
int n, start, direction;
printf("Enter the total number of disk blocks: ");
scanf("%d", &n);
printf("Enter the disk request string (enter -1 to terminate): ");
int i = 0;
while (1) {
scanf("%d", &requests[i]);
if (requests[i] == -1) {
break;
}
i++;
}
printf("Enter the starting head position: ");
scanf("%d", &start);
printf("Enter the direction (1 for Right, 0 for Left): ");
scanf("%d", &direction);
cScan(requests, n, start, direction);
return 0;
}