Operating System sem 6 practical exam program
How to run MPI program
for compiler : mpicc programname.c -o programname
for run : mpirun -np 2 ./programname
//Mpi - min max
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <limits.h>
#define ARRAY_SIZE 1000
int main(int argc, char** argv) {
int rank, size, i;
int local_min = INT_MAX, local_max = INT_MIN;
int global_min, global_max;
int array[ARRAY_SIZE];
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Generate random array on process all process
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand();
}
// Find local minimum and maximum values
for (i = 0; i < ARRAY_SIZE / size; i++) {
if (array[i] < local_min) {
local_min = array[i];
}
if (array[i] > local_max) {
local_max = array[i];
}
}
// Reduce local minimum and maximum values to global minimum and maximum values
MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
MPI_Reduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
// Print results on process 0
if (rank == 0) {
printf("Global minimum value = %d\n", global_min);
printf("Global maximum value = %d\n", global_max);
}
MPI_Finalize();
return 0;
}
Mpi-sum
//mpi sum
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define SIZE 1000
int main(int argc, char** argv) {
int rank, size, i;
double sum = 0.0, local_sum = 0.0;
double data[SIZE];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// generate random numbers on root process
for (i = 0; i < SIZE; i++) {
data[i] = (double) rand() / RAND_MAX;
}
// calculate local sum
for (i = 0; i < SIZE/size; i++) {
local_sum += data[i];
}
// calculate global sum
MPI_Reduce(&local_sum, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
// print the result
if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}
MPI_Finalize();
return 0;
}
FCFS:
//fcfs
#include<stdio.h>
#include<stdlib.h>
void main(){
int rq[199],initial,i,headm=0,n;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
for(i=0;i<n;i++){
headm +=abs(rq[i]-initial);
initial=rq[i];
}
printf("\ntotal moment %d",headm);
}
SSTF:
#include<stdio.h>
#include<stdlib.h>
int min(int q[],int n){
int i,min=1000,index,d;
return index;
}
void main(){
int rq[199],initial,i,headm=0,n,min=1000,index,d,count=0;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
while (count!=n)
{
min=1000;
for(i=0;i<n;i++){
d=abs(rq[i]-initial);
if(min>=d){
min=d;
index=i;
}
}
headm+=min;
initial=rq[index];
rq[index]=1000;
count++;
}
printf("\ntotal moment %d",headm);
}
Or
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for sstf disk scheduling
/* loop will execute until all process is completed*/
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
SCAN
//scan
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,initial,sum=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
int dir;
printf("\n Enter the direction:left[0]/right[1]:");
scanf("%d",&dir);
for(int i=1;i<n;i++)
{
for(int j=0;i>j;j++)
{
if(RQ[i]<RQ[j])
{
int temp = RQ[i];
RQ[i] = RQ[j];
RQ[j] = temp;
}
}
}
printf("\n Sorting the Request Sequence:\n");
for(int i=0;i<n;i++)
printf("\n %d",RQ[i]);
if(dir==0)
{
sum = (initial-0)+RQ[n-1];
}
if(dir==1)
{
sum = (199-initial)+(199-RQ[0]);
}
printf("\n The total head movement is :%d",sum);
}
/* OUTPUT: I'm don....
Enter the number of Requests
8
Enter the Requests sequence
176
79
34
60
92
11
41
114
Enter initial head position
50
Enter the direction:left[0]/right[1]:1
Sorting the Request Sequence:
11
34
41
60
79
92
114
176
The total head movement is :337
*/
OR
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,Totalheadmovement=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
if(move==1)
{
for(i=index;i<n;i++)
{
Totalheadmovement=Totalheadmovement+abs(RQ[i]-initial);
initial=RQ[i];
}
Totalheadmovement=Totalheadmovement+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
Totalheadmovement=Totalheadmovement+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for(i=index-1;i>=0;i--)
{
Totalheadmovement=Totalheadmovement+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",Totalheadmovement);
return 0;
}
CSCAN
//c-scan
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,initial,sum=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
int dir;
printf("\n Enter the direction:left[0]/right[1]:");
scanf("%d",&dir);
// int last = Rq[n];
for(int i=1;i<n;i++)
{
for(int j=0;i>j;j++)
{
if(RQ[i]<RQ[j])
{
int temp = RQ[i];
RQ[i] = RQ[j];
RQ[j] = temp;
}
}
}
printf("\n Sorting the Request Sequence:\n");
for(int i=0;i<n;i++)
printf("\n %d",RQ[i]);
int lastLeft=0,lastRight=0;
//find the last element
for(int i=0; i<n; i++)
{
if(RQ[i]<initial)
lastLeft = RQ[i];
else
{
lastRight = RQ[i];
break;
}
}
if(dir==0)
{
sum = (initial-0)+(199-0)+(199-lastRight);
}
if(dir==1)
{
sum = (199-initial)+(199-0)+(lastLeft-0);
}
printf("\n The total head movement is :%d",sum);
}
/* OUTPUT: I'm don....
Enter the number of Requests
8
Enter the Requests sequence
176
79
34
60
92
11
41
114
Enter initial head position
50
Enter the direction:left[0]/right[1]:1
Sorting the Request Sequence:
11
34
41
60
79
92
114
176
The total head movement is :389
*/
OR
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
LOOK
//look
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,initial,sum=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
int dir;
printf("\n Enter the direction:left[0]/right[1]:");
scanf("%d",&dir);
// int last = Rq[n];
for(int i=1;i<n;i++)
{
for(int j=0;i>j;j++)
{
if(RQ[i]<RQ[j])
{
int temp = RQ[i];
RQ[i] = RQ[j];
RQ[j] = temp;
}
}
}
printf("\n Sorting the Request Sequence:\n");
for(int i=0;i<n;i++)
printf("\n %d",RQ[i]);
if(dir==0)
{
sum = (initial-RQ[0])+(RQ[n-1]-RQ[0]);
}
if(dir==1)
{
sum = (RQ[n-1]-initial)+(RQ[n-1]-RQ[0]);
}
printf("\n The total head movement is :%d",sum);
}
/* OUTPUT: I'm don....
Enter the number of Requests
8
Enter the Requests sequence
176
79
34
60
92
11
41
114
Enter initial head position
50
Enter the direction:left[0]/right[1]:0
Sorting the Request Sequence:
11
34
41
60
79
92
114
176
The total head movement is :204
*/
OR
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
CLOOK
//c-look
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,initial,sum=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
int dir;
printf("\n Enter the direction:left[0]/right[1]:");
scanf("%d",&dir);
// int last = Rq[n];
for(int i=1;i<n;i++)
{
for(int j=0;i>j;j++)
{
if(RQ[i]<RQ[j])
{
int temp = RQ[i];
RQ[i] = RQ[j];
RQ[j] = temp;
}
}
}
printf("\n Sorting the Request Sequence:\n");
for(int i=0;i<n;i++)
printf("\n %d",RQ[i]);
int lastLeft=0,lastRight=0;
//find the last element
for(int i=0; i<n; i++)
{
if(RQ[i]<initial)
lastLeft = RQ[i];
else
{
lastRight = RQ[i];
break;
}
}
if (dir == 0)
{
sum = (initial - RQ[0]) + (RQ[n - 1] - RQ[0]) + (RQ[n - 1] - lastRight);
}
if(dir==1)
{
sum = (RQ[n-1]-initial)+(RQ[n-1]-RQ[0])+(lastLeft-RQ[0]);
}
printf("\n The total head movement is :%d",sum);
}
/* OUTPUT: I'm don....
Enter the number of Requests
8
Enter the Requests sequence
176
79
34
60
92
11
41
114
Enter initial head position
50
Enter the direction:left[0]/right[1]:0
Sorting the Request Sequence:
11
34
41
60
79
92
114
176
The total head movement is :320
*/
OR
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
Sequential File Allocation:
#include <stdio.h>
#include <stdlib.h>
void recurse(int files[])
{
int flag = 0, startBlock, len, j, k, ch;
printf("Enter the starting block and the length of the files: ");
scanf("%d%d", &startBlock, &len);
for (j=startBlock; j<(startBlock+len); j++)
{
if (files[j] == 0)
flag++;
}
if(len == flag)
{
for (int k=startBlock; k<(startBlock+len); k++)
{
if (files[k] == 0){
files[k] = 1;
printf("%d\t%d\n", k, files[k]);
}
}
if (k != (startBlock+len-1))
printf("The file is allocated to the disk\n");
}
else
printf("The file is not allocated to the disk\n");
printf("Do you want to enter more files?\n");
printf("Press 1 for YES, 0 for NO: ");
scanf("%d", &ch);
if (ch == 1)
recurse(files);
else
exit(0);
return;
}
int main()
{
int files[50];
for(int i=0;i<50;i++)
files[i]=0;
printf("Files Allocated are :\n");
recurse(files);
return 0;
}
Or
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int bit[256],n,fb;
typedef struct d
{
char fname[25];
int length,start;
struct d *next;
}NODE;
NODE *last,*first;
void init()
{
printf("Enter the no. of block\n");//10
scanf("%d",&n);
for (int i = 0; i < n; i++)
bit[i] =1;// initialinzing bit vector as free
fb=n;//free block
}
void BitVector(){
for (int i = 0; i < n; i++)
printf("%d\t\n",bit[i]);
}
void createFile(){
NODE *f;
int i,j;
int len;
char fname[24];
printf("Enter file name and length : ");
scanf("%s%d",&fname,&len);
f=(NODE*)malloc(sizeof(NODE));
if(fb>len){
strcpy(f->fname,fname);
f->length=len;
for (i = 0; i < n; i++)
{
if (bit[i]==1)
break;
}
f->start=i;
f->next=NULL;
if (first==NULL)// directry mokli asel tar pahilyapasun chalu kar
{
first=f;
}
else{//nahitar shevtchya file la chya shevtipasun chalu kar
last->next=f;//second file start
}
last=f;
fb-=len;//fb=fb-len
j=i-1;
while (len>0)
{
if(bit[j]==1){
bit[i]=0;
i=j;
len--;
}
j++;//to increase length
}
bit[i]=f->start+f->length;//to calculte the bit vector
printf("File %s created successully.\n",fname);
}
else{
printf("Insufficent Space");
}
}
void show_dir(){
NODE *f;
f=first;
printf("\tFile\tStart\tLength\n");
while (f!=NULL)
{
printf("%7s\t%8d\t%9d\n",f->fname,f->start,f->length);
f=f->next;
}
}
void fdelete(){
char fname[100];
NODE *fp,*fn;
int i,j;
printf("Enter the file name to delete :\n");
scanf("%s",&fname);
fp=fn=first;
while (fn!=NULL)
{
if(strcmp(fname,fn->fname)==0)
break;
fp=fn;
fn=fn->next;
}
if (fn==NULL)
{
printf("File not found\n");
}
else{
i=fn->start;
while (bit[i]!=(fn->start+fn->length)){
bit[i++]=1;
}
bit[i]=1;
fb+=fn->length;
if(fn==first){
first=first->next;
}
else if(fn==last){
last=fp;
last->next=NULL;
}
else{
fp->next=fn->next;
}
free(fn);
printf("File %s deleted successfully.\n",fname);
}
}
void main()
{
int choice;
init();
do
{
printf("1 :Show bit vector\n");
printf("2 :Create new file \n");
printf("3 :Show Directry\n");
printf("4 :Delete file\n");
printf("5 :Exit\n");
printf("\nEnter Your choice\n :");
scanf("%d",&choice);
switch (choice)
{
case 1:
BitVector();
break;
case 2:
createFile();
break;
case 3:
show_dir();
break;
case 4:
fdelete();
break;
default:
break;
}
} while (choice<5);
}
Linked File Allocation:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void recursivePart(int pages[]){
int st, len, k, c, j;
printf("Enter the index of the starting block and its length: ");
scanf("%d%d", &st, &len);
k = len;
if (pages[st] == 0){
for (j = st; j < (st + k); j++){
if (pages[j] == 0){
pages[j] = 1;
printf("%d------>%d\n", j, pages[j]);
}
else {
printf("The block %d is already allocated \n", j);
k++;
}
}
}
else
printf("The block %d is already allocated \n", st);
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursivePart(pages);
else
exit(0);
return;
}
int main(){
int pages[50], p, a;
for (int i = 0; i < 50; i++)
pages[i] = 0;
printf("Enter the number of blocks already allocated: ");
scanf("%d", &p);
printf("Enter the blocks already allocated: ");
for (int i = 0; i < p; i++){
scanf("%d", &a);
pages[a] = 1;
}
recursivePart(pages);
getch();
return 0;
}
Index File Allocation:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n",
ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Bankers Algorithm:
/* Add the following functionalities in your program
a) Accept Available
b) Display Allocation, Max
c) Display the contents of need matrix
d) Display Available
Process Allocation MAX
A B C A B C
P0 0 1 0 7 5 3
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
*/
#include<stdio.h>
int max[10][10],allocation[10][10],need[10][10];
int available[10];
int nr,np;
void readMatrix(int matrix[10][10])
{
for(int i=0;i<np;i++)
{
for(int j=0;j<nr;j++)
{
scanf("%d",&matrix[i][j]);
}
}
}
int displayMatrix(int matrix[10][10])
{
for(int i=0;i<np;i++)
{
printf("\nP%d\t",i);
for(int j=0;j<nr;j++)
{
printf("%d\t",matrix[i][j]);
}
}
}
int calNeed()
{
for(int i=0;i<np;i++)
{
for(int j=0;j<nr;j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
void banker()
{
int i, j, k = 0, flag;
int finish[10], safe_seq[10];
for (i = 0; i < np; i++)
{
finish[i] = 0; // declare as all process are incomplete
}
for (i = 0; i < np; i++)
{
flag = 0;
if(finish[i]==0) //execute incomplete process
{
for (j = 0; j < nr; j++)
{
if(need[i][j]>available[j])
{
flag = 1; // break loop as need is greater than avil and go to next process
break;
}
}
if(flag==0) // need is lesser than avail so complete process
{
finish[i] = 1;
safe_seq[k] = i;
k++;
// add allocated resources of finished process in available resources
for (j = 0; j < nr; j++)
available[j] += allocation[i][j];
//start checking from first process again.
i = -1;
}
}
}
flag = 0; // check is all processes are completed
for (i = 0; i < np; i++)
{
if(finish[i]==0)
{
printf("\n The system is in deadlock..");
flag = 1;
break;
}
}
if(flag==0)
{
printf("\n The system is in safe state \n Safe sequence is : ");
for (i = 0; i < np; i++)
printf("P%d", safe_seq[i]);
}
}
int main()
{
printf("\n Enter the no. of processess:");
scanf("%d",&np);
printf("\n Enter the no. of resources:");
scanf("%d",&nr);
printf("\nEnter the Matrix:");
readMatrix(allocation);
printf("\nEnter the max requirement Matrix:");
readMatrix(max);
printf("\nEnter the available Matrix:");
int i;
for(int i=0; i<nr; i++)
{
scanf("%d",&available[i]);
}
printf("\nThe display of allocation Matrix:");
displayMatrix(allocation);
printf("\nThe display of Max Requirement Matrix:");
displayMatrix(max);
printf("\nThe display of Available Matrix:\n");
for(int i=0; i<nr; i++)
{
printf("%d\t",available[i]);
}
printf("\nThe need of matrix is:");
calNeed();
displayMatrix(need);
banker();
}
/* OUTPUT:
Enter the no. of processess:4
Enter the no. of resources:3
Enter the Matrix:3 2 1
2 3 1
1 2 3
3 2 1
Enter the max requirement Matrix:3 2 4
3 3 4
4 3 3
3 4 3
Enter the available Matrix:1 2 3
The display of allocation Matrix:
P0 3 2 1
P1 2 3 1
P2 1 2 3
P3 3 2 1
The display of Max Requirement Matrix:
P0 3 2 4
P1 3 3 4
P2 4 3 3
P3 3 4 3
The display of Available Matrix:
1 2 3
The need of matrix is:
P0 0 0 3
P1 1 0 3
P2 3 1 0
P3 0 2 2
The system is in safe state
Safe sequence is : P0P1P2P3
*/
Comments
Post a Comment
hey