Assignment No1
Assignment No1
Assignment No:1
Set:A(1)
Assignment Name:Q.I)Add the following functionalities in your program
a)Accept Available
Display Allocation,Max
Display the content so find need matrix
Display Available
Q.2)I)Modify above program so as to include the following:
Accept Request for a process
Resource request algorithm
Safety algorithm
Consider a system with ‘n’ processes and ‘m’ resource types. Accept number of instances for every resource type.For each process
accept the allocation and maximum requirement matrices. Write a program to display the content so find need matrix and to check if
the given request of a process can be granted immediately or not.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
void check()
{
temp=0;
s=0;
for(i=0;i<p;i++)//Calculateneed=max−allocation
for(j=0;j<r;j++)
need[i][j]=max[i][j]−allocation[i][j];
printf("\nAllocationTableis:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d\t",allocation[i][j]);
printf("\n");
}
printf("\nNeedTableis:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d\t",need[i][j]);
printf("\n");
}
for(i=0;i<p;i++)
finish[i]=0;
for(i=0;i<r;i++)
work[i]=available[i];
while(temp<2)
{
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
if(finish[i]==0&&need[i][j]<=work[j])
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
{
for(j=0;j<r;j++)
work[j]=work[j]+allocation[i][j];
finish[i]=1;
safe[s++]=i;
}
}
temp++;
}
flag=0;
for(i=0;i<p;i++)
{
if(finish[i]==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\nSystem is in Deadlock state");
}
else
{
printf("\nSystem is in Safe state");
printf("\nSafe Sequenceis:");
for(i=0;i<p;i++)
printf("P%d\t",safe[i]);
}
main()
{
printf("\n~~~~~~~~~~~~~~~~~~~~~~~BANKER'SALGORITHM~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\nEnter the no of resources and processes:");
scanf("%d%d",&r,&p);
printf("\nEnter the AllocationTable:\n");
for(i=0;i<p;i++)//Accepttheallocationtable
for(j=0;j<r;j++)
scanf("%d",&allocation[i][j]);
check();
printf("\nDo U want to add new request:(0/1)");
scanf("%d",&ch);
if(ch==0)
exit(1);
printf("\nEntertheprocessno:");
scanf("%d",&index);
printf("\nEntertherequest:");
for(i=0;i<r;i++)
scanf("%d",&req[i]);
flag=0;
for(i=0;i<r;i++)
if(req[i]<=need[index][i])
flag=1;
else
flag=0;
if(flag==0)
{
printf("\nRequest can not be satisfied...");
exit(1);
}
for(i=0;i<r;i++)
if(req[i]<=available[i])
flag=1;
else
flag=0;
if(flag==0)
{
exit(1);
}
printf("\nReq
uest can not
be
satisfied...");
for(i=0;i<r;i++)
{
allocation[index][i]=allocation[index][i]+req[i];
available[i]=available[i]−req[i];
}
check();