QUESTION DESCRIPTION
Youve got three friends (Chandler, Ross, and Joey) who need a loan to tide them over for a bit.You have $24 with you.Chandler needs $8 dollars, Ross needs $13, and Joey needs $10.
You already lent $6 to Chandler, $8 to Ross, and $7 to Joey.So you are left with $24 - $21 (6+8+7) = $3Even after giving $6 to Chandler, he still needs $2.
Similarly, Ross needs $5 more and Joey $3.Until they get the amount they need, they can neither do whatever tasks they have to nor return the amount they borrowed. (Like a true friend!)You can pay $2 to Chandler, and wait for him to get his work done and then get back the entire $8. Or, you can pay $3 to Joey and wait for him to pay you back after his task is done.
You cant pay Ross because he needs $5 and you dont have enough. You can pay him once Chandler or Joey returns the borrowed amount after their work is done.
This state is termed as the safe state, where everyones task is completed and, eventually, you get all your money back.
Knowing Ross needs $10 urgently, instead of giving $8, you end up giving him $10.And you are left with only $1.In this state, Chandler still needs $2 more, Ross needs $3 more, and Joey still needs $3 more, but now you dont have enough money to give them and until they complete the tasks they need the money for, no money will be transferred back to you.
You give $2 to Chandler and let him complete his work. He returns your $8 which leaves you with $9. Out of this $9, you can give $5 to Ross and let him finish his task with total $13 and then return the amount to you, which can be forwarded to Joey to eventually let him complete his task.
(Once all the tasks are done, you can take Ross and Joey to Central Perk for not giving them priority.)
Youve got three friends (Chandler, Ross, and Joey) who need a loan to tide them over for a bit.You have $24 with you.Chandler needs $8 dollars, Ross needs $13, and Joey needs $10.
You already lent $6 to Chandler, $8 to Ross, and $7 to Joey.So you are left with $24 - $21 (6+8+7) = $3Even after giving $6 to Chandler, he still needs $2.
Similarly, Ross needs $5 more and Joey $3.Until they get the amount they need, they can neither do whatever tasks they have to nor return the amount they borrowed. (Like a true friend!)You can pay $2 to Chandler, and wait for him to get his work done and then get back the entire $8. Or, you can pay $3 to Joey and wait for him to pay you back after his task is done.
You cant pay Ross because he needs $5 and you dont have enough. You can pay him once Chandler or Joey returns the borrowed amount after their work is done.
This state is termed as the safe state, where everyones task is completed and, eventually, you get all your money back.
Knowing Ross needs $10 urgently, instead of giving $8, you end up giving him $10.And you are left with only $1.In this state, Chandler still needs $2 more, Ross needs $3 more, and Joey still needs $3 more, but now you dont have enough money to give them and until they complete the tasks they need the money for, no money will be transferred back to you.
You give $2 to Chandler and let him complete his work. He returns your $8 which leaves you with $9. Out of this $9, you can give $5 to Ross and let him finish his task with total $13 and then return the amount to you, which can be forwarded to Joey to eventually let him complete his task.
(Once all the tasks are done, you can take Ross and Joey to Central Perk for not giving them priority.)
TEST CASE 1
INPUT
INPUT
1
2
3
3
OUTPUTThe Claim Vector is: 3 3
The Allocated Resource Table: 0 0
The Maximum Claim Table: 0 0
Allocated resources: 0 0
Available resources: 3 3
Process1 is executing
The process is in safe state
Available vector: 3 3
TEST CASE 2
INPUT
INPUT
1
2
6
3
OUTPUTThe Claim Vector is: 6 3
The Allocated Resource Table: 0 0
The Maximum Claim Table: 0 0
Allocated resources: 0 0
Available resources: 6 3
Process1 is executing
The process is in safe state
Available vector: 6 3
Code :
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
int main()
{
scanf("%d", &processes);
for (i = 0; i < processes; i++)
{
running[i] = 1;
counter++;
}
scanf("%d", &resources);
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", ¤t[i][j]);
}
}
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &maximum_claim[i][j]);
}
}
printf("The Claim Vector is:");
for (i = 0; i < resources; i++)
{
printf(" %d", maxres[i]);
}
printf("\nThe Allocated Resource Table:");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf(" %d", current[i][j]);
}
printf("\n");
}
printf("The Maximum Claim Table:");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf(" %d", maximum_claim[i][j]);
}
printf("\n");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf("Allocated resources:");
for (i = 0; i < resources; i++)
{
printf(" %d", allocation[i]);
}
for (i = 0; i < resources; i++)
{
available[i] = maxres[i] - allocation[i];
}
printf("\nAvailable resources:");
for (i = 0; i < resources; i++)
{
printf(" %d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("Process%d is executing\n", i + 1);
running[i] = 0;
counter--;
safe = 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if (!safe)
{
printf("The processes are in unsafe state\n");
break;
}
else
{
printf("The process is in safe state");
printf("\nAvailable vector:");
for (i = 0; i < resources; i++)
{
printf(" %d", available[i]);
}
printf("\n");
}
}
return 0;
}