OneCompiler

os16

107

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);

// Initialize disk blocks randomly as free (0) or allocated (1)
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>
#include <mpi.h>

#define ARRAY_SIZE 1000

int main(int argc, char *argv[]) {
int rank, size;
int array[ARRAY_SIZE];
int min_local, min_global;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

// Generate random numbers in each process
srand(rank);
for (int i = 0; i < ARRAY_SIZE; i++) {
    array[i] = rand();
}

// Calculate local minimum
min_local = array[0];
for (int i = 1; i < ARRAY_SIZE; i++) {
    if (array[i] < min_local) {
        min_local = array[i];
    }
}

// Find global minimum using MPI_Reduce
MPI_Reduce(&min_local, &min_global, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);

if (rank == 0) {
    printf("Minimum number: %d\n", min_global);
}

MPI_Finalize();
return 0;

}