1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#define println(strInput) printf("%s\n",strInput);
#define PROCESS_COUNT 5
#define RESOURCE_COUNT 5
// resource allocation graph deadlock detection implement
int main()
{

println("RaG implement by Jonathan Huang");
println("===== initial conditions =====");
println("Process: P1,P2,P3,P4,P5; Resource R1,R2,R3,R4,R5;");
int tmpM = 0;
int tmpN = 0;

// rag[m][n] ,m(row) for process , n(column) for resource.
int rag[PROCESS_COUNT+RESOURCE_COUNT+1][PROCESS_COUNT+RESOURCE_COUNT+1];
// wfg[m][m] , represent for the wait-for graph
int wfg[PROCESS_COUNT+1][PROCESS_COUNT+1];
int adj[PROCESS_COUNT+1][PROCESS_COUNT+1];

//initial the rag
for(tmpM = 0 ; tmpM < PROCESS_COUNT+RESOURCE_COUNT+1 ; tmpM++ )
{
	for(tmpN = 0 ; tmpN < PROCESS_COUNT+RESOURCE_COUNT+1 ; tmpN++ )
	{
		rag[tmpM][tmpN] = 0;
	}
}

//init the rag relationship
//process view fill
rag[1][PROCESS_COUNT+1] = 1;
rag[2][PROCESS_COUNT+3] = 1;
rag[2][PROCESS_COUNT+4] = 1;
rag[2][PROCESS_COUNT+5] = 1;
rag[3][PROCESS_COUNT+5] = 1;
rag[4][PROCESS_COUNT+2] = 1;

//resource view fill
rag[PROCESS_COUNT+2][1] = 1;
rag[PROCESS_COUNT+1][2] = 1;
rag[PROCESS_COUNT+4][3] = 1;
rag[PROCESS_COUNT+5][4] = 1;
rag[PROCESS_COUNT+3][5] = 1;


println("\nResource Allocation  graph in direct graph mapping  matrix :");
for(tmpM = 0 ; tmpM < PROCESS_COUNT+RESOURCE_COUNT+1 ; tmpM++ )
{
        if(tmpM != 0 ){
		if(tmpM > 5)
		{
			printf("R%d\t",tmpM-PROCESS_COUNT,PROCESS_COUNT+1);
		}
		else
		{
			printf("P%d\t",tmpM,PROCESS_COUNT+1);
		}
        }else{
                printf("\tP1 P2 P3 P4 P5 R1 R2 R3 R4 R5");
        }

        for(tmpN = 0 ; tmpN < PROCESS_COUNT+RESOURCE_COUNT+1 ; tmpN++ )
        {
		if(tmpM != 0 && tmpN != 0)
		{
                	printf(" %d ",rag[tmpM][tmpN]);
		}
        }
	println(" ");
}



println("\nCorresponding wait-for graph (generated by RAG) :");
//initial the wfg by translate from  wag to wfg
int tmpZ = 0;
int wfgEdges = 0;
for(tmpM = 0 ; tmpM < PROCESS_COUNT+1 ; tmpM++ )
{
	if(tmpM != 0 ){
		printf("P%d\t",tmpM,PROCESS_COUNT+1);
	}else{
		printf("\tP1\tP2\tP3\tP4\tP5");
	}
        for(tmpN = 0 ; tmpN < PROCESS_COUNT+1 ; tmpN++ )
        {
		wfg[tmpM][tmpN] = 0;
		adj[tmpM][tmpN] = 0;
		if(tmpM != 0 && tmpN != 0)
		{

///------------
        for(tmpZ = 0 ; tmpZ < RESOURCE_COUNT+1 ; tmpZ++ )
        {	
                if( (rag[tmpM][PROCESS_COUNT+tmpZ] == 1) && (rag[PROCESS_COUNT+tmpZ][tmpN] == 1) )
                {
                        wfg[tmpM][tmpN] = 1;
			adj[tmpM][tmpN] = 1;
			wfgEdges++;
                }
        }
///-------------
			printf("%d\t",wfg[tmpM][tmpN]);
		}
        }
	println(" ");
}

//use adjacency martix to find walks by wait for graph edges count
println("\n=======Use adjacency martix to find walks by wait for graph edges count.====");
printf("Do the matrix multiplication from A^1 to A^(Edges) ==> total edges : %d\n",wfgEdges);
int tmpX = 0;
int circleCount = 0;
int tmpMatrix[PROCESS_COUNT+1][PROCESS_COUNT+1];
for(tmpZ = 2;tmpZ < wfgEdges+1 ; tmpZ++)
{

	for(tmpM = 0 ; tmpM < PROCESS_COUNT+1 ; tmpM++ )
	{
	        for(tmpN = 0 ; tmpN < PROCESS_COUNT+1 ; tmpN++ )
	        {
	                tmpMatrix[tmpM][tmpN] = 0;
	        }
	}

	for(tmpM = 0;tmpM < PROCESS_COUNT+1 ;tmpM++)
	{
		for(tmpN = 0;tmpN < PROCESS_COUNT+1;tmpN++)
		{
			for(tmpX = 0;tmpX < PROCESS_COUNT+1;tmpX++)
			{
				if(tmpM >0 && tmpN > 0 && tmpX >0){
				tmpMatrix[tmpM][tmpN] += adj[tmpM][tmpX] * wfg[tmpX][tmpN];
				}
			}
//		printf("%d,%d ",tmpM,tmpN);
		}
//	printf("\n");
	}

        for(tmpM = 0 ; tmpM < PROCESS_COUNT+1 ; tmpM++ )
        {
                for(tmpN = 0 ; tmpN < PROCESS_COUNT+1 ; tmpN++ )
                {
                        adj[tmpM][tmpN] = tmpMatrix[tmpM][tmpN];
                }
        }


	circleCount = 0;
	for(tmpX = 0; tmpX < PROCESS_COUNT+1;tmpX++)
	{
		circleCount = circleCount + adj[tmpX][tmpX];
	}
	
	printf("%d edges circle count : %d\n",tmpZ,circleCount );
	if(circleCount > 0)
	{
		println("^^^^^^^ circle detected ^^^^^^^^^");
		break;
	}
}

return 0;
}