OneCompiler

os2

111

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

#define MAX_BLOCKS 1000

int disk[MAX_BLOCKS]; // Bit vector representing disk blocks
int free_blocks[MAX_BLOCKS]; // Array to maintain the list of free blocks
int num_free_blocks; // Number of free blocks

void initializeDisk(int n) {
for (int i = 0; i < n; i++) {
disk[i] = 0; // Mark all blocks as free initially
free_blocks[i] = i; // Maintain list of free blocks
}
num_free_blocks = n; // Initialize number of free blocks
}

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

void createNewFile(int n) {
if (num_free_blocks == 0) {
printf("Error: Disk is full. Cannot create new file.\n");
return;
}

int startBlock = free_blocks[rand() % num_free_blocks]; // Randomly select a free block
disk[startBlock] = 1; // Mark block as allocated
num_free_blocks--; // Decrement number of free blocks
for (int i = 0; i < num_free_blocks; i++) {
    if (free_blocks[i] == startBlock) {
        // Shift remaining free blocks to fill the gap
        for (int j = i; j < num_free_blocks - 1; j++) {
            free_blocks[j] = free_blocks[j + 1];
        }
        break;
    }
}
printf("New file created with start block: %d\n", startBlock);

}

void showDirectory(int n) {
printf("Directory (Allocated Blocks):\n");
printf("Block Number\n");
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
printf("%d\n", i);
}
}
}

int main() {
int n, choice;
srand(time(NULL)); // Seed for random number generation

printf("Enter the number of blocks in the disk: ");
scanf("%d", &n);
initializeDisk(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!\n");
    }
} while (choice != 4);

return 0;

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

#define ARRAY_SIZE 1000

int main(int argc, char** argv) {
int rank, size;
int local_array[ARRAY_SIZE];
int global_sum = 0, local_sum = 0;

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

// Initialize random number generator
srand(rank);

// Generate random numbers for local array
for (int i = 0; i < ARRAY_SIZE; i++) {
    local_array[i] = rand() % 100; // Generate random numbers between 0 and 99
}

// Calculate local sum
for (int i = 0; i < ARRAY_SIZE; i++) {
    local_sum += local_array[i];
}

// Gather local sums from all processes to calculate global sum
MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if (rank == 0) {
    printf("Global Sum: %d\n", global_sum);
}

MPI_Finalize();
return 0;

}