os11
Q1)
#include <stdio.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 3},
{2, 1, 1},
{0, 0, 2}
};
int max[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 0},
{2, 0, 2},
{0, 0, 0},
{1, 0, 0},
{0, 0, 2}
};
int available[MAX_RESOURCES] = {0, 0, 0};
void acceptAvailable() {
printf("Enter the available resources:\n");
for (int i = 0; i < MAX_RESOURCES; i++) {
scanf("%d", &available[i]);
}
}
void displayAllocationMax() {
printf("Process\tAllocation\tMax\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d\t", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", allocation[i][j]);
}
printf("\t\t");
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
}
void displayNeedMatrix() {
printf("Need Matrix:\n");
printf("Process\tNeed\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d\t", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", max[i][j] - allocation[i][j]);
}
printf("\n");
}
}
void displayAvailable() {
printf("Available resources: ");
for (int i = 0; i < MAX_RESOURCES; i++) {
printf("%d ", available[i]);
}
printf("\n");
}
int main() {
char choice;
do {
printf("\nMenu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch(choice) {
case 'a':
acceptAvailable();
break;
case 'b':
displayAllocationMax();
break;
case 'c':
displayNeedMatrix();
break;
case 'd':
displayAvailable();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 'e');
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 i;
int array[ARRAY_SIZE];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Seed random number generator based on rank
srand(rank);
// Generate random numbers
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand() % 1000; // Generate numbers between 0 and 999
}
// Find local minimum
int local_min = array[0];
for (i = 1; i < ARRAY_SIZE; i++) {
if (array[i] < local_min) {
local_min = array[i];
}
}
// Find global minimum using MPI_Reduce
int global_min;
MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
if (rank == 0) {
printf("Minimum number: %d\n", global_min);
}
MPI_Finalize();
return 0;
}