操作系统银行家算法下的安全性算法

操作系统银行家算法下的安全性算法

具体介绍:

(181条消息) 操作系统中的银行家算法与安全性算法_Tsuki_L的博客-CSDN博客_安全性算法

这里我只做我自己的实现,具体含义以及逻辑请参考上述的blog

算法实现

算法的目的是为了查询一个序列是否有安全性序列

pojo类即实体类

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
package com.dwx.pojo;

public class Task {
private int A;
private int B;
private int C;

public int getA() {
return A;
}

public void setA(int a) {
A = a;
}

public int getB() {
return B;
}

public void setB(int b) {
B = b;
}

public int getC() {
return C;
}

public void setC(int c) {
C = c;
}

@Override
public String toString() {
return "task{" +
"A=" + A +
", B=" + B +
", C=" + C +
'}';
}

public Task(int a, int b, int c) {
A = a;
B = b;
C = c;
}

public Task() {
}
}

安全性算法的实现

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
package com.dwx.untils;

import com.dwx.pojo.Task;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MySecurityAlgorithms {


//这是一个安全性算法

public void getSecurityList(int num, List<Task> taskList, Task available, List<Task> allocation) {
//数据
boolean finish[] = new boolean[num];
Task work = new Task();

//默认所有的finish初始化都为false
//数据初始化
for (int i = 0; i < num; i++) {
finish[i] = false;
}
work = available;

int[] safeList = new int[num];


//表示回合里边有程序能够执行 如果回合里没有程序能够执行 则说明没有足够量的资源可以执行 程序进入等待状态
boolean can = true;
//按照顺序遍历taskList
//找到能够完成的进程
int i = 0;
while (can) {
can = false;

//遍历整个taskList
for (int j = 0; j < num; j++) {

Task p = taskList.get(j);
Task a = allocation.get(j);
//如果资源都足够 且都符合执行条件 那么就执行
if (p.getA() <= work.getA() + a.getA() && p.getB() <= work.getB() + a.getB() && p.getC() <= work.getC() + a.getC() && !finish[j]) {
work.setA(work.getA() + a.getA());
work.setB(work.getB() + a.getB());
work.setC(work.getC() + a.getC());
finish[j] = true;
can = true;

//将序号读入安全序列
safeList[i] = j;
i++;
}
}
}
//如果所有的finish都为true 则说明有执行过了 也有安全的序列

int t = 0;
for (boolean b : finish) {
if (b) {
t++;
}
}

if (t == num) {
System.out.print("找到了安全序列 安全序列为:");
for (int k = 0; k < num; k++) {
if (k == num - 1) {
System.out.println("p" + safeList[k]);
} else {
System.out.print("p" + safeList[k] + "-->");
}
}
System.out.println();
} else {
System.out.println("未找到安全序列");
}

}

}

测试类

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
import com.dwx.pojo.Task;
import com.dwx.untils.MySecurityAlgorithms;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MySATest {
@Test
public void MySITest() {
MySecurityAlgorithms mySecurityAlgorithms = new MySecurityAlgorithms();
int num = 5;
//需求队列
List<Task> taskList = new ArrayList<Task>();

taskList.add(new Task(7,5,3));
taskList.add(new Task(3,2,2));
taskList.add(new Task(9,0,2));
taskList.add(new Task(2,2,2));
taskList.add(new Task(4,3,3));

//已分配资源队列
List<Task> allocation = new ArrayList<Task>();
allocation.add(new Task(0,1,0));
allocation.add(new Task(2,0,0));
allocation.add(new Task(3,0,2));
allocation.add(new Task(2,1,1));
allocation.add(new Task(0,0,2));

Task available = new Task(3,3,2);
mySecurityAlgorithms.getSecurityList(num,taskList,available,allocation);
}
}

结果

1
找到了安全序列 安全序列为:p1-->p3-->p4-->p0-->p2
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信