answersLogoWhite

0


Best Answer

/*this is a perfect code. No bugs.....*/

#include<stdio.h>

#define MAX 10

void main()

{

int f1,k,l,i,j,no_of_res,no_of_proc,temp,f,seq;

temp=0;

f=0;

f1=1;

printf("\nEnter the no of Resources: ");

scanf("%d",&no_of_res);

printf("\nEnter the no of process: ");

scanf("%d",&no_of_proc);

int flag[no_of_proc],sequence[no_of_proc],claim[no_of_proc][no_of_res],allocation[no_of_proc][no_of_res];

int needed[no_of_proc][no_of_res],resource[no_of_res],available[no_of_res];

for(i=0;i<no_of_proc;i++)

{

flag[i]=-1;

}

for(i=0;i<no_of_proc;i++)

{

sequence[i]=-1;

}

printf("\nEnter the Claimed Matrix for Process:");

for(i=0;i<no_of_proc;i++)

{

printf("\nEnter the value for Process %d:",i+1);

for(j=0;j<no_of_res;j++)

{

printf("\nEnter the value of Resource :%d:",j+1);

scanf("%d",&claim[i][j]);

}

}

printf("\nEnter the Maximum Resource available:");

for(i=0;i<no_of_res;i++)

{

printf("\nEnter the value of Resource :%d:",i+1);

scanf("%d",&resource[i]);

}

printf("\nEnter the Allocation Matrix for Process:");

for(i=0;i<no_of_proc;i++)

{

printf("\nEnter the value for Process %d:",i+1);

for(j=0;j<no_of_res;j++)

{

printf("\nEnter the value of Resource Allocated :%d:",j+1);

scanf("%d",&allocation[i][j]);

}

}

for(i=0;i<no_of_res;i++)

{

for(j=0;j<no_of_proc;j++)

{

temp=temp+allocation[j][i];

}

available[i]=resource[i]-temp;

if(available[i]<=0)

available[i]=0;

temp=0;

}

printf("\nThe Cliamed Matrix of Resources is:");

for(i=0;i<no_of_proc;i++)

{

printf("\nP%d\t",i);

for(j=0;j<no_of_res;j++)

{

printf("%d\t",claim[i][j]);

}

}

printf("\nvalue of Maximum Resource Available :\n");

for(j=0;j<no_of_res;j++)

{

printf("%d\t",resource[j]);

}

printf("\nAllocation Matrix for Proccess:");

for(i=0;i<no_of_proc;i++)

{

printf("\n");

for(j=0;j<no_of_res;j++)

{

printf("%d\t",allocation[i][j]);

}

}

printf("\nvalue of Available resources after alloction:\n");

for(j=0;j<no_of_res;j++)

{

printf("%d\t",available[j]);

}

for(i=0;i<no_of_proc;i++)

{

for(j=0;j<no_of_res;j++)

{

needed[i][j]=claim[i][j]-allocation[i][j];

if(needed[i][j]<=-1)

needed[i][j]=0;

}

}

printf("\n Needed Matrix after allocation:\n");

for(i=0;i<no_of_proc;i++)

{

printf("\n");

for(j=0;j<no_of_res;j++)

{

printf("%d\t",needed[i][j]);

}

}

i=0;

seq=0;

while(i<no_of_proc)

{

if(flag[i]==-1)

{

for(j=0;j<no_of_res;j++)

{

if(needed[i][j]<=available[j])

f=0;

else

{

f=1;

break;

}

}

if(f==0)

{

printf("\n====================================================================\n");

flag[i]=i+1;

sequence[seq]=i+1;

seq=seq+1;

printf("\nProcess P%d will Run until Complition",i+1);

for(j=0;j<no_of_res;j++)

{

available[j]=available[j]+allocation[i][j];

allocation[i][j]=0;

needed[i][j]=0;

}

printf("\nThe needed Matrix of Resources is:");

for(k=0;k<no_of_proc;k++)

{

printf("\n");

for(l=0;l<no_of_res;l++)

{

printf("%d\t",needed[k][l]);

}

}

printf("\nAllocation Matrix for Proccess:");

for(k=0;k<no_of_proc;k++)

{

printf("\n");

for(l=0;l<no_of_res;l++)

{

printf("%d\t",allocation[k][l]);

}

}

printf("\nvalue of Available resources after alloction:\n");

for(l=0;l<no_of_res;l++)

{

printf("%d\t",available[l]);

}

i=0;

}

if(f==1)

i++;

f=1;

}

else

{

i++;

}

}

for(i=0;i<no_of_proc;i++)

{

if(sequence[i]==-1)

break;

else

f1=0;

}

if(f1==1)

printf("\nSystem is not in Safe state.....");

if(f1==0)

{

printf("\nSystem is in Safe state.....");

printf("\n The Sequence of Completion of Process is:");

for(i=0;i<no_of_proc;i++)

{

printf("P%d ",sequence[i]);

}

}

printf("\n");

}

Written by: Fabianski Benjamin

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program for banker's algorithm?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Can you give a C program about SJF algorithm?

no.


How do you RSA algorithm c?

Perform encryption on the following PT using RSA and find the CT p = 3; q = 11; M = 5


How can i write pseudo code for C plus plus if i haven't even wrote the program yet?

You are going about this backwards. First, define the program. Second, describe its algorithm. Third, if needed, write pseudo code. (Sometime, algorithm and pseudo code is the same process.) Fourth, or third, write real code.


Program in c to develop an algorithm that prints the n value of algorithm?

reymond rillera reymond rillera


Write a c program to find smallest among three using function with pointers?

Please visit http://talentsealed.blogspot.com/2009/10/to-find-smallest-among-three-using.htmlfor answer.


Write a algorithm for doubly linked list in c?

sorry


How does algorithm and c plus plus program get along?

They are bosom-friends.


Algorithm to find the roots of a quadratic equation?

The easiest way to write a generic algorithm is to simply use the quadratic formula. If it is a computer program, ask the user for the coefficients a, b, and c of the generic equation ax2 + bx + c = 0, then just replace them in the quadratic formula.


How to write algorithm in c program?

Algorithms are created using pseudocode, which is a combination of natural language (such as English) and commonly understood programming concepts. Pseudocode is a machine-independent language, but it is far too abstract for a machine to understand. It is intended for humans only. As programmers, our job is to translate these algorithms into a form the machine can process in order to produce the required machine-dependent code. For this we use programming languages, such as C, C++ and Java. The more abstract the programming language, the easier it is to convert an algorithm into working code. Of all the high-level programming languages, C has the least amount of abstraction, however we can make use of third party libraries to increase the amount of abstraction, or we can use the language itself to create our own abstractions.


Can someone provide the C program for Prim's algorithm?

The C code for Prim's algorithm can be found in the following link. https://sites.google.com/site/itstudentjunction/lab-programming-solutions/data-structures-programs/program-to-find-minimal-spanning-tree-using--prims-algorithm


C program algorithm for simple interest using while loop?

Not used