I) Consider the following snapshot of a system:
|
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 |
|||
Available |
|
||||||||
A |
B |
C |
|
||||||
3 |
3 |
2 |
|
Add the following functionalities in your program
1.
Accept
Available
2.
Display
Allocation, Max
3.
Display
the contents of need matrix
4. Display Available
5. Exit
Solution:
#include<stdio.h>
#include<conio.h>
int max[10][10],need[10][10],allocation[10][10];
int available[10],work[10],finish[10],request[10];
int np,nr,i,j,k,l,m,n,pid,pno;
int ch,cnt=0;
void accept_matrix(int A[10][10])
{
int i,j;
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&A[i][j]);
}
}
}
void Accept_Vector(int A[])
{
int i;
for(i=0;i<nr;i++)
{
scanf("%d",&A[i]);
}
}
void Find_need()
{
int i, j;
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
}
void Display_matrix()
{
int i,j;
printf("\nAllocation\tMax\t\tNeed\n");
for(i=0; i<np;i++)
{
for(j=0; j<nr;j++)
printf(" %d",allocation[i][j]) ;
printf("\t\t");
for (j=0; j<nr;j++)
printf(" %d",max[i][j]) ;
printf("\t\t");
for (j=0; j<nr;j++)
printf(" %d",need[i][j]) ;
printf("\n");
}
printf("\n Available\n");
for (j=0; j<nr;j++)
printf(" %d",available[j]);
printf("\t");
}
int main()
{
//clrscr();
printf("\n Enter the no of processes:");
scanf("%d",&np);
printf("\n Enter the no of resources:");
scanf("%d",&nr);
do
{
printf("\n\n--------MENU------------");
printf("\n\n1.Accept allocation:\n2.Accept Max:\n3.Calculate need\n4.Accept available\n5.Display Matrices\n6.exit:");
printf("\n\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the allocation matrix:");
accept_matrix(allocation);
break;
case 2: printf("\n Enter the max matrix:");
accept_matrix(max);
break;
case 3:Find_need();
break;
case 4: printf("\n Enter the available vector:");
Accept_Vector(available);
break;
case 5:Display_matrix();
break;
}
}while(ch!=6);
return 0;
}
0 Comments:
Post a Comment