process Allocation Max Available
--------- ------------ ------ -----------
p0 3 6 2
p1 4 8
p2 0 1
p3 3 5
p4 1 6
p5 2 12
Solution:Banker's algorithm using c
#include<stdio.h>
int executed[6];
int main()
{
int p = 6, i;
char process[10];
int need[6];
int allocated[6];
int max[6];
int avail = 2, k,j;
printf("Enter current allocation:");
for (i = 0; i<p; i++)
scanf("%d", &allocated[i]);
printf("Enter max claim:");
for (i = 0; i<p; i++)
scanf("%d", &max[i]);
//calculate need
for (i = 0; i<p; i++)
{
need[i] = max[i] - allocated[i];
}
k = 0;
while (1)
{
for (i = 0; i<p; i++)
{
if (need[i] <= avail)
{
executed[k++] = i;
need[i] = 99999999999; //sentinel element
if (i < 6)
{
avail = allocated[i] + avail;
}
}
}
if (k == p)
{
break;
}
}
printf("Safe sequence:");
for (i = 0; i<p; i++)
{
printf(" P%d ", executed[i]);
}
return 0;
}
--------- ------------ ------ -----------
p0 3 6 2
p1 4 8
p2 0 1
p3 3 5
p4 1 6
p5 2 12
Solution:Banker's algorithm using c
#include<stdio.h>
int executed[6];
int main()
{
int p = 6, i;
char process[10];
int need[6];
int allocated[6];
int max[6];
int avail = 2, k,j;
printf("Enter current allocation:");
for (i = 0; i<p; i++)
scanf("%d", &allocated[i]);
printf("Enter max claim:");
for (i = 0; i<p; i++)
scanf("%d", &max[i]);
//calculate need
for (i = 0; i<p; i++)
{
need[i] = max[i] - allocated[i];
}
k = 0;
while (1)
{
for (i = 0; i<p; i++)
{
if (need[i] <= avail)
{
executed[k++] = i;
need[i] = 99999999999; //sentinel element
if (i < 6)
{
avail = allocated[i] + avail;
}
}
}
if (k == p)
{
break;
}
}
printf("Safe sequence:");
for (i = 0; i<p; i++)
{
printf(" P%d ", executed[i]);
}
return 0;
}
Comments
Post a Comment