Article Directory
data structure.
- n: integer, the number of processes;
- m: integer, the number of resource types;
- Available[m]: A one-dimensional array that stores the number of available instances of each resource in [1...m];
- Max[n][m]: n×m matrix, Max[i][j] represents the total demand for resource j by process i;
- Allocation[n][m]: n×m matrix, Allocation[i][j] represents the number of resources j already held by the process i;
- Need[n][m]: n×m matrix, Need[i][j] represents the number of resources j needed by the process i.
vector.
- For two vectors XX of length nX和YYY,当且仅当 X [ i ] < Y [ i ] X[i]<Y[i] X[i]<Y [ i ] for alli ∈ [1.. n] i∈[1..n]i∈[ 1 . . The n- ] When are true, we say thatX <Y. X <Y.X<And .
System security testing algorithm.
- Used to test whether the system is in a safe state at this time, the algorithm is as follows:
TestSafety()
{
//1.
Work[1..m]=Available[1..m];
//向量Work初始化为当前可用资源实例数向量.
Finish[1..n]=false;
//向量Finish每个分量都为false,表示初态都是未完成.
//2.
change=true; //循环控制变量.
while(change)
{
//Need[i]表示矩阵的第i行,也就是进程Pi所需资源数向量,
//若进程Pi未完成,并且需求能够得到满足,就执行Pi,后续
//释放其中的资源,等价于Work获得Pi已经持有的资源数向量
//Allocation[i].
change=false;
for(int i=1;i<=n;++i)
{
if(Finish[i]==false && Need[i]<Work)
{
Work=Work+Allocation[i];
Finish[i]=true;
change=true;
}
}
}
//不动点算法:只有当Finish和Allocation不再发生改变时,
//也就意味着所有的进程都已经完成或者剩余进程都无法运行
//这两种情况之一发生时,才会跳出while循环.
//3.
for(int i=1;i<=n;++i)
{
if(Finish[i]==false)
{
return false;
}
}
return true;
//全真为真,一假俱假.
}
Resource request algorithm.
- Request[n][m]: n×m matrix, Request[i][j] represents the number of resource j requested by the process i at this time.
if(Request[i]<Need[i])
{
if(Request[i]<Available)
{
//Simulate allocation.
Available=Available-Request[i];
Allocation[i]=Allocation[i]+Request[i];
Need[i]=Need[i]-Request[i];
if(TestSafety())
{
Agree Allocation;
//System safe,just allocate.
}
else
{
Rollback;
//System unsafe,rollback.
}
}
else
{
Wait;
//Cause resource insufficient.
}
}
else
{
Error;
//Cause request too many resources.
}
Example 1.
- Is the system in a safe state at this time?
- If the process P 1 P_1P1Make a resource request Request 1 = (1, 0, 2) Request_1 = (1, 0, 2)Request1=(1,0,2 ) Can it be satisfied?
- If the process P 0 P_0P0Make a resource request Request 0 = (0, 2, 0) Request_0 = (0, 2, 0)Request0=(0,2,0 ) , can it be satisfied?
- If process P 4 P_4P4Make a resource request Request 4 = (3, 3, 0) Request_4 = (3, 3, 0)Request4=(3,3,0 ) , can it be satisfied?
answer.
Example two.
2. Consider the state of the system at T0 as follows:
Allocation | Max | Available | |
---|---|---|---|
A B C D | A B C D | A B C D | |
P 0 P_0 P0 | 0 0 1 2 | 0 0 1 2 | 1 5 2 0 |
P 1 P_1 P1 | 1 0 0 0 | 1 7 5 0 | |
P 2 P_2 P2 | 1 3 5 4 | 2 3 5 6 | |
P 3 P_3 P3 | 0 6 3 2 | 0 6 5 2 | |
P 4 P_4 P4 | 0 0 1 4 | 0 6 5 6 |
- What is the content of Need?
- Is the system in a safe state at this moment?
- If P 1 P_1P1Request resource Request 1 = (0, 4, 2, 0) Request_1=(0,4,2,0)Request1=(0,4,2,0 ) , does the system satisfy its request? why?
answer.