0% found this document useful (0 votes)
4 views2 pages

Slip 3

solved slip

Uploaded by

Vivek Dalvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Slip 3

solved slip

Uploaded by

Vivek Dalvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

slip3 Q1

#include <stdio.h>
#define N 5
#define M 4
int main()
{
int allocation[N][M] = {{0, 0, 1, 2},
{1, 0, 0, 0},
{1, 3, 5, 4},
{0, 6, 3, 2},
{0, 0, 1, 4}};
int max[N][M] = {{0, 0, 1, 2},
{1, 7, 5, 0},
{2, 3, 5, 6},
{0, 6, 5, 2},
{0, 6, 5, 6}};
int available[M] = {1, 5, 2, 0};
int need[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
int finish[N] = {0};
int safe_seq[N];
int work[M];
for (int i = 0; i < M; i++) {
work[i] = available[i];
}
int count = 0;
while (count < N) {
int found = 0;
for (int i = 0; i < N; i++) {
if (finish[i] == 0) {
int j;
for (j = 0; j < M; j++) {
if (need[i][j] > work[j])
break;
}
if (j == M) {
for (int k = 0; k < M; k++) {
work[k] += allocation[i][k];
}
safe_seq[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (found == 0) {
printf("System is not in safe state.\n");
return 0;
}
}
printf("Need matrix:\n");
for (int i = 0; i < N; i++) {
printf("P%d: ", i);
for (int j = 0; j < M; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
printf("System is in safe state.\nSafe sequence is: ");
for (int i = 0; i < N; i++) {
printf("P%d ", safe_seq[i]);
}
printf("\n");
return 0;
}

Q2

#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
int main (int argc, char* argv[]){
int i,my_id, num_procs,N=50;
int array[N],array_final_sum[N],array_final_mult[N];
int r_num,max_value;
unsigned seed;
double t0, t1, time;
MPI_Init(&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &my_id);
MPI_Comm_size (MPI_COMM_WORLD, &num_procs);
t0 = MPI_Wtime();
for(i=0;i<N;i++){
array[i]=my_id +1 ;
}
MPI_Reduce(array,array_final_sum,N,MPI_INT,MPI_SUM,0,MPI_COMM_WO
R LD);
MPI_Reduce(array,array_final_mult,N,MPI_INT,MPI_PROD,0,MPI_COMM_W
O RLD);
if(my_id == 0) {
for(i=0;i<N;i++) printf("Final array after sum: %d\n", array_final_sum[i]);
}
if(my_id == 0) {
for(i=0;i<N;i++) printf("Final array after product: %d\n", array_final_mult[i]) ;
}
seed=my_id+1;
srand(seed);
r_num=rand();
printf("my id is %d and r_num is %d\n", my_id,r_num);
MPI_Reduce(&r_num,&max_value,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
t1 = MPI_Wtime();
time = t1 - t0 ;
if(my_id == 0) {
printf("Max_value is (AND THE WINNER IS ....): %d\n", max_value) ;
printf("Total elapsed time [sec] : %f\n", time); }
MPI_Finalize();
return 0;
}

You might also like