Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Experiment-1

Aim:Conversionofresourceallocationgraph(RAG)towaitforgraph(WFG)foreachtypeofmethodu
sedforstoringgraph.

Theory: A wait-for graph is a directed graph used in computer science, particularly in


operatingsystemsandrelationaldatabases,fordetectingdeadlocks.Itcomesintoplaywhenyouhavemultiple
processes competing for shared resources that require exclusive access (locking). A deadlock
occurswhen two or more processes are perpetually waiting for each other to release resources they
need.
Code:

#include<stdio.h>
intmain(){
intnp,nr,temp,temp1;
printf("enternumberofresources:");sc
anf("%d",&nr);
printf("enternumberofprocesss:");sc
anf("%d",&np);
int rag[nr+np]
[nr+np];inti, j;
for(i=0;i<np+nr;i++)
{for(j=0; j<np+nr;j++)
{rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enterthenumberofresourcesprocess
%d,holding",i);scanf("%d",&temp);
for(j=0;j<temp;j++){
printf("entertheressorcenumberprocess
%dholding:",j);scanf("%d",&temp1);
rag[np+temp1][i]=1;
}
printf("enterthenumberofresourcesprocess
%d,requesting",i);scanf("%d",&temp);
for(j=0;j<temp;j++){
printf("entertheressorcenumberprocess
%drequesting:",i);scanf("%d",&temp1);
rag[i][np+temp1]=1;
}}
printf("RAG:\n");
for(i=0;i<np+nr;i++)
{for(j=0; j<np+nr;j++)
{printf("%d",rag[i][j]);
}
printf("\n");
}
int wfg[np]
[np];for(i=0;i<np;i
++){for(j=0;
j<np;j++){wfg[i]
[j]=0;
}
}
int k;
for(i=0;i<np;i++){
for(j=np;j<np + nr; j++)
{if(rag[i][j]==1){
for(k=0;k<np;k++)
{if(rag[j][k]==1)
wfg[i][k]=1;
}
}
}
}
printf("WFG :\
n");for(i=0;i<np;i+
+){for(j=0;j<np;j+
+){
printf("%d",wfg[i][j]);
}
printf("\n");
}
return0;
}

Output:
enter number of resources:
3enternumberofprocesss:3
enter the number of resources process 0, holding
1entertheressorcenumberprocess0holding:0
enterthenumberofresourcesprocess0,requesting1ente
rtheressorcenumberprocess0requesting:2
enterthenumberofresourcesprocess1,holding1ente
rtheressorcenumberprocess0holding:1
enterthenumberofresourcesprocess1,requesting1ente
rtheressorcenumberprocess1requesting:0
enterthenumberofresourcesprocess2,holding1ente
rtheressorcenumberprocess0holding:2
enter the number of resources process 2, requesting
1entertheressorcenumberprocess2requesting:1

RAG:
000001
000100
000010
100000
010000
001000
WFG :
001
100
010

You might also like