os22
Q1)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#define ARRAY_SIZE 1000
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int *arr = (int *)malloc(ARRAY_SIZE * sizeof(int));
if (rank == 0) {
srand(time(NULL));
for (int i = 0; i < ARRAY_SIZE; i++) {
arr[i] = rand() % 1000;
}
}
int sum_local = 0;
for (int i = rank; i < ARRAY_SIZE; i += size) {
if (arr[i] % 2 != 0) {
sum_local += arr[i];
}
}
int sum_global;
MPI_Reduce(&sum_local, &sum_global, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
printf("Sum of all odd numbers: %d\n", sum_global);
}
free(arr);
MPI_Finalize();
return 0;
}
Q2)
#include <stdio.h>
#include <stdlib.h>
#define MAX_BLOCKS 100
void showBitVector(int bitVector[], int n) {
printf("Bit Vector: ");
for (int i = 0; i < n; i++) {
printf("%d ", bitVector[i]);
}
printf("\n");
}
void deleteFile(int bitVector[], int n) {
int blockNum;
printf("Enter the block number to delete the file: ");
scanf("%d", &blockNum);
if (blockNum >= 0 && blockNum < n) {
if (bitVector[blockNum] == 1) {
bitVector[blockNum] = 0;
printf("File deleted successfully from block %d\n", blockNum);
} else {
printf("Error: Block %d is already free\n", blockNum);
}
} else {
printf("Error: Invalid block number\n");
}
}
int main() {
int n;
printf("Enter the total number of disk blocks: ");
scanf("%d", &n);
int bitVector[MAX_BLOCKS] = {0}; // Initialize all blocks as free
while (1) {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Delete already created file\n");
printf("3. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(bitVector, n);
break;
case 2:
deleteFile(bitVector, n);
break;
case 3:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}