os3
Q1)
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 4
void calculateNeedMatrix(int need[MAX_PROCESSES][MAX_RESOURCES], int max[MAX_PROCESSES][MAX_RESOURCES], int allocation[MAX_PROCESSES][MAX_RESOURCES]) {
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
// Function to check if the system is in a safe state
bool isSafeState(int available[MAX_RESOURCES], int max[MAX_PROCESSES][MAX_RESOURCES], int allocation[MAX_PROCESSES][MAX_RESOURCES]) {
int work[MAX_RESOURCES];
bool finish[MAX_PROCESSES] = {false};
// Initialize work to available
for (int i = 0; i < MAX_RESOURCES; i++) {
work[i] = available[i];
}
// Find an index i such that both:
// (a) Finish[i] == false
// (b) Need[i] <= Work
bool found;
int safeSequence[MAX_PROCESSES];
int count = 0;
do {
found = false;
for (int i = 0; i < MAX_PROCESSES; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < MAX_RESOURCES; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == MAX_RESOURCES) {
for (int k = 0; k < MAX_RESOURCES; k++) {
work[k] += allocation[i][k];
}
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
} while (found);
// Check if all processes are finished
if (count == MAX_PROCESSES) {
printf("System is in a safe state\n");
printf("Safe sequence: ");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d ", safeSequence[i]);
}
printf("\n");
return true;
} else {
printf("System is not in a safe state\n");
return false;
}
}
int main() {
int allocation[MAX_PROCESSES][MAX_RESOURCES] = {{0, 0, 1, 2}, {1, 0, 0, 0}, {1, 3, 5, 4}, {0, 6, 3, 2}, {0, 0, 1, 4}};
int max[MAX_PROCESSES][MAX_RESOURCES] = {{0, 0, 1, 2}, {1, 7, 5, 0}, {2, 3, 5, 6}, {0, 6, 5, 2}, {0, 6, 5, 6}};
int available[MAX_RESOURCES] = {1, 5, 2, 0};
int need[MAX_PROCESSES][MAX_RESOURCES];
// Calculate the need matrix
calculateNeedMatrix(need, max, allocation);
// Display the need matrix
printf("Need Matrix:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d: ", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
// Check if the system is in a safe state
isSafeState(available, max, allocation);
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 array[ARRAY_SIZE];
int local_sum = 0, global_sum = 0;
double local_avg = 0.0, global_avg = 0.0;
// Initialize MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Seed the random number generator
srand(rank);
// Generate random numbers
for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand() % 100; // Generating numbers between 0 and 99
}
// Calculate local sum
for (int i = 0; i < ARRAY_SIZE; i++) {
local_sum += array[i];
}
// Calculate global sum
MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
// Calculate global average
local_avg = (double)local_sum / ARRAY_SIZE;
MPI_Reduce(&local_avg, &global_avg, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
global_avg /= size;
// Print results
if (rank == 0) {
printf("Total sum: %d\n", global_sum);
printf("Average: %.2f\n", global_avg);
}
// Finalize MPI
MPI_Finalize();
return 0;
}