CATS专题一+进程同步模型.信号量机制
总结:先看进程优先级,优先级高的进程先进行,一般在CATS中第三个才会出现P/V操作
两个信号量都初始值为零,P操作为信号量减一 如:X=X-1=-1. V操作为信号量加一 如:Y=Y+1=1.
其中如果信号量为-1时,当前进程为阻塞状态,切换到另一进程。
如果信号量为1时,按当前进程继续执行。
如果信号量未经过操作前为-1,但经过V操作变为了0要注意是否为抢占式优先级调度,
如果为抢占式则优先级高的进程执行。
如果为非抢占式则按当前进程执行。
其他情况,如:信号量为1,经过P操作减为0时,进程按顺序进行。
以下题为例:
优先级Y>X
信号量R 和 H
采用抢占式优先级调度
【1】order:Y1 Y2 Y3 (此时R=R-1=-1,换到另一进程) X1 X2 X3 (此时R=R+1=0,抢占式,进程优先级高的执行) Y4 Y5(H=H+1=1,继续执行) Y6 X4 X5 X6
【2】按照顺序一步一步将对应的值写出
initallization这一列共八个空为Y1 Y2 X1 X2 Y4 Y6 X4 X6 的式子
E,C,S.Value 对应的值 按初值和对应每一步E,C,S的值填写。
CATS专题三+银行家算法.安全性算法(含做题代码)
Need 对应的Max值减Allocation值
Available的值为对应的Resource的值减去Allocation一列的值
Work初值为Available
查找ABC资源全部Need<=Work的进程
执行此进程 Work+Available 赋值给work
进程按P0-P5的顺序进行循环扫描
下面按顺序(字母全部小写,“,”表示间隔)
银行家算法代码实现(C++)
/**
来源:网络
**/
#include <iostream>
#include <vector>
using namespace std;
#define MAX 20
int n_process; // 表示进程的个数
int n_resource; // 表示资源的个数
int Resource[MAX]; // 表示资源的总数
int Max[MAX][MAX]; // 表示进程对每类资源的最大需求量
int Allocation[MAX][MAX]; // 表示系统给进程已分配每类资源的数目
int Need[MAX][MAX]; // 表示进程还需各类资源数目
int Available[MAX]; // 表示系统当前剩下的资源
int Work[MAX]; // 表示安全性检查的中间变量
bool Finish[MAX]; // 表示资源是否被安全性检查过
vector<int> Safeorder; // 表示安全序列
void Menu()
{
cout << "------------------Banker----------------------" << endl;
cout << "* 1.初始化数据 *" << endl;
cout << "* 2.显示资源分配情况 *" << endl;
cout << "* 3.申请资源并生成安全序列 *" << endl;
cout << "* 4.退出 *" << endl;
cout << "----------------------------------------------" << endl;
cout << "请选择:";
}
void checkInit()
{
if (n_resource)
for (int i = 0; i < n_process; i++)
{
for (int j = 0; j < n_resource; j++)
{
if (Max[i][j] < 0)
cout << "Max[" << i << "][" << j << "]输入值小于0!"
<< endl;
if (Allocation[i][j] < 0)
cout << "Allocation[" << i << "][" << j << "]输入值小于0!"
<< endl;
if (Allocation[i][j] > Max[i][j])
cout << "Allocation[" << i << "][" << j << "]的值大于Max["
<< i << "][" << j << "]输入值" << endl;
}
}
for (int i = 0; i < n_resource; i++)
{
if (Available[i] < 0)
cout << "Available[" << i << "]的值小于0!" << endl;
}
cout << "输入检查完毕!" << endl;
}
int Init()
{
if (n_resource != 0 && n_process != 0)
{
cout << "你已经初始化过了!" << endl;
return 1;
}
cout << "请分别输入资源个数和进程个数,中间用空格隔开:" << endl;
cin >> n_resource >> n_process;
cout << "请输入各个资源的总拥有量(Resource):" << endl;
for (int i = 0; i < n_resource; i++)
cin >> Resource[i];
for (int i = 0; i < n_process; i++)
{
cout << "P" << i << "对各个资源的最大需求量(Max):" << endl;
for (int j = 0; j < n_resource; j++)
cin >> Max[i][j];
cout << "P" << i << "各个资源已分配量(Allocation):" << endl;
for (int j = 0; j < n_resource; j++)
cin >> Allocation[i][j];
for (int j = 0; j < n_resource; j++)
Need[i][j] = Max[i][j] - Allocation[i][j];
}
for (int i = 0; i < n_resource; i++)
{
int sum[MAX] = {0};
for (int j = 0; j < n_process; j++)
{
if (i == 0)
sum[i] += Allocation[j][i];
if (i == 1)
sum[i] += Allocation[j][i];
if (i == 2)
sum[i] += Allocation[j][i];
}
Available[i] = Resource[i] - sum[i];
}
checkInit();
return 1;
}
bool Safecheck()
{
Safeorder.clear();
for (int i = 0; i < n_resource; i++)
Work[i] = Available[i];
for (int i = 0; i < n_process; i++)
Finish[i] = false;
// 开始安全性检查
int count = 0;
for (int k = 0; k < n_process; k++)
{
for (int i = 0; i < n_process; i++)
{
if (Finish[i] == false)
{
count = 0;
for (int j = 0; j < n_resource; j++)
{
if (Need[i][j] <= Work[j])
count++;
}
if (count == n_resource)
{
for (int j = 0; j < n_resource; j++)
{
Work[j] = Work[j] + Allocation[i][j];
}
Finish[i] = true;
Safeorder.push_back(i);
}
}
}
}
count = 0;
for (int i = 0; i < n_process; i++)
{
if (Finish[i] == true)
count++;
}
if (count == n_process)
return true;
else
return false;
}
int Order()
{
int n = -1; // 请求资源的进程号
int *Request = new int[n_resource]; // 表示请求的各个资源数量
cout << "请输入第一个满足条件的进程Need(各资源均小于Available)的进程号:";
cin >> n;
cout << "请输入满足条件进程Need各资源的数量 空格隔开:" << endl;
for (int i = 0; i < n_resource; i++)
cin >> Request[i];
// 开始判断
for (int i = 0; i < n_resource; i++)
{
if (Need[n][i] < Request[i])
{
cout << "错误,需求量比你的最大需求量还大"<< endl;
return 1;
}
}
for (int i = 0; i < n_resource; i++)
{
if (Available[i] < Request[i])
{
cout << "系统已经满足不了你了,你走吧!" << endl;
return 1;
}
}
// 试分配资源给请求进程,并做安全性检查
for (int i = 0; i < n_resource; i++)
{
Available[i] -= Request[i];
Allocation[n][i] += Request[i];
Need[n][i] -= Request[i];
}
bool Is_safe = Safecheck();
if (Is_safe == true)
{
cout << "系统已经分配资源给P" << n << "进程了!" << endl;
cout << "其中一个安全序列为:" << endl;
for (int i = 0; i < Safeorder.size(); i++)
{
cout << "P" << Safeorder.at(i) << "->";
}
cout << "End" << endl;
}
// 恢复试分配之前的现场
for (int i = 0; i < n_resource; i++)
{
Available[i] += Request[i];
Allocation[n][i] -= Request[i];
Need[n][i] += Request[i];
}
return 1;
}
void Display()
{
cout << endl;
cout << "进程 \t Max \t Allocation\tNeed\tAvailable" << endl;
for (int i = 0; i < n_process; i++)
{
cout << " P" << i << " \t";
for (int j = 0; j < n_resource; j++)
{
cout << Max[i][j] << " ";
}
cout << "\t ";
for (int j = 0; j < n_resource; j++)
{
cout << Allocation[i][j] << " ";
}
cout << "\t ";
for (int j = 0; j < n_resource; j++)
{
cout << Need[i][j] << " ";
}
cout << "\t ";
for (int j = 0; i == 0 && j < n_resource; j++)
{
cout << Available[j] << " ";
}
cout << endl;
}
cout << endl;
}
void ShowTable()
{
cout << "Prog*\t\t"
<< "Work\t\t"
<< "Need\t\t"
<< "Allo*\t\t"
<< "W + A\t\t"
<< "Finish" << endl;
for (int k = 0; k < Safeorder.size(); k++)
{
cout << "P" << Safeorder.at(k) << "\t";
int i = Safeorder.at(k);
for (int j = 0; j < n_resource; j++)
{
cout << Available[j] << " ";
}
cout << "\t\t";
for (int j = 0; j < n_resource; j++)
{
cout << Need[i][j] << " ";
}
cout << "\t\t";
for (int j = 0; j < n_resource; j++)
{
cout << Allocation[i][j] << " ";
Available[j] += Allocation[i][j];
}
cout << "\t\t";
for (int j = 0; j < n_resource; j++)
{
cout << Available[j] << " ";
}
cout << "\t\t"
<< "true" << endl;
}
cout << "End" << endl;
}
int main()
{
int choose = 0;
while (1)
{
Menu();
cin >> choose;
switch (choose)
{
case 1:
Init();
break;
case 2:
Display();
break;
case 3:
Order();
ShowTable();
break;
case 4:
cout << "系统已退出!";
return 1;
default:
cout << "就1-4这你都能输错???" << endl;
break;
}
}
}
做题代码参考使用